广播接收器 android.intent.action.BOOT_COMPLETED 不工作
Broadcast receiver android.intent.action.BOOT_COMPLETED not working
我正在尝试在引导完成时启动服务,经过大量挖掘后我什么也没发现。
我尝试的第一个方法是 this,我在大多数答案中都发现了这种方法,但它不起作用。
然后我发现这种方法不适用于 here
的 3.1+
这个我也看到了,https://developer.android.com/about/versions/android-3.1.html#launchcontrols
并且在我的 code.I 中也使用了 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));
}
}
}
引导服务
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();
}
}
代码工作完美,当服务从 activity 开始时我得到了所有日志,但是在引导完成时,它不工作
注:
我已经阅读了几乎所有相关的答案和一些答案,我也阅读了评论并且我知道几乎所有相关的东西,所以请不要给我提供其他链接,因为已经看到了大部分,我需要完美的解决方案。感谢任何帮助。
在清单文件中:
<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 class
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));
}
}
服务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);
}
}
<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();
}
}
我正在尝试在引导完成时启动服务,经过大量挖掘后我什么也没发现。
我尝试的第一个方法是 this,我在大多数答案中都发现了这种方法,但它不起作用。
然后我发现这种方法不适用于 here
的 3.1+这个我也看到了,https://developer.android.com/about/versions/android-3.1.html#launchcontrols
并且在我的 code.I 中也使用了 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));
}
}
}
引导服务
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();
}
}
代码工作完美,当服务从 activity 开始时我得到了所有日志,但是在引导完成时,它不工作
注: 我已经阅读了几乎所有相关的答案和一些答案,我也阅读了评论并且我知道几乎所有相关的东西,所以请不要给我提供其他链接,因为已经看到了大部分,我需要完美的解决方案。感谢任何帮助。
在清单文件中:
<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 class
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));
}
}
服务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);
}
}
<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();
}
}