如何在android中停止闹钟
Posted
技术标签:
【中文标题】如何在android中停止闹钟【英文标题】:How to stop an alarm in android 【发布时间】:2013-07-11 01:25:36 【问题描述】:在我的主要活动中,其中有一个按钮。在其 onclick 侦听器中,我正在调用函数来设置警报。警报正在工作,但我无法停止它。有人可以帮我吗
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:
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,在那里我保留了一个停止按钮。如何从这里停止闹钟?
注意:- 我正在硬编码设置闹钟的时间。
【问题讨论】:
您需要从报警管理器中取消报警尝试==== AlarmManager alarmManager = (AlarmManager) _cntx.getSystemService(Context.ALARM_SERVICE); PendingIntent pendingIntent = PendingIntent.getBroadcast(this.getApplicationContext(), 1253, 意图, PendingIntent.FLAG_UPDATE_CURRENT| Intent.FILL_IN_DATA); alarmManager.cancel(pendingIntent); 【参考方案1】:你可以这样取消闹钟:
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
。
【讨论】:
@droid_dev 如果您遇到问题,请打开一个新问题并发布您的代码并解释什么不起作用。社区可以帮助您。 alarmManager.cancel(pendingIntent) 只会取消来自alarmManager的警报。它不会杀死被解雇的活动。我还提出了知道如何杀死被解雇的活动的问题。参考***.com/questions/29735209/…如果有人知道答案请回复 我最初的意图添加了一个动作,所以为了让它工作,我添加了相同的动作到意图中。 @Favolas 是的,当然。您需要创建一个匹配的Intent
,因此 ACTION、CATEGORY、DATA 和 COMPONENT 信息必须全部匹配。
@DavidWasser 嘿伙计,你能帮我解决我 2-3 天以来一直遇到的问题吗,这是我的问题***.com/questions/43950978/…【参考方案2】:
你需要从警报管理器取消警报试试这个,
Intent intent = new Intent(this, Mote.class);
AlarmManager alarmManager = (AlarmManager) _cntx.getSystemService(Context.ALARM_SERVICE);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this.getApplicationContext(), 1253, intent, PendingIntent.FLAG_UPDATE_CURRENT| Intent.FILL_IN_DATA);
alarmManager.cancel(pendingIntent);
【讨论】:
你能帮我解决这个问题吗***.com/questions/51542099/…@user1140237以上是关于如何在android中停止闹钟的主要内容,如果未能解决你的问题,请参考以下文章