广播接收器android.intent.action.BOOT_COMPLETED无法正常工作

时间:2016-11-04 10:42:49

标签: android broadcastreceiver android-service android-broadcastreceiver

我正在尝试在启动完成时启动服务,在进行大量挖掘之后我什么都没找到。

我尝试的第一种方法是this,我在大多数答案中都找到了这种方法,但它不起作用。

然后我发现这种方法不适用于here

的3.1+

我也见过这个,https://developer.android.com/about/versions/android-3.1.html#launchcontrols

并在我的代码中也使用了 FLAG_INCLUDE_STOPPED_PACKAGES 。我从here引用了这种方法。

AndroidMenifest

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.REAL_GET_TASKS"/>

<application
    android:allowBackup="true"
    android:icon="@drawable/security"
    android:label="@string/app_name"
    android:supportsRtl="true">

    <receiver android:name="com.reversebits.projects.app.easyerase.receiver.BootReceiver"
        android:permission="android.permission.RECEIVE_BOOT_COMPLETED"
        android:enabled="true"
        android:exported="true">

        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
        </intent-filter>

        <intent-filter>
            <action android:name="myReceiver" />
        </intent-filter>

    </receiver>

    <activity
        android:name=".Home"
        android:theme="@style/AppTheme">

        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>

    </activity>

    <service
        android:name=".services.BootService"
        android:enabled="true">

        <intent-filter>
            <action android:name="com.reversebits.projects.app.easyerase.services.BootService"/>
        </intent-filter>

    </service>

</application>

BootReciever

public class BootReceiver extends BroadcastReceiver {

    public BootReceiver() {
    }
    @Override
    public void onReceive(Context context, Intent intent) {

        if(intent.getAction().equalsIgnoreCase(Intent.ACTION_BOOT_COMPLETED))
        {
            context.startService(new Intent(context, BootService.class));
        }
    }
}

BootService

public class BootService extends Service {
    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        //TODO do something useful Recommended by google instead of onStart()
        return Service.START_STICKY;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        loopThread();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }


    void loopThread() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                AudioManager am = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
                switch (am.getRingerMode()) {
                    case AudioManager.RINGER_MODE_SILENT:
                        Log.e("MyApp", "Silent mode");
                        break;
                    case AudioManager.RINGER_MODE_VIBRATE:
                        Log.e("MyApp", "Vibrate mode");
                        break;
                    case AudioManager.RINGER_MODE_NORMAL:
                        Log.e("MyApp", "Normal mode");
                        break;

                }
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                loopThread();
            }
        }).start();
    }
}

代码工作正常,当服务从活动开始时我得到了所有日志,但在启动完成时,它无法正常工作

注意: 我已经阅读了几乎所有相关的答案和一些答案,我也阅读了评论,我也知道几乎所有相关的事情,所以请不要提供其他链接,因为已经看到大部分已经,我需要完美的解决方案。任何帮助表示赞赏。

2 个答案:

答案 0 :(得分:1)

清单文件中的

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

  

<receiver android:name=".BootCompletedReceiver" >
    <intent-filter>
        <action android:name="android.intent.action.BOOT_COMPLETED" />
        <action android:name="android.intent.action.QUICKBOOT_POWERON" />
    </intent-filter>
</receiver>

<service android:name="NotifyingDailyService" >
</service>

BootCompletedReceiver类

public class BootCompletedReceiver extends BroadcastReceiver {

  @Override
 public void onReceive(Context context, Intent arg1) {
  // TODO Auto-generated method stub
   Log.w("boot_broadcast_poc", "starting service...");
   context.startService(new Intent(context, NotifyingDailyService.class));
   }

  }

服务类

public class NotifyingDailyService extends Service {

  @Override
  public IBinder onBind(Intent arg0) {
  // TODO Auto-generated method stub
  return null;
  }

   @Override
     public int onStartCommand(Intent pIntent, int flags, int startId) {
    // TODO Auto-generated method stub
     Toast.makeText(this, "NotifyingDailyService",     
      Toast.LENGTH_LONG).show();

      Log.i("com.example.bootbroadcastpoc","NotifyingDailyService");

     return super.onStartCommand(pIntent, flags, startId);
      }
     }

答案 1 :(得分:1)

  <receiver android:name=".BootCompletedIntentReceiver"
        android:enabled="true"
        android:exported="true"
        >
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
        </intent-filter>
    </receiver>

    <service android:name=".Service"
        android:enabled="true"
        android:exported="true">

    </service>

          public class BootCompletedIntentReceiver extends BroadcastReceiver {
   public void onReceive(Context context, Intent intent) {

    if ("android.intent.action.BOOT_COMPLETED".equals(intent.getAction())) {
        Intent pushIntent = new Intent(context, Service.class);

        context.startService(pushIntent);}

并且不要忘记

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

服务

public class BootService extends Service {
@Override
public IBinder onBind(Intent intent) {
    return null;
}

@Override
public int onStart(Intent intent, int startId) {
    //TODO do something useful Recommended by google instead of onStart()
     new Thread(new Runnable() {
        @Override
        public void run() {
            AudioManager am = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
            switch (am.getRingerMode()) {
                case AudioManager.RINGER_MODE_SILENT:
                    Log.e("MyApp", "Silent mode");
                    break;
                case AudioManager.RINGER_MODE_VIBRATE:
                    Log.e("MyApp", "Vibrate mode");
                    break;
                case AudioManager.RINGER_MODE_NORMAL:
                    Log.e("MyApp", "Normal mode");
                    break;

            }
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

        }
    }).start();
}




@Override
public void onDestroy() {
    super.onDestroy();
}
  }