0

我需要一个关于安卓系统推送通知的教程。我不需要 GCM 的实现,我只需要应用程序的警报管理器调用的推送通知。

这可能吗?

4

1 回答 1

3

另请参阅schedule-repeatable-android-notification-in-different

要使用 AlaramManager,首先我们需要在 Manifest 文件中将 BroadcastReceiver 声明为,

接收者 android:name=".TimeAlarm" />

public class CustomAlarm extends Activity {

     AlarmManager am;

     @Override
     public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.main);
      am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
      setOneTimeAlarm();
     }

     public void setOneTimeAlarm() {
      Intent intent = new Intent(this, TimeAlarm.class);
      PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0,
        intent, PendingIntent.FLAG_ONE_SHOT);
      am.set(AlarmManager.RTC_WAKEUP,
        System.currentTimeMillis() + (5 * 1000), pendingIntent);
     }

     public void setRepeatingAlarm() {
      Intent intent = new Intent(this, TimeAlarm.class);
      PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0,
        intent, PendingIntent.FLAG_CANCEL_CURRENT);
      am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(),
        (5 * 1000), pendingIntent);
     }

    }


    public class TimeAlarm extends BroadcastReceiver {

     NotificationManager nm;

     @Override
     public void onReceive(Context context, Intent intent) {
      nm = (NotificationManager) context
        .getSystemService(Context.NOTIFICATION_SERVICE);
      CharSequence from = "Nithin";
      CharSequence message = "Crazy About Android...";
      PendingIntent contentIntent = PendingIntent.getActivity(context, 0,
        new Intent(), 0);
      Notification notif = new Notification(R.drawable.icon,
        "Crazy About Android...", System.currentTimeMillis());
      notif.setLatestEventInfo(context, from, message, contentIntent);
      nm.notify(1, notif);
     }
    }
于 2012-11-18T22:36:38.037 回答