Android 锁屏无法继续定位问题

// 每5秒定位一次

mLocationOption.setInterval(5 * 1000);

// 地址信息

mLocationOption.setNeedAddress(true);

mLocationClient.setLocationOption(mLocationOption);

mLocationClient.setLocationListener(locationListener);

mLocationClient.startLocation();

}

/**

  • 停止定位

*/

void stopLocation() {

if (null != mLocationClient) {

mLocationClient.stopLocation();

}

}

AMapLocationListener locationListener = new AMapLocationListener() {

@Override

public void onLocationChanged(AMapLocation aMapLocation) {

//发送结果的通知

sendLocationBroadcast(aMapLocation);

if (!mIsWifiCloseable) {

return;

}

if (aMapLocation.getErrorCode() == AMapLocation.LOCATION_SUCCESS) {

mWifiAutoCloseDelegate.onLocateSuccess(getApplicationContext(), PowerManagerUtil.getInstance().isScreenOn(getApplicationContext()), NetUtil.getInstance().isMobileAva(getApplicationContext()));

} else {

mWifiAutoCloseDelegate.onLocateFail(getApplicationContext() , aMapLocation.getErrorCode() , PowerManagerUtil.getInstance().isScreenOn(getApplicationContext()), NetUtil.getInstance().isWifiCon(getApplicationContext()));

}

}

private void sendLocationBroadcast(AMapLocation aMapLocation) {

if (null != aMapLocation) {

Intent mIntent = new Intent(LocationChangBroadcastReceiver.RECEIVER_ACTION);

mIntent.putExtra(LocationChangBroadcastReceiver.RECEIVER_DATA, aMapLocation);

sendBroadcast(mIntent);

ToastUtils.show(“获取到定位信息”);

String string = System.currentTimeMillis() + “,”+aMapLocation.getLatitude() + “,” + aMapLocation.getLongitude();

Utils.saveFile(string, “backlocation.txt”, true);

}

}

};

}

定位服务的基类

/**

  • 利用双service进行notification绑定,进而将Service的OOM_ADJ提高到1

  • 同时利用LocationHelperService充当守护进程,在NotificationService被关闭后,重启他。

  • 如果LocationHelperService被停止,NotificationService不负责唤醒

*/

public class NotificationService extends Service {

/**

  • startForeground的 noti_id

*/

private static int NOTI_ID = 123321;

private Utils.CloseServiceReceiver mCloseReceiver;

@Override

public int onStartCommand(Intent intent, int flags, int startId) {

Log.e(“background location”, “远程服务调用成功”);

mCloseReceiver = new Utils.CloseServiceReceiver(this);

registerReceiver(mCloseReceiver, Utils.getCloseServiceFilter());

return START_STICKY;

}

@Override

public void onDestroy() {

if (mCloseReceiver != null) {

unregisterReceiver(mCloseReceiver);

mCloseReceiver = null;

}

super.onDestroy();

}

private final String mHelperServiceName = “com.hdsx.background.locationservice.LocationHelperService”;

/**

  • 触发利用notification增加进程优先级

*/

protected void applyNotiKeepMech() {

startForeground(NOTI_ID, Utils.buildNotification(getBaseContext()));

startBindHelperService();

}

public void unApplyNotiKeepMech() {

stopForeground(true);

}

public Binder mBinder;

public class LocationServiceBinder extends ILocationServiceAIDL.Stub {

@Override

public void onFinishBind(){

}

}

private ILocationHelperServiceAIDL mHelperAIDL;

private void startBindHelperService() {

connection = new ServiceConnection() {

@Override

public void onServiceDisconnected(ComponentName name) {

//doing nothing

}

@Override

public void onServiceConnected(ComponentName name, IBinder service) {

ILocationHelperServiceAIDL l = ILocationHelperServiceAIDL.Stub.asInterface(service);

mHelperAIDL = l;

try {

l.onFinishBind(NOTI_ID);

} catch (RemoteException e) {

e.printStackTrace();

}

}

};

Intent intent = new Intent();

intent.setAction(mHelperServiceName);

bindService(Utils.getExplicitIntent(getApplicationContext(), intent), connection, Service.BIND_AUTO_CREATE);

}

private ServiceConnection connection;

@Nullable

@Override

public IBinder onBind(Intent intent) {

if (mBinder == null) {

mBinder = new LocationServiceBinder();

}

return mBinder;

}

}

另外一个服务:

