删除闹钟:Android

时间:2017-07-31 09:41:10

标签: android

这是我的addAlarm()方法:

Intent intent = new Intent(this, ALReciver.class);
 PendingIntent pendingIntent = PendingIntent.getBroadcast(this.getApplicationContext(), 10 , intent, PendingIntent.FLAG_UPDATE_CURRENT|  Intent.FILL_IN_DATA);

         AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);

                alarmManager.set(AlarmManager.RTC_WAKEUP, targetCal.getTimeInMillis(),pendingIntent );

此代码工作正常,但我想删除此alaram以下代码:

 Intent intent = new Intent(this, ALReciver.class);
        PendingIntent sender = PendingIntent.getBroadcast(this,10, intent,PendingIntent.FLAG_CANCEL_CURRENT);

        AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);

        alarmManager.cancel(sender);

这段代码不起作用..我不明白我做错了什么?

4 个答案:

答案 0 :(得分:1)

Replce:

PendingIntent sender = PendingIntent.getBroadcast(this,10, intent,PendingIntent.FLAG_CANCEL_CURRENT);

PendingIntent pendingIntent = PendingIntent.getBroadcast(this.getApplicationContext(), 10 , intent, PendingIntent.FLAG_UPDATE_CURRENT|  Intent.FILL_IN_DATA);

答案 1 :(得分:1)

这是我的开始样本,取消

我使用相同的意图。但你的错误是这个。待定意图给予公共按钮点击时间执行操作作为开始和取消使用该意图然后成功。

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_alarm);


        Intent alarmIntent = new Intent(Alaram.this, AlarmReceiver.class);
        pendingIntent = PendingIntent.getBroadcast(MyActivity.this, 0, alarmIntent, 0);

        findViewById(R.id.btn_startAlarm).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                start();
            }
        });

        findViewById(R.id.btn_stopAlarm).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                cancel();
            }
        });


    }

    public void start() {
        AlarmManager manager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
        int interval = 8000;

        manager.setInexactRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), interval, pendingIntent);
        Toast.makeText(this, "Alarm Set", Toast.LENGTH_SHORT).show();
    }

    public void cancel() {
        AlarmManager manager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
        manager.cancel(pendingIntent);
        Toast.makeText(this, "Alarm Canceled", Toast.LENGTH_SHORT).show();
    }

}

对我来说工作正常是你试试这个。

答案 2 :(得分:0)

将标记从PendingIntent.FLAG_CANCEL_CURRENT更改为PendingIntent.FLAG_UPDATE_CURRENT

答案 3 :(得分:0)

您可以取消闹钟。为此,您需要创建相同的意图:

alarmMgr.cancel(alarmIntent);