AlarmManager停止运行

编程入门 行业动态 更新时间:2024-10-11 05:24:37
本文介绍了AlarmManager停止运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在写应该在后台运行,每10分钟的节目。在code我似乎我积极地使用我的手机很好,只要上班,但很长一段时间后,说在一夜之间,这个过程似乎停止它自己。当我的程序作为运行它应该我可以把设备上的缓存的进程下查看,但随后将停止显示一段时间后,在列表中。

我正在阅读有关 WakefulIntentService ,并想知道如果我需要使用的。据我了解,这将让你的后台进程运行,即使手机睡觉。不知道什么是睡眠指的Andr​​oid,如果是当关闭电源,还是手机进入休眠状态,如果它不是用了一段时间。

这是code我使用的是:

Main类:

公共类主要扩展ListActivity {      @覆盖      公共无效的onCreate(捆绑冰柱)      {           AlarmManager alarmManager =(AlarmManager)getSystemService(ALARM_SERVICE);           意图myIntent =新意图(这一点,AlarmReceiver.class);           的PendingIntent的PendingIntent = PendingIntent.getBroadcast(在此,0,myIntent,0);           alarmManager.cancel(的PendingIntent);           alarmManager.setInexactRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,SystemClock.elapsedRealtime()+ 600000 600000的PendingIntent);      } }

广播接收器类:

公共类AlarmReceiver扩展广播接收器 {      @覆盖      公共无效的onReceive(上下文的背景下,意图意图)      {           context.startService(新意图(上下文,MainService.class));      } }

服务类:

公共类MainService延伸服务 {保护无效handleIntent(意向意图){    //获得唤醒锁    电源管理PM =(电源管理)getSystemService(POWER_SERVICE);    mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,我的标签);    mWakeLock.acquire(); //检查全局后台数据设置    ConnectivityManager厘米=(ConnectivityManager)getSystemService(CONNECTIVITY_SERVICE);    如果(!cm​​.getBackgroundDataSetting())    {        stopSelf();        返回;    }    新FetchItems()执行();} } 私有类FetchItems扩展的AsyncTask<太虚,太虚,太虚> {    保护无效doInBackground(无效...未使用)    {      SomeLongProcess();      返回null;    }    @覆盖    保护无效onPostExecute(虚空结果)    {        stopSelf();    } } @覆盖 公众诠释onStartCommand(意向意图,诠释旗帜,INT startId) {   handleIntent(意向);   返回START_STICKY; } @覆盖 公共无效调用onStart(意向意图,诠释startId){   handleIntent(意向); }@覆盖公共无效的onDestroy(){         super.onDestroy();         AlarmManager alarmManager =(AlarmManager)getSystemService(ALARM_SERVICE);         意图myIntent =新意图(这一点,AlarmReceiver.class);         的PendingIntent的PendingIntent = PendingIntent.getService(在此,0,myIntent,0);         alarmManager.cancel(的PendingIntent);      mWakeLock.release();}

AndroidManifest.xml中:

<接收机器人:名字=。AlarmReceiver>    &所述;意图滤光器>        <作用机器人:名字=android.intent.action.BOOT_COMPLETED/>    &所述; /意图滤光器>< /接收器>

解决方案

是否真的有每十分钟甚至运行时,该设备是睡着了吗?如果没有,可以使用 AlarmManager.ELAPSED_REALTIME 键,当设备被唤醒,节省电池的很多的服务将运行。至于你的问题,你可以假设屏幕会暗==睡觉(当然,如果其他服务都拿着wakelocks,事实并非如此)。

I'm writing a program that should run every 10 minutes in the background. The code I have seems to work fine as long as I'm actively using my phone, but after a long period of time, say overnight, the process seems to stop on it's own. When my program is running as it should I can view it under the "cached process" on my device, but then it will stop showing in the list after awhile.

I was reading about WakefulIntentService and was wondering if I need to use that. As I understand it, it will keep your background process running even if the phone sleeps. Not sure what "sleep" means in Android, if it's when you power off, or does the phone go into a "sleep" state if it's not used for awhile.

This is the code I'm using:

Main class:

public class Main extends ListActivity { @Override public void onCreate(Bundle icicle) { AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE); Intent myIntent = new Intent(this, AlarmReceiver.class); PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, myIntent, 0); alarmManager.cancel(pendingIntent); alarmManager.setInexactRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() + 600000, 600000, pendingIntent); } }

BroadcastReceiver class:

public class AlarmReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { context.startService(new Intent(context, MainService.class)); } }

Service class:

public class MainService extends Service { protected void handleIntent(Intent intent) { // obtain the wake lock PowerManager pm = (PowerManager)getSystemService(POWER_SERVICE); mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "My Tag"); mWakeLock.acquire(); // check the global background data setting ConnectivityManager cm = (ConnectivityManager)getSystemService(CONNECTIVITY_SERVICE); if (!cm.getBackgroundDataSetting()) { stopSelf(); return; } new FetchItems().execute(); } } private class FetchItems extends AsyncTask<Void, Void, Void> { protected Void doInBackground(Void... unused) { SomeLongProcess(); return null; } @Override protected void onPostExecute(Void result) { stopSelf(); } } @Override public int onStartCommand(Intent intent, int flags, int startId) { handleIntent(intent); return START_STICKY; } @Override public void onStart(Intent intent, int startId) { handleIntent(intent); } @Override public void onDestroy() { super.onDestroy(); AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE); Intent myIntent = new Intent(this, AlarmReceiver.class); PendingIntent pendingIntent = PendingIntent.getService(this, 0, myIntent, 0); alarmManager.cancel(pendingIntent); mWakeLock.release(); }

AndroidManifest.xml:

<receiver android:name=".AlarmReceiver"> <intent-filter> <action android:name="android.intent.action.BOOT_COMPLETED" /> </intent-filter> </receiver>

解决方案

Does it really have to run every ten minutes even when the device is asleep? If it doesn't, use AlarmManager.ELAPSED_REALTIME and your service will be run when the device is wakes up, saving a lot of battery. As for your question, you can assume that the screen going dark == going to sleep (of course, if other services are holding wakelocks, that is not the case).

更多推荐

AlarmManager停止运行

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

发布评论

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

>www.elefans.com

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