BOOT_COMPLETED не вызывает BroadcastRecevier

Я просмотрел несколько сообщений на этом сайте о том, что BOOT_COMPLETED не звонит Broadcast Receiver. Я думал, что моя проблема похожа на этот вопрос BroadcastReceiver не работает после BOOT_COMPLETED

Однако я не пытаюсь захватить более одного Intent в одном сервисе. Я просто хочу действие BOOT_COMPLETED.

Можете ли вы просмотреть мой код и сообщить мне, где и как это исправить?

Вот Манифест:

    <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.google.android.gcm.demo.app"
    android:versionCode="1"
    android:versionName="1.0" >

    <!-- GCM requires Android SDK version 2.2 (API level 8) or above. -->
    <!--
         The targetSdkVersion is optional, but it's always a good practice
         to target higher versions.
    -->
    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="16" />

    <!-- GCM connects to Google Services. -->
    <uses-permission android:name="android.permission.INTERNET" />

    <!-- GCM requires a Google account. -->
    <uses-permission android:name="android.permission.GET_ACCOUNTS" />

    <!-- Keeps the processor from sleeping when a message is received. -->
    <uses-permission android:name="android.permission.WAKE_LOCK" />

    <!--
     Creates a custom permission so only this app can receive its messages.

     NOTE: the permission *must* be called PACKAGE.permission.C2D_MESSAGE,
           where PACKAGE is the application's package name.



    -->
    <permission
        android:name="com.google.android.gcm.demo.app.permission.C2D_MESSAGE"
        android:protectionLevel="signature" />

    <uses-permission android:name="com.google.android.gcm.demo.app.permission.C2D_MESSAGE" />

    <!-- This app has permission to register and receive data message. -->
    <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />

    <!-- permission for network state -->
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

    <uses-permission android:name="android.permission.BATTERY_STATS" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

    <!-- Main activity. -->

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name="com.google.android.gcm.demo.app.DemoActivity"
            android:label="@string/app_name"
            android:screenOrientation="portrait" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name="com.google.android.gcm.demo.app.sqllite.SQLViews" >
        </activity>
        <activity android:name="com.google.android.gcm.demo.app.ScheduleView" >
        </activity>
        <activity android:name="com.google.android.gcm.demo.app.ConventionView" >
        </activity>
        <activity android:name="com.google.android.gcm.demo.app.EventList.DataView" >
        </activity>
        <activity android:name="com.google.android.gcm.demo.app.EventList.EventDetails" >
        </activity>
        <activity android:name="com.google.android.gcm.demo.app.Alerts.AlertView" >
        </activity>
        <activity android:name="com.google.android.gcm.demo.app.Alerts.AlertDetails" >
        </activity>

        <service
            android:name="com.google.android.gcm.demo.app.Alerts.AlarmsService"
            class="com.google.android.gcm.demo.app.Alerts.AlarmsService" >
            <intent-filter>
                <action
                    android:name="com.google.android.gcm.demo.app.Alerts.AlarmsService"
                    android:value="com.google.android.gcm.demo.app.Alerts.AlarmsService" />
            </intent-filter>
        </service>

        <receiver
            android:name="com.google.android.gcm.demo.app.Alerts.AlarmsBroadcastReceiver"
            android:enabled="true"
            android:exported="true" >
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
            </intent-filter>
        </receiver>

        <!--
          BroadcastReceiver that will receive intents from GCM
          services and handle them to the custom IntentService.

          The com.google.android.c2dm.permission.SEND permission is necessary
          so only GCM services can send data messages for the app.




        -->
        <receiver
            android:name="com.google.android.gcm.GCMBroadcastReceiver"
            android:permission="com.google.android.c2dm.permission.SEND" >
            <intent-filter>

                <!-- Receives the actual messages. -->
                <action android:name="com.google.android.c2dm.intent.RECEIVE" />
                <!-- Receives the registration id. -->
                <action android:name="com.google.android.c2dm.intent.REGISTRATION" />

                <category android:name="com.google.android.gcm.demo.app" />
            </intent-filter>
        </receiver>

        <!--
          Application-specific subclass of GCMBaseIntentService that will
          handle received messages.

          By default, it must be named .GCMIntentService, unless the
          application uses a custom BroadcastReceiver that redefines its name.



        -->

        <service android:name=".GCMIntentService" />
    </application>

</manifest>

Широковещательный приемник:

package com.google.android.gcm.demo.app.Alerts;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

public class AlarmsBroadcastReceiver extends BroadcastReceiver {

    private static final String TAG = "BootReceiver";

    Intent startServiceIntent;
    @Override
    public void onReceive(Context context, Intent intent) {

        if (intent.getAction().equals("android.intent.action.BOOT_COMPLETED")) {

            startServiceIntent = new Intent(context, AlarmsService.class);
            context.startService(startServiceIntent);
            Log.d("TAG", "alarmBroadcastReceiver");

            System.out.println("alarm broadcast reciver...");

        }



    }

}

Услуга:

 package com.google.android.gcm.demo.app.Alerts;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;


public class AlarmsService extends Service {


       String tag="alerttService";
       @Override
       public void onCreate() {
           super.onCreate();
           Toast.makeText(this, "Service created...", Toast.LENGTH_LONG).show();      
           Log.i(tag, "Service created...");
       }

       public int onStartCommand(Intent intent, int flags, int startId) {
            Log.d("TAG", "Service command started.");
            return super.onStartCommand(intent, flags, startId);
        }


       @Override
       public void onStart(Intent intent, int startId) {      
           super.onStart(intent, startId);

           Toast.makeText(this, "Service started...", Toast.LENGTH_LONG).show();   
           Log.i(tag, "Service started. ..");
       }

       @Override
       public void onDestroy() {
           super.onDestroy();
           Toast.makeText(this, "Service destroyed...", Toast.LENGTH_LONG).show();
       }

       @Override
       public IBinder onBind(Intent intent) {
           return null;
       }

}

person Leoa    schedule 12.08.2012    source источник
comment
Он установлен на SDCard или внутренний? Вы пытались установить android:installLocation на internalOnly в элементе <manifest>?   -  person Jens    schedule 12.08.2012
comment
Где вы тестируете этот код (устройство/эмулятор? какая версия ОС?)? Вы запускали свою активность вручную (например, через программу запуска) перед попыткой перезагрузки?   -  person CommonsWare    schedule 12.08.2012
comment
Я тестирую это на эмуляторе. Мин. 8, Цель 16.   -  person Leoa    schedule 12.08.2012
comment
По какой-то очень странной причине, когда я создал новый эмулятор, он у меня заработал. однако он не работает в Honeycomeb или более ранних настройках устройства на эмуляторе.   -  person Leoa    schedule 12.08.2012
comment
Я также перезагрузился с помощью adb ....   -  person Leoa    schedule 12.08.2012


Ответы (1)


Я использовал эмуляторы, которые я создал до того, как установил SDK для Jellybean. Я создал новые эмуляторы для всех версий, и теперь программа работает в Android 8 и выше на эмуляторе.

person Leoa    schedule 12.08.2012