取消报警管理器后,再次设置它不起作用(没有通知)

时间:2017-05-09 15:19:26

标签: java android notifications broadcastreceiver

我试图让我的应用在特定时间发出通知(使用timePicker)并每天重复(使用setRepeat方法)。

一切正常,直到我取消闹钟以检查取消是否有效。但是,如果我再次设置闹钟以取消闹钟以取消闹钟,除非我的应用程序正在运行,否则它无法正常工作。

我已将活动转换为对话框并从startActivityForResult开始,在闹钟管理器使用的日历上设置时间。提前谢谢。

NotificationCenter.class(活动兼对话框的java文件):

let v = 123;

MainActivity.xml

        // IMPLEMENTATION OF TIME PICKER'S TIME CHANGE EVENT
    timePicker.setOnTimeChangedListener(new TimePicker.OnTimeChangedListener() {
        @Override
        public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
            savedHour = hourOfDay;
            savedMinute = minute;

        }
    });

    // IMPLEMENTATION OF SAVE BUTTON
    save.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //using this for couple of reasones
            Boolean isNotification = notificationSwitch.isChecked();
            editor.putBoolean("isNotification", isNotification);
            editor.putInt("saved_hour", savedHour);
            editor.putInt("saved_minute", savedMinute);
            editor.putBoolean("hasTimeSaved", true);
            editor.apply();
            //sending this intent to mainActivity as a result
            Intent result = new Intent();
            result.putExtra("saved_hour", savedHour);
            result.putExtra("saved_minute", savedMinute);
            setResult(2, result);
            finish();
        }
    });

MyBroadcastReceiver:

      @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // To remove status bar
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN);

    setContentView(R.layout.activity_main2);

    // ALARM SERVICE FOR NOTIFICATION
    alarmService = (AlarmManager) getSystemService(ALARM_SERVICE);
    calendar = Calendar.getInstance();
    preferences = PreferenceManager.getDefaultSharedPreferences(this);
    alertIntent = new Intent(this, MyBroadcastReceiver.class);
    pendingIntent = PendingIntent.getBroadcast(this, 1, alertIntent, PendingIntent.FLAG_UPDATE_CURRENT);
// GET THE NOTIFICATION LINEAR LAYOUT AND SET AN ON CLICK LISTENER TO IT
    LinearLayout notificationManager = (LinearLayout) findViewById(R.id.notification_manager);
    notificationManager.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            startActivityForResult(new Intent(getApplicationContext(), NotificationCenter.class), 11);
        }
    });
    @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == 11) {
        Toast.makeText(this,"request_code_ok", Toast.LENGTH_SHORT).show();
        if (resultCode == 2) {
            Toast.makeText(this,"result_code_ok", Toast.LENGTH_SHORT).show();
            if (preferences.getBoolean("isNotification", false)) {
                Toast.makeText(this, "true", Toast.LENGTH_SHORT).show();
            int savedHour = data.getIntExtra("saved_hour", 0);
            int savedMinute = data.getIntExtra("saved_minute", 0);
            calendar.set(Calendar.HOUR_OF_DAY, savedHour);
            calendar.set(Calendar.MINUTE, savedMinute);
            calendar.set(Calendar.SECOND, 1);

            Intent alertIntent = new Intent(this, MyBroadcastReceiver.class);
            pendingIntent = PendingIntent.getBroadcast(this, 1, alertIntent, PendingIntent.FLAG_UPDATE_CURRENT);
            alarmService.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_FIFTEEN_MINUTES, pendingIntent);
        } else {
                alarmService.cancel(pendingIntent);
                Toast.makeText(this,"alarm_cancelled", Toast.LENGTH_SHORT).show();
            }
        }
    }
}

}

的AndroidManifest.xml

public class MyBroadcastReceiver extends BroadcastReceiver{

@Override
public void onReceive(Context context, Intent intent) {
    // NOTIFICATION
    NotificationCompat.Builder mBuilder =
            new NotificationCompat.Builder(this)
                    .setTicker("Alert!")
                    .setSmallIcon(R.drawable.notification_icon)
                    .setContentTitle("My notification")
                    .setContentText("Hello World!");
    Intent resultIntent = new Intent(this, ChapterOne.class);
    TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
    stackBuilder.addParentStack(ChapterOne.class);
    stackBuilder.addNextIntent(resultIntent);
    PendingIntent resultPendingIntent =
            stackBuilder.getPendingIntent(
                    0,
                    PendingIntent.FLAG_UPDATE_CURRENT
            );
    mBuilder.setContentIntent(resultPendingIntent);
    mBuilder.setDefaults(NotificationCompat.DEFAULT_SOUND);
    mBuilder.setPriority(2);
    mBuilder.setAutoCancel(true);
    NotificationManager mNotificationManager =
            (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
    mNotificationManager.notify( 0,mBuilder.build());
}

0 个答案:

没有答案