单击“通知”停止AlarmManager

时间:2018-05-16 19:08:04

标签: java android notifications alarmmanager

美好的一天,我希望有人能够帮助我!我的申请中有select t.* from t order by row_number() over (partition by connectionId order by fileCreated), fileCreated; AlarmManager。我只想点击通知就想停止播放闹钟。 这是我的代码:

NotificationCompat

NotificationHelper:

public class MyAlarm extends BroadcastReceiver {

private static final int NOTIFICATION_ID = 101;

@Override
public void onReceive(final Context context, Intent intent) {
    NotificationHelper notificationHelper = new NotificationHelper(context);
    MediaPlayer mediaPlayer = MediaPlayer.create(context, Settings.System.DEFAULT_RINGTONE_URI);
    Uri uri = intent.getData();

    Intent newIntent = new Intent(context, AddNoteActivity.class);
    newIntent.setData(uri);
    PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, newIntent, PendingIntent.FLAG_UPDATE_CURRENT);

    NotificationCompat.Builder nb = notificationHelper.getChannel1Notification(pendingIntent);

    notificationHelper.getManager().notify(NOTIFICATION_ID, nb.build());

    mediaPlayer.start();
}
第三节中的

和setAlarm:

public class NotificationHelper extends ContextWrapper {

public static final String chanel1ID = "chanel1ID";
public static final String chanel1Name = "chanel 1";

private NotificationManager notificationManager;

public NotificationHelper(Context base) {
    super(base);
    if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
        createChannels();
    }
}
@RequiresApi(api = Build.VERSION_CODES.O)
public void createChannels(){
    NotificationChannel channel1 = new NotificationChannel(chanel1ID, chanel1Name, NotificationManager.IMPORTANCE_DEFAULT);
    channel1.enableLights(true);
    channel1.enableVibration(true);
    channel1.setLightColor(R.color.colorPrimary);
    channel1.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);

    getManager().createNotificationChannel(channel1);
}

public NotificationManager getManager() {
    if (notificationManager == null) {
        notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    }

    return notificationManager;
}

public NotificationCompat.Builder getChannel1Notification(PendingIntent intent){

    return new NotificationCompat.Builder(getApplicationContext(), chanel1ID)
            .setContentTitle("asda")
            .setContentIntent(intent)
            .setSmallIcon(R.drawable.ic_notifications_black_24dp)
            .setAutoCancel(true);
}

希望有人能在这里帮助我,因为我几乎整天都在寻找解决方案。 谢谢你的时间!

1 个答案:

答案 0 :(得分:0)

NotificationCompat.Builder.setContentIntent

  

提供PendingIntent以在单击通知时发送。

你正在使用

Intent newIntent = new Intent(context, AddNoteActivity.class);

因此,在AddNoteActivity中,您需要获取MediaPlayer的实例并调用stop()。