如何创建即使在 phone 重启后也始终 运行 的通知?
How to create a notification that always run even after phone reboot?
我在 MainActivity 的 setAlarm() 方法中设置了一个警报管理器,其中包含来自 firebase 数据库的日期并基于该日期设置警报。当我尝试 运行 时,这非常好。但是当我尝试在警报时间结束之前重新启动 phone 时,什么也没有发生。
我还尝试创建一个引用服务的接收器。从这个服务 onHandleIntent(Intent intent),我调用 MainActivity.setAlarm()。此外,我还在 android 清单中添加了权限启动完成。之后,我尝试重新启动我的 phone,当我的 phone 完成启动时,应用程序强制关闭 。我认为它停止是因为我在 activity class 调用了一个方法。
这是我的闹钟接收器
public class AlarmReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
Intent notificationIntent = new Intent(context, BabyListActivity.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
stackBuilder.addParentStack(BabyListActivity.class);
stackBuilder.addNextIntent(notificationIntent);
PendingIntent pendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
Notification notification1 = builder.setContentTitle("1")
.setContentText("New Notification From Demo App..")
.setTicker("New Message Alert!")
.setSmallIcon(R.mipmap.ic_launcher_round)
.setContentIntent(pendingIntent).build();
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, notification1);
}
这是我的 startAlarm() 方法
public static void startAlarm(Context context){
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent notificationIntent = new Intent("android.media.action.DISPLAY_NOTIFICATION");
notificationIntent.addCategory("android.intent.category.DEFAULT");
PendingIntent broadcast = PendingIntent.getBroadcast(context, 100, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
Calendar cal = Calendar.getInstance();
cal.set(Calendar.YEAR, 2017);
cal.set(Calendar.MONTH, 8-1);
cal.set(Calendar.DAY_OF_MONTH, 10);
cal.set(Calendar.HOUR_OF_DAY, 10);
cal.set(Calendar.MINUTE, 15);
cal.set(Calendar.SECOND, 4);
alarmManager.setExact(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), broadcast);
}
这是我的 BootAlarmReceiver
public class BootAlarmReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if ("android.intent.action.BOOT_COMPLETED".equals(intent.getAction())) {
// It is better to reset alarms using Background IntentService
Intent i = new Intent(context, BootService.class);
ComponentName service = context.startService(i);
if (null == service) {
// something really wrong here
Log.e(TAG, "Could not start service ");
}
else {
Log.e(TAG, "Successfully started service ");
}
} else {
Log.e(TAG, "Received unexpected intent " + intent.toString());
}
}
这是我的引导服务
public class BootService extends Service {
public BootService() {
super("BootService");
}
@Override
public void onCreate() {
MainActivity.startAlarm(this);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
return START_NOT_STICKY;
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
}
这是我的android清单
<receiver android:name=".AlarmReceiver">
<intent-filter>
<action android:name="android.media.action.DISPLAY_NOTIFICATION"></action>
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
<service android:name=".BootService"/>
<receiver android:name=".BootAlarmReceiver" android:enabled="true">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
谁能告诉我如何解决这个问题?谢谢...
我认为至少有两个错误。首先,你永远不会new一个Activity class,它应该由android框架管理,如果你只是想调用其中的一些方法,你最好将方法提取到一个utility class 或简单地将方法更改为不推荐的静态方法。其次,onHandleIntent
方法将在后台线程中运行,你不能在主线程中调用应该是运行的方法。
所以在我看来,为了测试,您可以将 MainActivity 中的 startAlarm()
方法设置为静态,并且 BootService 扩展服务而不是 IntentService
,进行这些更改并重试.
像这样更改 setAlarm() 方法:
public static void startAlarm(Context context){
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent notificationIntent = new Intent("android.media.action.DISPLAY_NOTIFICATION");
notificationIntent.addCategory("android.intent.category.DEFAULT");
PendingIntent broadcast = PendingIntent.getBroadcast(context, 100, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
Calendar cal = Calendar.getInstance();
cal.set(Calendar.YEAR, 2017);
cal.set(Calendar.MONTH, 8-1);
cal.set(Calendar.DAY_OF_MONTH, 10);
cal.set(Calendar.HOUR_OF_DAY, 11);
cal.set(Calendar.MINUTE, 40);
cal.set(Calendar.SECOND, 00);
alarmManager.setExact(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), broadcast);
}
像这样更改您的 BootAlarmReceiver:
public class BootService extends Service {
@Override
public void onCreate() {
MainActivity.startAlarm(this);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
return START_NOT_STICKY;
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
}
再试一次!
试试这个..
public class RebootReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if ("android.intent.action.BOOT_COMPLETED".equals(intent.getAction())) {
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Intent notificationIntent = new Intent("android.media.action.DISPLAY_NOTIFICATION");
notificationIntent.addCategory("android.intent.category.DEFAULT");
PendingIntent broadcast = PendingIntent.getBroadcast(context, 100, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
Calendar cal = Calendar.getInstance();
cal.set(Calendar.YEAR, 2017);
cal.set(Calendar.MONTH, 8-1);
cal.set(Calendar.DAY_OF_MONTH, 10);
cal.set(Calendar.HOUR_OF_DAY, 10);
cal.set(Calendar.MINUTE, 15);
cal.set(Calendar.SECOND, 4);
alarmManager.setExact(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), broadcast);
}
}
}
然后在你的清单中..
<receiver android:name=".RebootReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.QUICKBOOT_POWERON" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
我在 MainActivity 的 setAlarm() 方法中设置了一个警报管理器,其中包含来自 firebase 数据库的日期并基于该日期设置警报。当我尝试 运行 时,这非常好。但是当我尝试在警报时间结束之前重新启动 phone 时,什么也没有发生。
我还尝试创建一个引用服务的接收器。从这个服务 onHandleIntent(Intent intent),我调用 MainActivity.setAlarm()。此外,我还在 android 清单中添加了权限启动完成。之后,我尝试重新启动我的 phone,当我的 phone 完成启动时,应用程序强制关闭 。我认为它停止是因为我在 activity class 调用了一个方法。
这是我的闹钟接收器
public class AlarmReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
Intent notificationIntent = new Intent(context, BabyListActivity.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
stackBuilder.addParentStack(BabyListActivity.class);
stackBuilder.addNextIntent(notificationIntent);
PendingIntent pendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
Notification notification1 = builder.setContentTitle("1")
.setContentText("New Notification From Demo App..")
.setTicker("New Message Alert!")
.setSmallIcon(R.mipmap.ic_launcher_round)
.setContentIntent(pendingIntent).build();
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, notification1);
}
这是我的 startAlarm() 方法
public static void startAlarm(Context context){
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent notificationIntent = new Intent("android.media.action.DISPLAY_NOTIFICATION");
notificationIntent.addCategory("android.intent.category.DEFAULT");
PendingIntent broadcast = PendingIntent.getBroadcast(context, 100, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
Calendar cal = Calendar.getInstance();
cal.set(Calendar.YEAR, 2017);
cal.set(Calendar.MONTH, 8-1);
cal.set(Calendar.DAY_OF_MONTH, 10);
cal.set(Calendar.HOUR_OF_DAY, 10);
cal.set(Calendar.MINUTE, 15);
cal.set(Calendar.SECOND, 4);
alarmManager.setExact(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), broadcast);
}
这是我的 BootAlarmReceiver
public class BootAlarmReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if ("android.intent.action.BOOT_COMPLETED".equals(intent.getAction())) {
// It is better to reset alarms using Background IntentService
Intent i = new Intent(context, BootService.class);
ComponentName service = context.startService(i);
if (null == service) {
// something really wrong here
Log.e(TAG, "Could not start service ");
}
else {
Log.e(TAG, "Successfully started service ");
}
} else {
Log.e(TAG, "Received unexpected intent " + intent.toString());
}
}
这是我的引导服务
public class BootService extends Service {
public BootService() {
super("BootService");
}
@Override
public void onCreate() {
MainActivity.startAlarm(this);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
return START_NOT_STICKY;
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
}
这是我的android清单
<receiver android:name=".AlarmReceiver">
<intent-filter>
<action android:name="android.media.action.DISPLAY_NOTIFICATION"></action>
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
<service android:name=".BootService"/>
<receiver android:name=".BootAlarmReceiver" android:enabled="true">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
谁能告诉我如何解决这个问题?谢谢...
我认为至少有两个错误。首先,你永远不会new一个Activity class,它应该由android框架管理,如果你只是想调用其中的一些方法,你最好将方法提取到一个utility class 或简单地将方法更改为不推荐的静态方法。其次,onHandleIntent
方法将在后台线程中运行,你不能在主线程中调用应该是运行的方法。
所以在我看来,为了测试,您可以将 MainActivity 中的 startAlarm()
方法设置为静态,并且 BootService 扩展服务而不是 IntentService
,进行这些更改并重试.
像这样更改 setAlarm() 方法:
public static void startAlarm(Context context){
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent notificationIntent = new Intent("android.media.action.DISPLAY_NOTIFICATION");
notificationIntent.addCategory("android.intent.category.DEFAULT");
PendingIntent broadcast = PendingIntent.getBroadcast(context, 100, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
Calendar cal = Calendar.getInstance();
cal.set(Calendar.YEAR, 2017);
cal.set(Calendar.MONTH, 8-1);
cal.set(Calendar.DAY_OF_MONTH, 10);
cal.set(Calendar.HOUR_OF_DAY, 11);
cal.set(Calendar.MINUTE, 40);
cal.set(Calendar.SECOND, 00);
alarmManager.setExact(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), broadcast);
}
像这样更改您的 BootAlarmReceiver:
public class BootService extends Service {
@Override
public void onCreate() {
MainActivity.startAlarm(this);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
return START_NOT_STICKY;
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
}
再试一次!
试试这个..
public class RebootReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if ("android.intent.action.BOOT_COMPLETED".equals(intent.getAction())) {
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Intent notificationIntent = new Intent("android.media.action.DISPLAY_NOTIFICATION");
notificationIntent.addCategory("android.intent.category.DEFAULT");
PendingIntent broadcast = PendingIntent.getBroadcast(context, 100, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
Calendar cal = Calendar.getInstance();
cal.set(Calendar.YEAR, 2017);
cal.set(Calendar.MONTH, 8-1);
cal.set(Calendar.DAY_OF_MONTH, 10);
cal.set(Calendar.HOUR_OF_DAY, 10);
cal.set(Calendar.MINUTE, 15);
cal.set(Calendar.SECOND, 4);
alarmManager.setExact(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), broadcast);
}
}
}
然后在你的清单中..
<receiver android:name=".RebootReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.QUICKBOOT_POWERON" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>