机器人 :-Alarm manager not working

标签 android android-alarms

在我的代码中,警报管理器不工作。我的应用程序的其余部分运行良好。请查看我的代码。

   Intent myIntent = new Intent(getApplicationContext(), AndroidAlarmService.class);
   myIntent.putExtra("class", "home");
   PendingIntent pendingIntent = PendingIntent.getService(this, 0,myIntent, 0);
   AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
   alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,System.currentTimeMillis(),6000,pendingIntent);

和我的 android AlarmService 类:-

public class AndroidAlarmService extends BroadcastReceiver implements URLs{
 public void onReceive(Context context, Intent intent) {

    // TODO Auto-generated method stub
     System.out.println("BroadCast\n");
     String name=intent.getStringExtra("class");
     if(name.equals("home")){

    Intent homeIn=new Intent(context,Home.class);
    context.startActivity(homeIn);
     }

}
}

在 list 中我已经这样做了;

 <receiver android:name=".AndroidAlarmService" android:enabled="true" >
      <intent-filter>
          <action android:name="android.intent.action.PHONE_STATE"></action>
      </intent-filter>
 </receiver>

为什么它不起作用??

最佳答案

我得到了答案。我做了以下更改:

Intent myIntent = new Intent(getApplicationContext(), AndroidAlarmService.class);
myIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0,myIntent, 0);

在我的 AndroidAlarmService 类中:

Intent homeIn=new Intent(context,Home.class);
homeIn.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(homeIn);

关于机器人 :-Alarm manager not working,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8415142/

相关文章:

Android:将带有剪辑的图片绘制到 ScrollView 中会导致一致的 ANR?

java - Android:抽屉导航垂直阴影

android 在创建媒体播放器时出错?

android - 如何在特定时间在 Android 应用程序中设置通知?

android - 预定的警报未正确触发

android - 获取之前添加到AlarmManager中的闹钟时间

java - 如何在没有用户交互的情况下以编程方式卸载 Android 应用程序?

c# - 在 Android Xamarin.forms 上的整个应用程序中保持沉浸式模式

android - Firebase 发出太多警报

android - 如何检查 AlarmManager 只重复 5 次