我已经设置了一个警报管理器,它会在应用程序被销毁10秒后显示通知。
但它没有用。
这是我的onDestroy()代码:
@Override
public void onDestroy() {
super.onDestroy();
Intent myIntent = new Intent(this, alarmManager.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, myIntent,0);
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
//alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, 10*1000, 10*1000, pendingIntent);
alarmManager.cancel(pendingIntent);
}
,这里是alarmManager.class:
public class alarmManager extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Intent service1 = new Intent(context, Notification.class);
context.startService(service1);
}
}
,这里是Notification.class:
public class Notification extends Service {
@Override
public void onCreate() {
super.onCreate();
Intent mainIntent = new Intent(this, Main.class);
NotificationManager notificationManager
= (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
android.app.Notification noti = new NotificationCompat.Builder(this)
.setAutoCancel(true)
.setContentIntent(PendingIntent.getActivity(this, 0, mainIntent,
PendingIntent.FLAG_UPDATE_CURRENT))
.setContentTitle("Title")
.setContentText("It's been so Long!!!")
.setSubText("Please return back to App")
.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
.setVibrate(new long[] {1000,1000,1000,1000})
.setSmallIcon(R.drawable.ic_launcher)
.setTicker("Important Notification")
.setWhen(System.currentTimeMillis())
.build();
notificationManager.notify(0, noti);
}
*我很多天都在研究这个问题,但我无法解决这个问题。
任何帮助都非常* 赞赏。
提前致谢。
答案 0 :(得分:1)
要进行一些相对较小的更改,但总体而言,您已获得必要的代码。
Service
。移动代码以创建Notification
到BroadcastReceiver
,并将this
替换为context
。BroadcastReceiver
列在清单中。setRepeating()
作为警报;使用set()
。你只需要点燃一次。alarmManager.cancel(pendingIntent);
这些是让它运作所必需的变化。但是,您也可以考虑从Notification
中删除声音和振动,并将其视为状态消息。最好重命名BroadcastReceiver
课程。也许类似于AlarmReceiver
,以符合Java类命名约定,并避免与AlarmManager
类混淆。