android:-警报管理器不工作

发布于 2024-12-20 03:29:24 字数 1218 浏览 3 评论 0原文

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

   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>

为什么它不工作?

In my code alarm manger is not working.Rest of my application is working well.Please see my code.

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

and my android AlarmService class:-

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

}
}

in manifest I have done this;

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

Why its not working??

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

清醇 2024-12-27 03:29:24

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

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

I got the answer.I made following changes:

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

In my AndroidAlarmService class:

Intent homeIn=new Intent(context,Home.class);
homeIn.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(homeIn);
淑女气质 2024-12-27 03:29:24

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

简单地改变:

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

为:

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

I had the same problem until I found that I had put my Broadcast Receiver on a different package, not the general.

Simply changed:

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

for:

<receiver android:name="com.MyCompany.MyPackage.AndroidAlarmService" android:enabled="true" >
水波映月 2024-12-27 03:29:24

您是否尝试过

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

将 6000 更改为其他内容?看来您其他一切都正确。

编辑:

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

Have you tried changing the

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

Have you tried changing the 6000 to something else? It seems like you have everything else correct.

EDIT:

Make sure you have the Read_phone_state permission in your manifest.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文