2011-12-07 208 views
1

在我的代码报警管理器不工作。我的应用程序的恢复工作正常。请参阅我的代码。android: - 报警管理器不工作

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> 

为什么它不工作?

+1

你在你的清单XML添加权限。它需要READ_PHONE_STATE权限。 –

+0

是在应用程序标签上方... – freshDroid

+0

它不应该在应用程序标签上方,而是封装在标签中....类似于<应用程序..... <接收方将您的数据放在此处>' – THelper

回答

3

我得到了answer.I作出以下改动:

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); 
+0

上方有效,但是您可以在强制停止应用程序后告诉我它不工作,因此您可以找到任何解决方案 – PankajAndroid

-1

你有没有试图改变

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

你有没有试图改变6000到别的东西?好像你有其他一切正确。

编辑:

确保您有READ_PHONE_STATE许可,您的清单。

+0

这是6000的问题吗?这是否意味着6秒? – freshDroid

+0

看看我的编辑。 –

+0

yes.i在应用程序标签 – freshDroid

1

我有同样的问题,直到我发现,我已经把我的广播接收器上不同的包,不一般。

只是改变:

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

为:

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