AlarmManager 停止运行

发布于 2024-12-04 17:20:30 字数 3011 浏览 2 评论 0原文

我正在编写一个程序,该程序应在后台每 10 分钟运行一次。只要我积极使用手机,我的代码似乎就可以正常工作,但是经过很长一段时间(例如一夜之间),该过程似乎会自行停止。当我的程序按预期运行时,我可以在设备上的“缓存进程”下查看它,但一段时间后它将停止在列表中显示。

我正在阅读有关 WakefulIntentService 的内容,想知道我是否需要使用它。据我了解,即使手机处于睡眠状态,它也会让您的后台进程保持运行。不确定“睡眠”在 Android 中意味着什么,如果是在关闭电源时,或者如果一段时间不使用手机是否会进入“睡眠”状态。

这是我正在使用的代码:

主类:

 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 类:

 public class AlarmReceiver extends BroadcastReceiver 
 {
      @Override
      public void onReceive(Context context, Intent intent) 
      { 
           context.startService(new Intent(context, MainService.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>

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>

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

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

发布评论

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

评论(1

茶底世界 2024-12-11 17:20:30

即使设备处于睡眠状态,它真的必须每十分钟运行一次吗?如果没有,请使用 AlarmManager.ELAPSED_REALTIME,您的服务将在设备唤醒时运行,从而节省大量电池。至于你的问题,你可以假设屏幕变暗==进入睡眠状态(当然,如果其他服务持有唤醒锁,则情况并非如此)。

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

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