我正在构建一个通知应用程序,该应用程序使用警报管理器设置多个通知,以提醒用户日常任务,当手机处于开机状态时一切正常,但是当重新启动手机时,会显示通知,但通知数据会显示丢失(例如通知内容),这些数据从我的第一次活动中保存到了意图中 这是下面的代码。 (我已添加权限并对清单进行了操作)请我帮忙
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());
}
}
答案 0 :(得分:0)
默认情况下,设备关闭时所有警报将被取消。至 防止这种情况的发生,您可以将应用程序设计为 如果用户重新启动,则自动重新启动重复警报 设备。
因此您可以添加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.
}
}
}