Android 如何在每4小时后显示通知?

Android 如何在每4小时后显示通知?,android,service,notifications,Android,Service,Notifications,我希望在活动销毁后每4小时显示一次通知 这是我的主要活动onDestroy()的代码片段。 @Override protected void onDestroy() { Intent notificationIntent = new Intent(this, Notification.class); PendingIntent contentIntent = PendingIntent.getService(this, 0, notificationIntent,

我希望在活动销毁后每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);
}
这是我的通知。类代码片段:

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:

        am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, 
           System.currentTimeMillis(), 1000*00*00*04, contentIntent);
第三个参数是“报警的后续重复之间的间隔(以毫秒为单位)”,因此您的报警将只触发一次,因为间隔是零。您需要将第三个参数更改为4小时,以毫秒为单位转换为3600*1000*4

am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, 
           System.currentTimeMillis(), 3600 * 1000 * 4, contentIntent);

谢谢,如果我想每10秒显示一次通知,那么将3600*1000*4更改为10*1000,也就是10000毫秒;这不是真的!!我销毁了应用程序,然后等待了10秒,但没有收到通知!!!现在该怎么办???一步一步地遵循本教程,看看你是否能让他们的代码正常工作:很抱歉,兄弟,但它仍然不起作用!我无法理解这里的问题!