public class LocationHelperService extends Service {

private Utils.CloseServiceReceiver mCloseReceiver;

@Override

public void onCreate() {

super.onCreate();

startBind();

mCloseReceiver = new Utils.CloseServiceReceiver(this);

registerReceiver(mCloseReceiver, Utils.getCloseServiceFilter());

}

@Override

public void onDestroy() {

if (mInnerConnection != null) {

unbindService(mInnerConnection);

mInnerConnection = null;

}

if (mCloseReceiver != null) {

unregisterReceiver(mCloseReceiver);

mCloseReceiver = null;

}

super.onDestroy();

}

private ServiceConnection mInnerConnection;

private void startBind() {

final String locationServiceName = “com.hdsx.background.locationservice.LocationService”;

mInnerConnection = new ServiceConnection() {

@Override

public void onServiceDisconnected(ComponentName name) {

Intent intent = new Intent();

intent.setAction(locationServiceName);

startService(Utils.getExplicitIntent(getApplicationContext(), intent));

}

@Override

public void onServiceConnected(ComponentName name, IBinder service) {

ILocationServiceAIDL l = ILocationServiceAIDL.Stub.asInterface(service);

try {

l.onFinishBind();

} catch (RemoteException e) {

e.printStackTrace();

}

}

};

Intent intent = new Intent();

intent.setAction(locationServiceName);

bindService(Utils.getExplicitIntent(getApplicationContext(), intent), mInnerConnection, Service.BIND_AUTO_CREATE);

}

private HelperBinder mBinder;

private class HelperBinder extends ILocationHelperServiceAIDL.Stub {

Android锁屏无法继续定位问题 产生问题的原因: 手机锁屏后,Android系统为了省电以及减少CPU消耗,在一段时间后会将手机进入休眠状态。此时的服务以及线程等都会停止。 最近就这个问题,阅读了很多代码以及官方文档,下面就说下最近都尝试过的方式,可能其中有些您实现了,我这边没实现,望见谅。本文采用的高德定位。 一、PowerManager.WakeLock (1)直接强制当前页面cpu运行 private PowerManager pm; private PowerManager.WakeLock wakeLock; @Override public void onCreate() { super.onCreate(); //创建PowerManager对象 pm = (PowerManager) getSystemService(Context.POWER_SERVICE); //保持cpu一直运行,不管屏幕是否黑屏 wakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "CPUKeepRunning"); wakeLock.acquire(); } @Override public void onDestroy() { wakeLock.release(); super.onDestroy(); } 这个写法我表示并没有什么用,并不能强制cpu持续运行。 (2)WakefulBroadcastReceiver public class WLWakefulReceiver extends WakefulBroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { // String extra = intent.getStringExtra("msg"); Intent serviceIntent = new Intent(context, MyIntentService.class); serviceIntent.putExtra("msg", extra); startWakefulService(context, serviceIntent); } } WakefulBroadcastReceiver 内部的原理也是PowerManager,注册广播时8.0的请动态注册,静态没有用。广播注册完了之后,写一个服务用来与广播互动。 public class MyIntentService extends IntentService { public MyIntentService() { super("MyIntentService"); } @Override public void onCreate() { super.onCreate(); } @Override protected void onHandleIntent(@Nullable final Intent intent) { //子线程中执行 Log.i("MyIntentService", "onHandleIntent"); String extra = intent.getStringExtra("msg"); new Thread(new Runnable() { @Override public void run() { LocationUtil.getInstance().startLocation(LocationUtil.NULL, new LocationUtil.OnLocationBack() { @Override public void back(AMapLocation aMapLocation, String backString) { Log.e("定位結果", aMapLocation.getAddress()+""); 定位结果操作,我这边是把定位的时候保存到数据库里面。 } }); } }).start(); Log.i("MyIntentService", "onHandleIntent:"+extra); //调用completeWakefulIntent来释放唤醒锁。 // WLWakefulReceiver.completeWakefulIntent(intent); } } 注册
Android锁屏无法继续定位的问题可能是由于以下原因导致的: 1. 系统设置限制:Android系统在锁屏状态下会对某些功能进行限制,包括定位服务。这是为了保护用户隐私和降低耗电量。如果发现无法继续定位,可以进入系统设置中的"安全与隐私"或"锁屏与密码"等选项,检查是否有相关的设置限制。如果有,可以尝试解除限制或者调整相关设置。 2. 电池优化:Android系统的电池优化功能会对一些应用进行限制,限制它们在后台运行的能力,从而可能影响定位服务的正常运行。可以在系统设置中找到"电池"或"电池优化"选项,检查是否有对定位服务的限制。如果有,可以尝试将相关应用排除在电池优化之外或调整相关设置。 3. 应用权限:某些应用在获取定位信息时需要用户授权,但如果用户在锁屏状态下无法授权,就会导致无法继续定位。可以在系统设置中找到"应用管理"或"应用权限"选项,检查相关应用的定位权限是否被禁用或限制。如果有,可以尝试启用相应的权限。 4. 第三方应用冲突:某些第三方应用可能会对系统定位服务产生冲突,从而导致无法继续定位。可以尝试关闭或卸载一些可能与定位服务冲突的应用,然后重新尝试定位。 以上是Android锁屏无法继续定位的可能原因和解决方法,希望能对你有帮助。如果问题仍然存在,建议联系相关厂商或社区寻求更专业的技术支持。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值