定于AlarmManager Android的服务没有启动

编程入门 行业动态 更新时间:2024-10-25 22:25:40
本文介绍了定于AlarmManager Android的服务没有启动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个服务(在清单文件中正确声明)是在的onDestroy()安排其自身又通过 AlarmManager 。然而,服务没有启动,即使的onDestroy()运行正常。可能是什么问题?

调度code:

@覆盖公共无效的onDestroy(){    广播接收器BR =新的广播接收器(){        @覆盖        公共无效的onReceive(上下文C,意图我){            c.startService(新意图(C,MyService.class));        }    };    registerReceiver(BR,新的IntentFilter(XXXX));    PI的PendingIntent = PendingIntent.getBroadcast(这一点,0,新的意向(XXXX),0);    AlarmManager点=(AlarmManager)(this.getSystemService(Context.ALARM_SERVICE));    am.set(AlarmManager.ELAPSED_REALTIME_WAKEUP,SystemClock.elapsedRealtime()+ ONE_MINUTE,PI);    Log.i(的onDestroy,服务调度。);    unregisterReceiver(BR);    super.onDestroy();}

在清单文件中的服务声明:

<服务    机器人:名字=com.xxxx.MyService    机器人:出口=真正的>< /服务>

解决方案

首先,你的广播接收器是的onDestroy结束后要离开纳秒( ),使其失去作用。请在清单与注册广播接收器 A <接收方式> 元素

二, _WAKEUP 报警只能保持设备清醒足够长的时间在处理一个的onReceive() 广播接收器。你需要使用激活锁定来保持设备比清醒更长时间。这取决于你在做什么,我的 WakefulIntentService 可能会有些用处。

第三,你不需要导出服务,并通过这样做,你打开了潜在的安全漏洞。我建议删除的android:出口=真正的

I have a service (properly declared in the Manifest file) that upon onDestroy() schedules itself to start again one minute later via AlarmManager. However the service is not starting even though onDestroy() runs correctly. What could be wrong?

The scheduling code:

@Override public void onDestroy() { BroadcastReceiver br = new BroadcastReceiver() { @Override public void onReceive(Context c, Intent i) { c.startService(new Intent(c, MyService.class)); } }; registerReceiver(br, new IntentFilter("xxxx")); PendingIntent pi = PendingIntent.getBroadcast(this, 0, new Intent("xxxx"), 0); AlarmManager am = (AlarmManager)(this.getSystemService(Context.ALARM_SERVICE)); am.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() + ONE_MINUTE, pi); Log.i("onDestroy", "Service scheduled."); unregisterReceiver(br); super.onDestroy(); }

The service declaration in the Manifest file:

<service android:name="com.xxxx.MyService" android:exported="true"> </service>

解决方案

First, your BroadcastReceiver is going away nanoseconds after the end of onDestroy(), making it useless. Please register your BroadcastReceiver in the manifest with a <receiver> element.

Second, a _WAKEUP alarm only keeps the device awake long enough to process an onReceive() in a BroadcastReceiver. You need to use a WakeLock to keep the device awake longer than that. Depending upon what you are doing, my WakefulIntentService may be of some use.

Third, you do not need to export your service, and by doing so, you are opening up potential security holes. I recommend removing android:exported="true".

更多推荐

定于AlarmManager Android的服务没有启动

本文发布于:2023-11-27 04:36:19,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1636622.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:AlarmManager   Android

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!