如何在android中停止闹钟 [英] How to stop an alarm in android

查看:82
本文介绍了如何在android中停止闹钟的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的主要活动中,其中有一个按钮.在它的 onclick 侦听器中,我正在调用函数来设置闹钟.闹钟正在工作,但我无法阻止它.有人可以帮我吗

In my main activity which has a button in it. In its onclick listener im calling function to set alarm.The alarm is working but iam not able to stop it .Can some one help me

MainActivity.java

MainActivity.java

public class MainActivity extends Activity {

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

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


        });
    }

    private void setalarm() {

        Calendar cal=Calendar.getInstance();
        // cal.set(Calendar.MONTH,6);
        // cal.set(Calendar.YEAR,2013);
        // cal.set(Calendar.DAY_OF_MONTH,12);
        cal.set(Calendar.HOUR_OF_DAY,18);
        cal.set(Calendar.MINUTE,32);
        Intent intent = new Intent(this, Mote.class);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(this.getApplicationContext(), 1253, intent, PendingIntent.FLAG_UPDATE_CURRENT|  Intent.FILL_IN_DATA);

        AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);

        alarmManager.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(),pendingIntent );
        alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);
        Toast.makeText(this, "Alarm SET.", Toast.LENGTH_LONG).show();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
}

Mote.java:

public class Mote  extends BroadcastReceiver{

    public void onReceive(Context context, Intent intent) {
        Toast.makeText(context, "Alarm worked.", Toast.LENGTH_LONG).show();
        int icon = R.drawable.ic_launcher;
        CharSequence tickerText = "Hello you have to take medicine I am Nitin Sharma";
        long when = System.currentTimeMillis();

        CharSequence contentTitle = "My notification";
        CharSequence contentText = "Hello World!";

        final int NOTIF_ID = 1234;
        NotificationManager notofManager = (NotificationManager)context. getSystemService(Context.NOTIFICATION_SERVICE);
        Intent notificationIntent = new Intent(context, Alset.class);
        PendingIntent contentIntent = PendingIntent.getActivity(context,0, notificationIntent, 0);
        Notification notification = new Notification(icon, tickerText,when );
        notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
        notification.flags = Notification.FLAG_INSISTENT;
        notification.defaults |= Notification.DEFAULT_SOUND;
        notofManager.notify(NOTIF_ID,notification);

        Toast.makeText(context, "Alarm worked.", Toast.LENGTH_LONG).show();

        Intent i = new Intent(context,Alset.class);
        i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(i);

Alset.java:

Alset.java:

public class Alset extends Activity{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activitystop); 
        Log.e("IM here ","Im here");

        findViewById(R.id.button1).setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                Toast.makeText(Alset.this, "Stop the alrm now", Toast.LENGTH_LONG).show();
            }
        });
    }   
}

一旦警报开始,我将在接收者的 OnReceive() 方法中获得回调.从那里我要去活动 Alset,在那里我保留了一个停止按钮.我如何从这里停止警报?

Once the alarm starts I will get the callback in the receiver's OnReceive() method. From there I'm going to the activity Alset where I kept a stop button. How do I to stop the alarm from here?

注意: - 我正在硬编码设置闹钟的时间.

NOTE: - I'm hardcoding the time to set the alarm.

推荐答案

你可以这样取消闹钟:

     Intent intent = new Intent(this, Mote.class);
     PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 1253, intent, 0);
     AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
     alarmManager.cancel(pendingIntent);

此外,您应该在设置闹钟的代码中从对 getBroadcast() 的调用中删除 Intent.FILL_IN_DATA.

Also, you should remove Intent.FILL_IN_DATA from your call to getBroadcast() in the code where you set the alarm.

这篇关于如何在android中停止闹钟的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