在android

时间:2015-11-27 22:53:32

标签: android notifications

我正在尝试在android中创建定期通知,而我几乎已经实现了它,问题是我的代码打开应用程序而不是创建通知,这是我的代码:

NotificationCompat.Builder mBuilder =
           new NotificationCompat.Builder(this)
                   .setSmallIcon(R.mipmap.ic_launcher)
                   .setContentTitle(this.getText(R.string.notificationMessage))
                   .setContentText("")
                   .setAutoCancel(true);

    Intent resultIntent = new Intent(this, MainActivity.class);

    // Because clicking the notification opens a new ("special") activity, there's
    // no need to create an artificial back stack.
    PendingIntent resultPendingIntent =
           PendingIntent.getActivity(
                   this,
                   0,
                   resultIntent,
                   PendingIntent.FLAG_UPDATE_CURRENT
           );
    mBuilder.setContentIntent(resultPendingIntent);

    NotificationManager mNotifyMgr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    mNotifyMgr.notify(1,mBuilder.build());


    AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);

    Calendar calendar = Calendar.getInstance();
    calendar.set(Calendar.HOUR_OF_DAY, 07);
    calendar.set(Calendar.MINUTE, 00);
    calendar.set(Calendar.SECOND, 00);

    alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 24 * 60 * 60 * 1000, resultPendingIntent);  //set repeating every 24 hours

更新代码:

类主要活动:

PendingIntent servicePendingIntent = PendingIntent.getService(this, 0, new Intent(this, NotificationService.class), PendingIntent.FLAG_CANCEL_CURRENT );

    AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);

    Calendar calendar = Calendar.getInstance();
    calendar.set(Calendar.HOUR_OF_DAY, 15);
    calendar.set(Calendar.MINUTE, 20);
    calendar.set(Calendar.SECOND, 00);

    alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 24 * 60 * 60 * 1000, servicePendingIntent);  //set repeating every 24 hours

类NotificationService:

public class NotificationService extends IntentService {

/**
 * Creates an IntentService.  Invoked by your subclass's constructor.
 *
 * @param name Used to name the worker thread, important only for debugging.
 */
public NotificationService(String name) {
    super(name);
}

@Override
protected void onHandleIntent(Intent intent) {

    Log.d("NotificationService", "onHandleIntent");

    NotificationCompat.Builder mBuilder =
            new NotificationCompat.Builder(this)
                    .setSmallIcon(R.mipmap.ic_launcher)
                    .setContentTitle(this.getText(R.string.notificationMessage))
                    .setContentText("")
                    .setAutoCancel(true);

    Intent resultIntent = new Intent(this, MainActivity.class);

    // Because clicking the notification opens a new ("special") activity, there's
    // no need to create an artificial back stack.
    PendingIntent resultPendingIntent =
            PendingIntent.getActivity(
                    this,
                    0,
                    resultIntent,
                    PendingIntent.FLAG_UPDATE_CURRENT
            );
    mBuilder.setContentIntent(resultPendingIntent);

    NotificationManager mNotifyMgr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    mNotifyMgr.notify(1,mBuilder.build());



}

}

2 个答案:

答案 0 :(得分:1)

您正在将活动的待处理意图传递给警报管理器。您需要将通知代码移动到服务(我建议使用IntentService)。

然后,您可能需要创建一个运行该服务的待处理意图,并将该待处理意图传递给警报管理器。这样,每次闹钟响起时,都会运行通知代码。

PendingIntent servicePendingIntent = PendingIntent.getService(this, 0, new Intent(this, NotificationService.class));
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 24 * 60 * 60 * 1000, servicePendingIntent);

答案 1 :(得分:0)

您的<Route path="test" component={RouteList} /> <Route path="test/22" component={StopList}/> 指向您的活动。您只需使用PendingIntent代替,并让此服务显示您的通知。