1

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

   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);
     }

}
}

显然我已经这样做了;

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

为什么它不起作用?

4

3 回答 3

3

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

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);
于 2011-12-08T04:57:24.467 回答
2

我遇到了同样的问题,直到我发现我把我的广播接收器放在不同的包上,而不是一般的包上。

简单地改变了:

<receiver android:name=".AndroidAlarmService" android:enabled="true" >

为了:

<receiver android:name="com.MyCompany.MyPackage.AndroidAlarmService" android:enabled="true" >
于 2013-01-30T21:10:32.410 回答
-1

你试过改变

alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,System.currentTimeMillis(),6000,pendingIntent);

你试过把6000换成别的吗?看起来你的其他一切都是正确的。

编辑:

确保您在清单中具有 Read_phone_state 权限。

于 2011-12-07T12:18:44.720 回答