【问题标题】:How to make service run even in sleep mode?即使在睡眠模式下,如何使服务运行?
【发布时间】:2013-04-11 07:05:00
【问题描述】:

我有实现位置监听器的服务。现在我的问题是如何确保我的服务即使在睡眠模式下也能捕获位置。我已阅读有关警报管理器的信息

alarm.setRepeating(AlarmManager.RTC_WAKEUP, triggerAtMillis, intervalMillis, operation);

但是如何使用它。这是我的代码..任何帮助将不胜感激..
我的服务

public class LocationCaptureService extends Service implements LocationListener {
public static int inteval;
java.sql.Timestamp createdTime;
LocationManager LocationMngr;

@Override
public void onCreate() {
    inteval=10*1000;

    startLocationListener(inteval);
}

@Override
public IBinder onBind(Intent arg0) {
    return null;
}

private void startLocationListener(int inteval,String nwProvider) {
    this.LocationMngr = (LocationManager)getSystemService(Context.LOCATION_SERVICE);        
        this.LocationMngr.requestLocationUpdates(LocationManager.GPS_PROVIDER, inteval, 0, this);
}   

public void onLocationChanged(Location location) {
    String status="c",S=null;
    double longitude,lattitude,altitude;
    g_currentBestLocation = location;
    createdTime = new Timestamp (new java.util.Date().getTime());    
         longitude=location.getLongitude();
         lattitude=location.getLatitude();
         altitude=location.getAltitude();
         //use this
         }
  }                              
  public void onProviderDisabled(String provider) {}                            
  public void onProviderEnabled(String provider) {}                             
  public void onStatusChanged(String provider, int status, Bundle extras) {}

}

【问题讨论】:

  • 我认为您不想使用AlarmManager,除非您需要安排特定的时间间隔来查询位置。如果您希望在 a) 您的应用完成 b) 用户告诉您的应用停止之前不要终止您的服务,那么您需要一种不同的方法。
  • thanx for the comment.. 你能告诉我如何在我的应用程序完成之前让服务不被杀死。我需要返回 start_sticky

标签: android android-service


【解决方案1】:

如果您想确保您的服务不会被操作系统杀死/回收,您需要将其设为前台服务。默认情况下,所有服务都是后台服务,这意味着当操作系统需要资源时它们将被终止。详情参考这个doc

基本上,您需要为您的服务创建一个Notification 并指明它是前台。这样,用户将看到持续通知,因此他知道您的应用正在运行,并且操作系统不会终止您的服务。

这是一个简单的示例,说明如何创建通知(在您的服务中执行此操作)并使其成为前台:

Intent intent = new Intent(this, typeof(SomeActivityInYourApp));
PendingIntent pi = PendingIntent.getActivity(this, 0, intent,   PendingIntent.FLAG_UPDATE_CURRENT);

NotificationCompat.Builder builder = new NotificationCompat.Builder(this);

builder.setSmallIcon(Resource.Drawable.my_icon);
builder.setTicker("App info string");
builder.setContentIntent(pi);
builder.setOngoing(true);
builder.setOnlyAlertOnce(true);

Notification notification = builder.build();

// optionally set a custom view

startForeground(SERVICE_NOTIFICATION_ID, notification);

请注意,上述示例是初步的,不包含取消通知的代码等。此外,当您的应用不再需要该服务时,它应该调用 stopForeground 以删除通知并允许您的服务被终止,不这样做是浪费资源。

【讨论】:

  • 我在华为U8860上尝试过这种方法,当设备处于睡眠模式(屏幕关闭并且设备被锁定)时服务不工作。这个问题只存在于这个华为 U8860 上,我在 HTC ONE X 、 S3 、 Experia T、 S2 上尝试过它......并且它有效。关于为什么这不适用于此设备的任何想法?谢谢
  • 一年后,我正在回复这条评论,迟到总比没有好。您可能需要持有 WakeLock 才能让您的服务继续运行。请参阅developer.android.com/reference/android/os/… 了解更多信息。
  • 一年零一天 :D .
猜你喜欢
  • 2014-03-01
  • 1970-01-01
  • 1970-01-01
  • 2016-06-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多