我希望在销毁活动后每4小时显示一次通知。
这是我的主要活动的代码片段onDestroy()。
@Override
protected void onDestroy() {
Intent notificationIntent = new Intent(this, Notification.class);
PendingIntent contentIntent = PendingIntent.getService(this, 0, notificationIntent,
PendingIntent.FLAG_CANCEL_CURRENT);
AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
am.cancel(contentIntent);
am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, System.currentTimeMillis(), 1000*00*00*04, contentIntent);
}
,这是我的Notification.class代码段:
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.media.RingtoneManager;
import android.os.IBinder;
import android.support.v4.app.NotificationCompat;
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);
}
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
}
我正在研究这么多天,但我无法完成任务。
任何帮助都会得到高度赞赏。
提前致谢!
请帮助!!!
答案 0 :(得分:1)
你在这里乘以0:
am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
System.currentTimeMillis(), 1000*00*00*04, contentIntent);
第三个参数是后续重复警报"之间的间隔(以毫秒为单位),因此您的警报只会被触发一次,因为间隔为零。您需要将第三个参数更改为四个小时,以毫秒为单位转换为3600 * 1000 * 4.
am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
System.currentTimeMillis(), 3600 * 1000 * 4, contentIntent);