我的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应该可以工作,但是重新启动后仍然不能工作
答案 0 :(得分:0)
我刚刚找到了解决方案!
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());
}
@Override
protected 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";
@Override
public 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 = 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");