如何在屏幕休眠时在后台创建 android 应用程序 运行?
How to make an android app run in background when the screen sleeps?
我正在开发一个跟踪应用程序,它通过每 3 秒获取用户的当前位置来跟踪用户。当屏幕打开时,我能够获取经纬度值。但是当屏幕休眠时。我无法获取数据。
代码:
@Override
public void onLocationChanged(Location location) {
mLastLocation = location;
if (mCurrLocationMarker != null)
{
mCurrLocationMarker.remove();
}
latitude = location.getLatitude();
longitude = location.getLongitude();
latLngcurrent = new LatLng(location.getLatitude(), location.getLongitude());
Toast.makeText(context,String.valueOf(latitude)+" "+String.valueOf(longitude), Toast.LENGTH_LONG).show();
Log.d("onLocationChanged", String.format("latitude:%.3f longitude:%.3f",latitude,longitude));
Log.d("onLocationChanged", "Exit");
Toast.makeText(this, "exiting LocationChanged ", Toast.LENGTH_SHORT).show();
}
上面给出的 onLocationChanged 方法每 3 秒获取一次经纬度值。即使屏幕休眠,我应该怎么做才能使此方法 运行。我已经阅读了有关唤醒锁的信息,但不知道如何实现它,我们将不胜感激。提前致谢。
注意:应用程序 运行在完成多任务处理后非常完美,
每 3 秒使用作业调度程序获取位置它在屏幕休眠时也有效https://developer.android.com/reference/android/app/job/JobScheduler.html
您需要使用 Service
来获取位置更新,即使屏幕处于休眠状态或您的应用未打开也是如此。
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
public class MyLocationService extends Service
{
private static final String TAG = "MyLocationService";
private LocationManager mLocationManager = null;
private static final int LOCATION_INTERVAL = 3000;
private static final float LOCATION_DISTANCE = 10f;
LocationListener[] mLocationListeners = new LocationListener[] {
new LocationListener(LocationManager.GPS_PROVIDER),
new LocationListener(LocationManager.NETWORK_PROVIDER)
};
@Override
public IBinder onBind(Intent arg0)
{
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId)
{
Log.e(TAG, "onStartCommand");
super.onStartCommand(intent, flags, startId);
return START_STICKY;
}
@Override
public void onCreate()
{
Log.e(TAG, "onCreate");
initializeLocationManager();
try {
mLocationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER, LOCATION_INTERVAL, LOCATION_DISTANCE,
mLocationListeners[1]);
} catch (java.lang.SecurityException ex) {
Log.i(TAG, "fail to request location update, ignore", ex);
} catch (IllegalArgumentException ex) {
Log.d(TAG, "network provider does not exist, " + ex.getMessage());
}
try {
mLocationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER, LOCATION_INTERVAL, LOCATION_DISTANCE,
mLocationListeners[0]);
} catch (java.lang.SecurityException ex) {
Log.i(TAG, "fail to request location update, ignore", ex);
} catch (IllegalArgumentException ex) {
Log.d(TAG, "gps provider does not exist " + ex.getMessage());
}
}
@Override
public void onDestroy()
{
Log.e(TAG, "onDestroy");
super.onDestroy();
if (mLocationManager != null) {
for (int i = 0; i < mLocationListeners.length; i++) {
try {
mLocationManager.removeUpdates(mLocationListeners[i]);
} catch (Exception ex) {
Log.i(TAG, "fail to remove location listners, ignore", ex);
}
}
}
}
private void initializeLocationManager() {
Log.e(TAG, "initializeLocationManager");
if (mLocationManager == null) {
mLocationManager = (LocationManager) getApplicationContext().getSystemService(Context.LOCATION_SERVICE);
}
}
// create LocationListener class to get location updates
private class LocationListener implements android.location.LocationListener
{
Location mLastLocation;
public LocationListener(String provider)
{
Log.e(TAG, "LocationListener " + provider);
mLastLocation = new Location(provider);
}
@Override
public void onLocationChanged(Location location)
{
Log.e(TAG, "onLocationChanged: " + location);
mLastLocation.set(location);
}
@Override
public void onProviderDisabled(String provider)
{
Log.e(TAG, "onProviderDisabled: " + provider);
}
@Override
public void onProviderEnabled(String provider)
{
Log.e(TAG, "onProviderEnabled: " + provider);
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras)
{
Log.e(TAG, "onStatusChanged: " + provider);
}
}
}
将 MyLocationService
添加到您的 AndroidManifest.xml
中:
<service android:name=".MyLocationService" android:process=":mylocation_service" />
不要忘记在您的 AndroidManifest.xml
文件中添加以下两个权限:
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
从 activity:
启动服务
您可以通过将 Intent(指定要启动的服务)传递给 startService()
,从 activity 或其他应用程序组件启动 service
。 Android 系统调用服务的 onStartCommand()
方法并将 Intent 传递给它。
Intent intent = new Intent(this, MyLocationService.class);
startService(intent);
更新:
即使设备屏幕关闭,您也需要保持 partial wake lock 到 运行 服务。
如果您持有部分唤醒锁,CPU 将继续 运行,无论任何显示超时或屏幕状态如何,甚至在用户按下电源按钮后也是如此。
获取唤醒锁:
PowerManager mgr = (PowerManager)context.getSystemService(Context.POWER_SERVICE);
WakeLock wakeLock = mgr.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "MyWakeLock");
wakeLock.acquire();
要发布它:
wakeLock.release();
尝试使用 Service or JobScheduler and implement LocationListener
@Override
public void onLocationChanged(Location location) {
mLastLocation = location;
if (mCurrLocationMarker != null)
{
mCurrLocationMarker.remove();
}
latitude = location.getLatitude();
longitude = location.getLongitude();
latLngcurrent = new LatLng(location.getLatitude(),location.getLongitude());
Toast.makeText(context,String.valueOf(latitude)+" "+String.valueOf(longitude), Toast.LENGTH_LONG).show();
Log.d("onLocationChanged", String.format("latitude:%.3f longitude:%.3f",latitude,longitude));
Log.d("onLocationChanged", "Exit");
Toast.makeText(this, "exiting LocationChanged ", Toast.LENGTH_SHORT).show();
}
我正在开发一个跟踪应用程序,它通过每 3 秒获取用户的当前位置来跟踪用户。当屏幕打开时,我能够获取经纬度值。但是当屏幕休眠时。我无法获取数据。
代码:
@Override
public void onLocationChanged(Location location) {
mLastLocation = location;
if (mCurrLocationMarker != null)
{
mCurrLocationMarker.remove();
}
latitude = location.getLatitude();
longitude = location.getLongitude();
latLngcurrent = new LatLng(location.getLatitude(), location.getLongitude());
Toast.makeText(context,String.valueOf(latitude)+" "+String.valueOf(longitude), Toast.LENGTH_LONG).show();
Log.d("onLocationChanged", String.format("latitude:%.3f longitude:%.3f",latitude,longitude));
Log.d("onLocationChanged", "Exit");
Toast.makeText(this, "exiting LocationChanged ", Toast.LENGTH_SHORT).show();
}
上面给出的 onLocationChanged 方法每 3 秒获取一次经纬度值。即使屏幕休眠,我应该怎么做才能使此方法 运行。我已经阅读了有关唤醒锁的信息,但不知道如何实现它,我们将不胜感激。提前致谢。
注意:应用程序 运行在完成多任务处理后非常完美,
每 3 秒使用作业调度程序获取位置它在屏幕休眠时也有效https://developer.android.com/reference/android/app/job/JobScheduler.html
您需要使用 Service
来获取位置更新,即使屏幕处于休眠状态或您的应用未打开也是如此。
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
public class MyLocationService extends Service
{
private static final String TAG = "MyLocationService";
private LocationManager mLocationManager = null;
private static final int LOCATION_INTERVAL = 3000;
private static final float LOCATION_DISTANCE = 10f;
LocationListener[] mLocationListeners = new LocationListener[] {
new LocationListener(LocationManager.GPS_PROVIDER),
new LocationListener(LocationManager.NETWORK_PROVIDER)
};
@Override
public IBinder onBind(Intent arg0)
{
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId)
{
Log.e(TAG, "onStartCommand");
super.onStartCommand(intent, flags, startId);
return START_STICKY;
}
@Override
public void onCreate()
{
Log.e(TAG, "onCreate");
initializeLocationManager();
try {
mLocationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER, LOCATION_INTERVAL, LOCATION_DISTANCE,
mLocationListeners[1]);
} catch (java.lang.SecurityException ex) {
Log.i(TAG, "fail to request location update, ignore", ex);
} catch (IllegalArgumentException ex) {
Log.d(TAG, "network provider does not exist, " + ex.getMessage());
}
try {
mLocationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER, LOCATION_INTERVAL, LOCATION_DISTANCE,
mLocationListeners[0]);
} catch (java.lang.SecurityException ex) {
Log.i(TAG, "fail to request location update, ignore", ex);
} catch (IllegalArgumentException ex) {
Log.d(TAG, "gps provider does not exist " + ex.getMessage());
}
}
@Override
public void onDestroy()
{
Log.e(TAG, "onDestroy");
super.onDestroy();
if (mLocationManager != null) {
for (int i = 0; i < mLocationListeners.length; i++) {
try {
mLocationManager.removeUpdates(mLocationListeners[i]);
} catch (Exception ex) {
Log.i(TAG, "fail to remove location listners, ignore", ex);
}
}
}
}
private void initializeLocationManager() {
Log.e(TAG, "initializeLocationManager");
if (mLocationManager == null) {
mLocationManager = (LocationManager) getApplicationContext().getSystemService(Context.LOCATION_SERVICE);
}
}
// create LocationListener class to get location updates
private class LocationListener implements android.location.LocationListener
{
Location mLastLocation;
public LocationListener(String provider)
{
Log.e(TAG, "LocationListener " + provider);
mLastLocation = new Location(provider);
}
@Override
public void onLocationChanged(Location location)
{
Log.e(TAG, "onLocationChanged: " + location);
mLastLocation.set(location);
}
@Override
public void onProviderDisabled(String provider)
{
Log.e(TAG, "onProviderDisabled: " + provider);
}
@Override
public void onProviderEnabled(String provider)
{
Log.e(TAG, "onProviderEnabled: " + provider);
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras)
{
Log.e(TAG, "onStatusChanged: " + provider);
}
}
}
将 MyLocationService
添加到您的 AndroidManifest.xml
中:
<service android:name=".MyLocationService" android:process=":mylocation_service" />
不要忘记在您的 AndroidManifest.xml
文件中添加以下两个权限:
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
从 activity:
启动服务您可以通过将 Intent(指定要启动的服务)传递给 startService()
,从 activity 或其他应用程序组件启动 service
。 Android 系统调用服务的 onStartCommand()
方法并将 Intent 传递给它。
Intent intent = new Intent(this, MyLocationService.class);
startService(intent);
更新:
即使设备屏幕关闭,您也需要保持 partial wake lock 到 运行 服务。
如果您持有部分唤醒锁,CPU 将继续 运行,无论任何显示超时或屏幕状态如何,甚至在用户按下电源按钮后也是如此。
获取唤醒锁:
PowerManager mgr = (PowerManager)context.getSystemService(Context.POWER_SERVICE);
WakeLock wakeLock = mgr.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "MyWakeLock");
wakeLock.acquire();
要发布它:
wakeLock.release();
尝试使用 Service or JobScheduler and implement LocationListener
@Override
public void onLocationChanged(Location location) {
mLastLocation = location;
if (mCurrLocationMarker != null)
{
mCurrLocationMarker.remove();
}
latitude = location.getLatitude();
longitude = location.getLongitude();
latLngcurrent = new LatLng(location.getLatitude(),location.getLongitude());
Toast.makeText(context,String.valueOf(latitude)+" "+String.valueOf(longitude), Toast.LENGTH_LONG).show();
Log.d("onLocationChanged", String.format("latitude:%.3f longitude:%.3f",latitude,longitude));
Log.d("onLocationChanged", "Exit");
Toast.makeText(this, "exiting LocationChanged ", Toast.LENGTH_SHORT).show();
}