我的broadcastReceiver工作正常,但是当我重新启动手机时,所有通知都停止工作.
AndroidManifest.xml中
<?xml version="1.0" enCoding="utf-8"?><manifest xmlns:androID="http://schemas.androID.com/apk/res/androID" package="com.example.a52780.nontifications" androID:installLocation="internalOnly"> <uses-permission androID:name="androID.permission.SET_ALARM"/> <uses-permission androID:name="androID.permission.RECEIVE_BOOT_COMPLETED "/> <application androID:allowBackup="true" androID:icon="@mipmap/ic_launcher" androID:label="@string/app_name" androID:roundIcon="@mipmap/ic_launcher_round" androID:supportsRtl="true" androID:theme="@style/Apptheme"> <activity androID:name=".MainActivity"> <intent-filter> <action androID:name="androID.intent.action.MAIN" /> <category androID:name="androID.intent.category.LAUNCHER" /> </intent-filter> </activity> <receiver androID:name=".AlarmReceiver" androID:priority="999"> <intent-filter> <action androID:name="androID.intent.action.BOOT_COMPLETED" /> <action androID:name="androID.intent.action.QUICKBOOT_POWERON" /> </intent-filter> </receiver> </application></manifest>
AlarmReceiver.java
public class AlarmReceiver extends broadcastReceiver { @OverrIDe public voID onReceive(Context context, Intent intent) { // Get ID & message from Intent int notificationID = intent.getIntExtra("notificationID", 0); String message = intent.getStringExtra("text"); // when notif. is tapped, call MainActivity Intent mainIntent = new Intent(context, MainActivity.class); PendingIntent contentIntent = PendingIntent.getActivity(context, 0, mainIntent, 0); notificationmanager mynotificationmanager = (notificationmanager) context.getSystemService(Context.NOTIFICATION_SERVICE); // PREPARE NOTIF Notification.Builder builder = new Notification.Builder(context); builder.setSmallicon(androID.R.drawable.ic_dialog_info) .setContentTitle("Wake up!") .setContentText(message) .setWhen(System.currentTimeMillis()) .setContentIntent(contentIntent); mynotificationmanager.notify(notificationID, builder.build()); }}
另外我试着把androID:enabled =“true”和androID:exported =“false”放在接收器中.但它没有用.应用程序安装在手机内存中,而不是安装在SD卡上. broadcastReceiver应该可以工作,无论如何它在重启后都不起作用
解决方法:
我刚刚找到解决方案!
AndroIDManifest.xml中
<receiver androID:name="com.example.a52780.nontifications.AlarmReceiver" androID:enabled="true" androID:exported="false" androID:permission="androID.permission.RECEIVE_BOOT_COMPLETED"> <intent-filter> <action androID:name="androID.intent.action.BOOT_COMPLETED" /> <action androID:name="androID.intent.action.QUICKBOOT_POWERON" /> </intent-filter> </receiver> <service androID:name="com.example.a52780.nontifications.BootService" androID:enabled="true" androID:exported="false"/>
我还添加了BootService.java
private voID setAlarm(Intent intent) { int notificationID = intent.getIntExtra("notificationID", 0); String text = intent.getStringExtra("text"); Intent mainIntent = new Intent(this, MainActivity.class); PendingIntent contentIntent = PendingIntent.getActivity(this, 0, mainIntent, 0); notificationmanager mynotificationmanager = (notificationmanager) this.getSystemService(Context.NOTIFICATION_SERVICE); Notification.Builder builder = new Notification.Builder(this); builder.setSmallicon(androID.R.drawable.ic_dialog_info) .setContentTitle("Wake up!") .setContentText(text) .setWhen(System.currentTimeMillis()) .setContentIntent(contentIntent); mynotificationmanager.notify(notificationID, builder.build());}@OverrIDeprotected voID onHandleIntent(@Nullable Intent intent) { setAlarm(intent); Intent service = new Intent(this, BootService.class); stopService(service);}
AlarmReceiver.java
private static final String BOOT_COMPLETED = "androID.intent.action.BOOT_COMPLETED";private static final String QUICKBOOT_POWERON = "androID.intent.action.QUICKBOOT_POWERON";@OverrIDepublic voID onReceive(Context context, Intent intent) { // Get ID & message from Intent int notificationID = intent.getIntExtra("notificationID", 0); String message = intent.getStringExtra("text"); String action = intent.getAction(); if (BOOT_COMPLETED.equals(action) || QUICKBOOT_POWERON.equals(action)) { Intent service = new Intent(context, BootService.class); intent.putExtra("notificationID", notificationID); intent.putExtra("text", message); context.startService(service); } }
我在MainActivity中更改了调用intent的代码.
旧:
Intent intent = new Intent(MainActivity.this, AlarmReceiver.class); intent.putExtra("notificationID", notificationID); intent.putExtra("text", text); intent.putExtra("alarmStartTime", alarmStartTime); PendingIntent alarmIntent = PendingIntent.getbroadcast(MainActivity.this, notificationID, intent, PendingIntent.FLAG_CANCEL_CURRENT); AlarmManager alarm = (AlarmManager) getSystemService(ALARM_SERVICE);
IDk为什么,但添加后才有效
intent.setAction("androID.intent.action.BOOT_COMPLETED");intent.setAction("androID.intent.action.QUICKBOOT_POWERON");
总结 以上是内存溢出为你收集整理的java – 重新启动后BroadcastReceiver无法正常工作全部内容,希望文章能够帮你解决java – 重新启动后BroadcastReceiver无法正常工作所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)