每 5 分钟通知一次

use*_*410 5 android android-alarms

我想每 5 分钟向用户发送一次通知,我正在使用以下代码。它第一次向我显示通知,但下次不给。

public void startAlarm() {
    AlarmManager alarmManager = (AlarmManager) this.getSystemService(this.ALARM_SERVICE);
    long whenFirst = System.currentTimeMillis();         // notification time
    Intent intent = new Intent(this, NotifyUser.class);
    PendingIntent pendingIntent = PendingIntent.getService(this, 0, intent, 0);
    alarmManager.setRepeating(AlarmManager.RTC, whenFirst, 60*5000, pendingIntent);            
}

public class NotifyUser extends Service {   
    @Override
    public IBinder onBind(Intent intent) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        loadNotification(); 
    }

    private void loadNotification() {
        NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        Notification notify = new Notification(R.drawable.ic_launcher/*android.R.drawable.stat_notify_more*/, "Hanumanji waiting for you", System.currentTimeMillis());
        Context context = NotifyUser.this;
        CharSequence title = "Hanumanji is waiting for you";
        CharSequence details = "Do Hanuman Chalisa Parayan with ShlokApp.";
        Intent intent = new Intent(context, NotifyUser.class);
        PendingIntent pending = PendingIntent.getActivity(context, 0, intent, 0);
        notify.setLatestEventInfo(context, title, details, pending);
        notify.sound = Uri.parse("android.resource://pro.shlokapp.hanumanchalisa/"+ R.raw.game_sound_pause);
        nm.notify(0, notify);
    }

    public int onStartCommand(Intent intent, int flags, int startId) {
        return 1;
    }

    public void onStart(Intent intent, int startId) {
        // TO DO
    }

    public IBinder onUnBind(Intent arg0) {
        // TO DO Auto-generated method
        return null;
    }

    public void onStop() {}

    public void onPause() {}

    @Override
    public void onDestroy() {}

    @Override
    public void onLowMemory() {}
}
Run Code Online (Sandbox Code Playgroud)

Sid*_*yas 5

它第一次向我显示通知,但下次不给。: 原因是你使用nm.notify(0, notify); 不要使用 0 因为它会显示最新的通知。

下面的代码就像一个魅力:

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    MyTimerTask myTask = new MyTimerTask();
    Timer myTimer = new Timer();

    myTimer.schedule(myTask, 5000, 1500);

}

class MyTimerTask extends TimerTask {
    public void run() {

        generateNotification(getApplicationContext(), "Hello");
    }
}

private void generateNotification(Context context, String message) {

    int icon = R.drawable.ic_launcher;
    long when = System.currentTimeMillis();
    String appname = context.getResources().getString(R.string.app_name);
    NotificationManager notificationManager = (NotificationManager) context
            .getSystemService(Context.NOTIFICATION_SERVICE);
    int currentapiVersion = android.os.Build.VERSION.SDK_INT;
    Notification notification;
    PendingIntent contentIntent = PendingIntent.getActivity(context, 0,
            new Intent(context, MainActivity.class), 0);

    // To support 2.3 os, we use "Notification" class and 3.0+ os will use
    // "NotificationCompat.Builder" class.
    if (currentapiVersion < android.os.Build.VERSION_CODES.HONEYCOMB) {
        notification = new Notification(icon, message, 0);
        notification.setLatestEventInfo(context, appname, message,
                contentIntent);
        notification.flags = Notification.FLAG_AUTO_CANCEL;
        notificationManager.notify((int) when, notification);

    } else {
        NotificationCompat.Builder builder = new NotificationCompat.Builder(
                context);
        notification = builder.setContentIntent(contentIntent)
                .setSmallIcon(icon).setTicker(appname).setWhen(0)
                .setAutoCancel(true).setContentTitle(appname)
                .setContentText(message).build();

        notificationManager.notify((int) when, notification);

    }

}
Run Code Online (Sandbox Code Playgroud)

}

使用定时器类。根据您的需要更改计时器间隔。希望这可以帮助。


Tim*_*Tim 1

在这篇文章如何准确使用Notification.Builder中有一个例子。我用它在我的应用程序中发出通知。它还使用支持库中的NotificationBuilder。

我认为在上面的代码中,您只是更新通知,该通知已经存在。尝试通过每次设置/更新新通知时显示一个加一的数字来进行检查。

希望这会对您有所帮助=)。