在我的代码中,警报管理器不工作。我的应用程序的其余部分运行良好。请查看我的代码。
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/