Android AlarmManager停止运行

Android AlarmManager停止运行,android,alarmmanager,android-service,Android,Alarmmanager,Android Service,我正在写一个程序,应该在后台每10分钟运行一次。只要我积极地使用我的手机,我的代码似乎工作得很好,但是经过很长一段时间,比如说一夜之间,这个过程似乎自行停止了。当我的程序正常运行时,我可以在我的设备上的“缓存进程”下查看它,但过一会儿它就会停止显示在列表中 我在读关于,想知道我是否需要使用它。据我所知,即使手机处于休眠状态,它也会让您的后台进程保持运行。不知道“睡眠”在安卓系统中是什么意思,如果它是在你关机的时候,或者如果手机暂时不使用,它会进入“睡眠”状态 这是我正在使用的代码: 主要类别:

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

我在读关于,想知道我是否需要使用它。据我所知,即使手机处于休眠状态,它也会让您的后台进程保持运行。不知道“睡眠”在安卓系统中是什么意思,如果它是在你关机的时候,或者如果手机暂时不使用,它会进入“睡眠”状态

这是我正在使用的代码:

主要类别:

 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);
      }
 }
广播接收器类别:

 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();
}
公共类MainService扩展服务
{  
受保护的无效句柄内容(意图)
{
//获取尾流锁
PowerManager pm=(PowerManager)getSystemService(POWER\u服务);
mWakeLock=pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,“我的标签”);
mWakeLock.acquire();//检查全局背景数据设置
ConnectivityManager cm=(ConnectivityManager)getSystemService(CONNECTIVITY_服务);
如果(!cm.getBackgroundDataSetting())
{ 
stopSelf();
返回;
}
新建FetchItems().execute();
}
}
私有类FetchItems扩展异步任务
{
受保护的空位背景(空位…未使用)
{           
SomeLongProcess();
返回null;
}
@凌驾
受保护的void onPostExecute(void结果)
{   
stopSelf();
}
}
@凌驾
公共int onStartCommand(Intent Intent、int标志、int startId)
{
手册内容(意图);
返回开始时间;
}
@凌驾
公共void onStart(Intent Intent,int startId){
手册内容(意图);
}
@凌驾
公共空间
{
super.ondestory();
AlarmManager AlarmManager=(AlarmManager)getSystemService(报警服务);
Intent myIntent=新Intent(这是AlarmReceiver.class);
pendingent pendingent=pendingent.getService(this,0,myIntent,0);
alarmManager.cancel(挂起事件);
mWakeLock.release();
}
AndroidManifest.xml:

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


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

这是一个好问题。我正在编写的程序基本上是一个RSS阅读器,在设置中,用户可以指定是否需要自动更新,因此我猜它应该在设备处于睡眠状态时运行,否则它只会在他们使用手机时更新他们的订阅源。除非他们在唤醒手机时首先打开的是你的应用程序,您将有足够的时间缓存提要。考虑这一点:用户(和电话)已经睡了8个小时,在那个时候你将运行你的服务48次。RSS不是关键数据,为了节省一些电池,你可能想稍微放松一下你的需求。这是真的。我实际上已经设置好了,用户可以选择他们想要的时间间隔,并显示一条消息,说明间隔越短,电池的使用量就越多。他们可以在5分钟到5小时内选择任何地方。我只是不想把它放在上面的代码中,因为它似乎不相关。但问题仍然是,当手机一段时间没有使用时,为什么服务会停止运行。如果抛出未捕获的异常或进程被终止,相关警报将被清除。您可以检查logcat并向您的服务和接收器添加一些日志记录。这很好。我不确定如果设备睡眠,AlarmManager是否会自动停止,我需要使用WakefulIntentService之类的工具,但听起来AlarmManager应该在一切正常的情况下不间断运行。我将查找抛出的任何异常。非常感谢。