重启手机后报警通知数据消失
Posted
技术标签:
【中文标题】重启手机后报警通知数据消失【英文标题】:Alarm Notification Data disappear after rebooting phone 【发布时间】:2019-04-21 07:02:57 【问题描述】:我正在构建一个通知应用程序,它使用警报管理器设置多个通知以提醒用户日常任务,当手机开机时一切都很好,但是当重启手机时,通知正在显示,但通知数据是丢失(例如通知内容)这些数据从我的第一个活动中保存到意图中 这是下面的代码。 (我已向我的清单添加权限并接收操作)请我需要一些帮助
public class add_rreminde extends AppCompatActivity implements View.OnClickListener
private int notificationId=1;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_rreminde);
//buton ok
findViewById(R.id.but).setOnClickListener(this);
findViewById(R.id.cancel).setOnClickListener(this);
@Override
public void onClick(View view)
EditText remide=findViewById(R.id.edit);
DatePicker date=findViewById(R.id.date);
Intent intent=new Intent(add_rreminde.this,AlarmReciver.class);
intent.putExtra("notificationId",notificationId);
intent.putExtra("todo",remide.getText().toString());
final int _id=(int)System.currentTimeMillis();
PendingIntent alarmIntent=PendingIntent.getBroadcast(add_rreminde.this,_id,intent,PendingIntent.FLAG_ONE_SHOT);
AlarmManager alarm=(AlarmManager)getSystemService(ALARM_SERVICE);
switch (view.getId())
case(R.id.but):
int day=date.getDayOfMonth();
int month=date.getMonth();
int year=date.getYear();
//create date
Calendar startDate=Calendar.getInstance();
startDate.set(Calendar.DAY_OF_MONTH,day);
startDate.set(Calendar.MONTH,month);
startDate.set(Calendar.YEAR,year);
Long alarmStartDate=startDate.getTimeInMillis();
//setAlarme
alarm.set(AlarmManager.RTC_WAKEUP,alarmStartDate,alarmIntent);
Toast.makeText(getApplicationContext(),"done",Toast.LENGTH_LONG).show();
break;
case (R.id.cancel):
alarm.cancel(alarmIntent);
Toast.makeText(getApplicationContext(),"canceled",Toast.LENGTH_LONG).show();
break;
我的推送通知接收者
public class AlarmReciver extends BroadcastReceiver
@Override
public void onReceive(Context context, Intent intent)
//get id from notification id intent
int notification =intent.getIntExtra("notificationId",0);
String getMessage=intent.getStringExtra("todo");
//when notification is opened open mainActivity
Intent mainIntent=new Intent(context,add_rreminde.class);
PendingIntent contentIntent=PendingIntent.getActivity(context,0,mainIntent,0);
NotificationManager mynotificationManager=(NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
//prepare notification
Notification.Builder builder=new Notification.Builder(context);
builder.setSmallIcon(R.drawable.assistant)
.setContentTitle("changer oil")
.setContentText(getMessage)
.setWhen(System.currentTimeMillis())
.setAutoCancel(true)
.setContentIntent(contentIntent)
.setPriority(Notification.PRIORITY_MAX)
.setDefaults(Notification.DEFAULT_ALL);
//notify
mynotificationManager.notify(notification,builder.build());
【问题讨论】:
【参考方案1】:默认情况下,设备关闭时会取消所有警报。到 防止这种情况发生,您可以将应用程序设计为 如果用户重新启动,则自动重新启动重复警报 设备。
这样您就可以添加BroadcastReceiver
来接收ACTION_BOOT_COMPLETED
首先:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
和:
<receiver android:name=".SampleBootReceiver"
android:enabled="true">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"></action>
</intent-filter>
</receiver>
现在您在设备重启时再次设置警报:
public class SampleBootReceiver extends BroadcastReceiver
@Override
public void onReceive(Context context, Intent intent)
if (intent.getAction().equals("android.intent.action.BOOT_COMPLETED"))
// Set the alarm here.
【讨论】:
您好,先生,总是收到通知但数据丢失仍然是同样的问题。在我的接收器中,我确实在 if 条件下设置了警报,但没有任何改变以上是关于重启手机后报警通知数据消失的主要内容,如果未能解决你的问题,请参考以下文章