AlarmManager不工作 [英] AlarmManager not working
本文介绍了AlarmManager不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要10个秒(例如)后开始活动 AlarmReceiver
。我需要它不运行应用程序激活。但无论应用程序运行与否的 AlarmReceiver
不会被调用。有什么建议?
意向意图=新的意图(这一点,AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(这一点,111,意图,0);
AlarmManager alarmManager =(AlarmManager)getSystemService(ALARM_SERVICE);
//alarmManager.set(AlarmManager.RTC_WAKEUP,System.currentTimeMillis的()
// +(10 * 1000),pendingIntent);
Toast.makeText(这一点,报警设置,Toast.LENGTH_LONG).show();
解决方案
公共类AlarmReceiver扩展的BroadcastReceiver {
@覆盖
公共无效的onReceive(上下文的背景下,意图意图){
字符串消息=Hellooo,ALRM工作----;
Toast.makeText(背景下,消息,Toast.LENGTH_SHORT).show();
意图intent2 =新的意图(背景下,TripNotification.class);
intent2.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent2);
}
公共无效setAlarm(上下文的背景下){
Log.d(碳,ALRM SET!);
//获取当前时间日历对象
日历CAL = Calendar.getInstance();
//加30秒到日历对象
cal.add(Calendar.SECOND,30);
意向意图=新的意图(背景下,AlarmReceiver.class);
PendingIntent发送= PendingIntent.getBroadcast(背景下,192837,意向,PendingIntent.FLAG_UPDATE_CURRENT);
//获取AlarmManager服务
AlarmManager AM =(AlarmManager)context.getSystemService(context.ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP,cal.getTimeInMillis(),发送方);
}
}
这是最后的codeI设法得到工作。您需要添加
<接收器安卓程序=:遥控机器人:NAME =AlarmReceiver>< /接收器>
略高于< /应用>以清单文件
标记
这将设置一个报警在30秒内触发调用该方法后, SetAlarm()
I need to start the activity AlarmReceiver
after 10 seconds (for example). I need it to be activated without running the app. But whether the app runs or not the AlarmReceiver
do not get called. Any suggestions?
Intent intent = new Intent(this, AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 111, intent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
//alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()
//+ (10 * 1000), pendingIntent);
Toast.makeText(this, "Alarm set", Toast.LENGTH_LONG).show();
解决方案
public class AlarmReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
String message = "Hellooo, alrm worked ----";
Toast.makeText(context, message, Toast.LENGTH_SHORT).show();
Intent intent2 = new Intent(context, TripNotification.class);
intent2.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent2);
}
public void setAlarm(Context context){
Log.d("Carbon","Alrm SET !!");
// get a Calendar object with current time
Calendar cal = Calendar.getInstance();
// add 30 seconds to the calendar object
cal.add(Calendar.SECOND, 30);
Intent intent = new Intent(context, AlarmReceiver.class);
PendingIntent sender = PendingIntent.getBroadcast(context, 192837, intent, PendingIntent.FLAG_UPDATE_CURRENT);
// Get the AlarmManager service
AlarmManager am = (AlarmManager) context.getSystemService(context.ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), sender);
}
}
This is the final code i managed to get working. You need to add
<receiver android:process=":remote" android:name="AlarmReceiver"></receiver>
just above the </application>
tag in Manifest file.
This will set an alarm to trigger in 30 seconds after calling the method SetAlarm()
这篇关于AlarmManager不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文