android通知按钮动作

时间:2015-08-23 06:37:03

标签: android notifications android-notifications

我已经创建了一个通知,其中包含一个用于暂停的操作按钮。通知工作正常。我把它设定了一段时间。我不明白的是如何收到点击贪睡按钮的事件以及如何对其采取行动。

这是我的收件人

public class NotificationReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
   //I tried receiving action but it is always null
    String action = intent.getAction();
   //Some random id to obtain the time for notification in the service from the database
    int SessionId = intent.getIntExtra(IntentStrings.SESSION, 0);
    Intent service1 = new Intent(context, BookmarkAlarmService.class);
    service1.putExtra(IntentStrings.SESSION, SessionId);
    context.startService(service1);

}

这是我的服务

public class BookmarkAlarmService extends Service {

private NotificationManager mManager;

@Override
public IBinder onBind(Intent arg0) {
    return null;
}

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

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    handleStart(intent, startId);
    return START_NOT_STICKY;


}

@Override
public void onDestroy() {
    super.onDestroy();
}


void handleStart(Intent intent, int startId) {
    mManager = (NotificationManager) this.getApplicationContext().getSystemService(NOTIFICATION_SERVICE);
    //get time from database
    int id = intent.getIntExtra(IntentStrings.SESSION, 0);
    DbSingleton dbSingleton = DbSingleton.getInstance();
    Session session = dbSingleton.getSessionById(id);
    //Intent
    Intent intent1 = new Intent(this.getApplicationContext(), SessionDetailActivity.class);
    intent1.putExtra(IntentStrings.SESSION, session.getTitle());
    PendingIntent pendingNotificationIntent = PendingIntent.getActivity(this.getApplicationContext(), 0, intent1, PendingIntent.FLAG_UPDATE_CURRENT);

    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
            .setSmallIcon(R.mipmap.ic_launcher)
            .setContentTitle("session about to start")
            .setContentText(session.getTitle())
            .setAutoCancel(true)
            .setContentIntent(pendingNotificationIntent)
            .addAction(R.drawable.ic_alarm_black_24dp,"Snooze",pendingNotificationIntent);
    intent1.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);

    mBuilder.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION));
    mManager.notify(1, mBuilder.build());
}

这就是我在活动中创建通知的方式

public void createNotification() {
    //obtain calendar object with some time 
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(someTime);

    Intent myIntent = new Intent(this, NotificationReceiver.class);
    myIntent.putExtra(IntentStrings.SESSION, session.getId());
    PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, myIntent, 0);

    AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
    alarmManager.set(AlarmManager.RTC, calendar.getTimeInMillis(), pendingIntent);
}

我在Manifest中添加了这些行

 <service
        android:name=".Services.BookmarkAlarmService"
        android:enabled="true" />

    <receiver android:name=".Receivers.NotificationReceiver" />

现在如何检测贪睡操作按钮?

0 个答案:

没有答案