简体   繁体   English

屏幕关闭时服务关闭

[英]Service is off when screen is turns off

I am confused a little bit on the behavior of the service.I created a service class which is Emailing my location as soon as the latitude and longitude of My Device is changed.The Service is working perfect when the screen of my phone is on but as soon as the phone screen off the service is not updating me for new locations.I have read somewhere on the SO about wake lock.I don't know how can I implement wake lock on my service and even I don't know whether it will work or not.Please guide me how can I acquire wake lock on my service class Code for my service class: 我对服务的行为有些困惑。我创建了一个服务类,当我的设备的纬度和经度发生变化时,它会立即通过电子邮件发送我的位置。当手机屏幕打开时,该服务可以正常工作,但是服务关闭后,电话不会立即为我更新新位置。我已经在SO上的某个地方阅读了有关唤醒锁定的信息。我不知道如何在服务上实现唤醒锁定,甚至不知道是否它会正常工作。请指导我如何获取服务类代码的唤醒锁:

public class LocationUpdater extends Service {
private LocationManager lm;
private LocationListener locationListener;
@Override
public IBinder onBind(Intent intent) {
    return null;
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
           Toast.makeText(getApplicationContext(), "Location updater started",Toast.LENGTH_LONG).show();
    locationListener = new MyLocationListener();
    lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    if (!lm.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
        lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0,0,
                locationListener);
    } else {
        lm.requestLocationUpdates(LocationManager.GPS_PROVIDER,0, 0,
                locationListener);
        lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,0, 0,
                locationListener);
    }


    return START_STICKY;
}

@Override
public void onDestroy() {
    lm.removeUpdates(locationListener);
    super.onDestroy();
}
@Override
public void onCreate() {
    Toast.maketext(getApplicationContext,"Updater Serive Created",Toast.LENGTH_LONG).show;


    } 
private class MyLocationListener implements LocationListener {

    @Override
    public void onLocationChanged(Location loc) {

        if (loc != null) {
         String latitude=loc.getLatitude();
         Striing longitude=loc.getLongitude();
         emailMyLocation(latitude,longitude);
         }
        }
       @Override
       public void onStatusChanged(String provider, int status, Bundle extras) {

    }

    @Override
    public void onProviderEnabled(String provider) {

    }

    @Override
    public void onProviderDisabled(String provider) {

    }
    }
    }

This is the service class which is updating the location of my device to my email.Please tell how can I make it work even if the screen is off. 这是服务类,它将设备的位置更新为我的电子邮件。请告诉我,即使屏幕关闭,我也要如何使它工作。

You should check out answer for this question in the following thread: How can I keep my Android service running when the screen is turned off? 您应该在以下线程中查看该问题的答案: 屏幕关闭时如何保持我的Android服务运行? . Probably, you need a partial WakeLock, which will keep CPU running even when the screen is turned off. 可能需要部分WakeLock,即使关闭屏幕,它也可以保持CPU运行。 You should also check documentation of PowerManager in Android API, which explains behavior of wake locks. 您还应该查看Android API中PowerManager的文档,该文档说明了唤醒锁的行为。

You can aquire the wake lock like this: Make a Globals class and declare wakelock as static variable 您可以像这样获取唤醒锁:创建一个Globals类,并将唤醒锁声明为静态变量。

 public class Globals {
          public static PowerManager.WakeLock wakelock;
 }

Now in your service class Declare these: 现在在您的服务类中声明以下内容:

   PowerManager.WakeLock wakeLock ;
   PowerManager pm;

Now you can have two functions for wakelocks: 现在您可以拥有两个用于唤醒锁的功能:

public void acquirewakeLock() {
         if(Globals.wakelock!=null){
         Globals.wakelock.release();
         Globals.wakelock=null;
         }
    pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
    wakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
    "TrackerService");
    wakeLock.acquire();
    Globals.wakelock=this.wakeLock;  
}
public void releaseWakelock() {
      wakeLock.release();
      }

Now in your onCreate method you can quire wakelock with this: 现在,在您的onCreate方法中,您可以使用以下方法获取唤醒锁:

    @Override
    public void onCreate() {
       acquirewakeLock()
     }

This will take care of whether CPU is held by other activities and services or not.And you will be able to give cpu to your sevice class. 这将照顾CPU是否由其他活动和服务占用,并且可以将cpu分配给您的服务班级。 You can also use 您也可以使用

   wakelock.acquire(time in milliseconds)

if you know the time your service needs to process. 如果您知道您的服务需要处理的时间。 :) :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM