Android应用程序中的警报问题
我有一个警报应用程序,应该每 1 分钟打印一条 toast 消息。但是当应用程序第一次运行时,它只执行一次。
这是我的代码:
private class ConnectivityReceiver extends BroadcastReceiver{
MainActivity m= new MainActivity ();
private Timer mTimer;
private TimerTask mTimerTask;
@Override
public void onReceive(Context context, Intent intent) {
NetworkInfo info = intent.getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO);
PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "YOUR TAG");
wl.acquire(); Toast.makeText(context, "Alarm !!!!!!!!!!", Toast.LENGTH_LONG).show(); // Every 1 minute i print the tost message. wl.release();
}
public void SetAlarm(Context context)
{
AlarmManager am=(AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
Intent i = new Intent(context, ConnectivityReceiver.class);
PendingIntent pi = PendingIntent.getBroadcast(context, 0, i, 0);
am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 1000 * 60 * 1, pi); // Millisec * Second * Minute
}
public void CancelAlarm(Context context) {
Intent intent = new Intent(context, ConnectivityReceiver.class);
PendingIntent sender = PendingIntent.getBroadcast(context, 0, intent, 0);
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
alarmManager.cancel(sender);
}
}
i have a alarm application that should print a toast message every 1 minute.But it does that just once when Application is run for the first time.
here my code:
private class ConnectivityReceiver extends BroadcastReceiver{
MainActivity m= new MainActivity ();
private Timer mTimer;
private TimerTask mTimerTask;
@Override
public void onReceive(Context context, Intent intent) {
NetworkInfo info = intent.getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO);
PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "YOUR TAG");
wl.acquire(); Toast.makeText(context, "Alarm !!!!!!!!!!", Toast.LENGTH_LONG).show(); // Every 1 minute i print the tost message. wl.release();
}
public void SetAlarm(Context context)
{
AlarmManager am=(AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
Intent i = new Intent(context, ConnectivityReceiver.class);
PendingIntent pi = PendingIntent.getBroadcast(context, 0, i, 0);
am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 1000 * 60 * 1, pi); // Millisec * Second * Minute
}
public void CancelAlarm(Context context) {
Intent intent = new Intent(context, ConnectivityReceiver.class);
PendingIntent sender = PendingIntent.getBroadcast(context, 0, intent, 0);
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
alarmManager.cancel(sender);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
只需做一件事,
将
PendingIntent sender = PendingIntent.getBroadcast(context, 0, Intent, 0);
替换为
PendingIntent sender = PendingIntent.getBroadcast(context, **someUniqueId** , Intent, 0);
其中 uniqueId 是整数,您可以生成或声明为“i++”,其中 i 是
public static int i;
希望这能起作用
Just do one thing,
in the line
PendingIntent sender = PendingIntent.getBroadcast(context, 0, intent, 0);
replace with
PendingIntent sender = PendingIntent.getBroadcast(context, **someUniqueId**, intent, 0);
where uniqueId is integer either u generate or declare as "i++" where i is
public static int i;
Hope this will work
请将您的这一行更新
为
这将使您的
System.currentTimeMillis();
添加一分钟,并将在该时间之后开始。Please update your this line
to
This will add one minute your
System.currentTimeMillis();
and will start after that time.