我需要在 10 秒后启动 Activity AlarmReceiver
(例如)。我需要在不运行应用程序的情况下激活它。但是无论应用程序是否运行,AlarmReceiver
都不会被调用。有什么建议吗?
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);
}
}
这是我设法开始工作的最终代码。你需要添加
<receiver android:process=":remote" android:name="AlarmReceiver"></receiver>
就在 </application>
上方 list 文件中的标记。
这将设置在调用方法 SetAlarm()
后 30 秒内触发警报
关于android - 警报管理器不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10684077/