Android 通过接收广播int值来控制GPS开关

/frameworks/base/services/core/java/com/android/server/policy/PhoneWindowManager.java

//*/add gps control
import android.location.LocationManager;
//*/

//*/add gps control
int currentMode;
//*/

IntentFilter filter = new IntentFilter();

filter.addAction(UiModeManager.ACTION_ENTER_CAR_MODE);

filter.addAction(UiModeManager.ACTION_EXIT_CAR_MODE);

filter.addAction(UiModeManager.ACTION_ENTER_DESK_MODE);

filter.addAction(UiModeManager.ACTION_EXIT_DESK_MODE);

filter.addAction(Intent.ACTION_DOCK_EVENT);

filter.addAction("sys.gps.switch");//*/add gps

Intent intent = context.registerReceiver(mDockReceiver, filter);

BroadcastReceiver mDockReceiver = new BroadcastReceiver() {

@Override

public void onReceive(Context context, Intent intent) {

if (Intent.ACTION_DOCK_EVENT.equals(intent.getAction())) {

mDockMode = intent.getIntExtra(Intent.EXTRA_DOCK_STATE,

Intent.EXTRA_DOCK_STATE_UNDOCKED);

} else {

try {

IUiModeManager uiModeService = IUiModeManager.Stub.asInterface(

ServiceManager.getService(Context.UI_MODE_SERVICE));

mUiMode = uiModeService.getCurrentModeType();

} catch (RemoteException e) {

}

}

updateRotation(true);

synchronized (mLock) {

updateOrientationListenerLp();

}

//*/add gps
if (intent.getAction().equals("sys.gps.switch")) {
int mydata = intent.getIntExtra("gpsSwitch", 0);//1 open 2 close
currentMode = Settings.Secure.getInt(mContext.getContentResolver(), Settings.Secure.LOCATION_MODE,Settings.Secure.LOCATION_MODE_OFF);
if (mydata == 1) {
Intent intent2 = new Intent("com.android.settings.location.MODE_CHANGING");
intent2.putExtra("CURRENT_MODE", currentMode);
intent2.putExtra("NEW_MODE",

Settings.Secure.LOCATION_MODE_SENSORS_ONLY);
mContext.sendBroadcast(intent2, android.Manifest.permission.WRITE_SECURE_SETTINGS);
Settings.Secure.setLocationProviderEnabled(mContext.getContentResolver(), LocationManager.GPS_PROVIDER,true); Settings.Secure.putInt(mContext.getContentResolver(),Settings.Secure.LOCATION_MODE,Settings.Secure.LOCATION_MODE_SENSORS_ONLY);
Log.e("GPS","=====LOCATION_MODE_ON=====");
} else {
Intent intent2 = new Intent("android.location.MODE_CHANGED");
intent2.putExtra("CURRENT_MODE", Settings.Secure.LOCATION_MODE_SENSORS_ONLY);
intent2.putExtra("NEW_MODE", Settings.Secure.LOCATION_MODE_OFF);
mContext.sendBroadcast(intent2, android.Manifest.permission.WRITE_SECURE_SETTINGS);
Settings.Secure.setLocationProviderEnabled(mContext.getContentResolver(), LocationManager.GPS_PROVIDER,false);
Settings.System.putInt(mContext.getContentResolver(), Settings.Secure.LOCATION_MODE, Settings.Secure.LOCATION_MODE_OFF);
Log.e("GPS","=====LOCATION_MODE_OFF=====");
}

}
//*/

}

};

相关推荐
SweetCode5 小时前
汉诺塔问题
android·java·数据库
音视频牛哥6 小时前
Android 端构建高性能 RTSP 转 RTMP|轻量级RTSP服务 网关:透传与二次编码深度实践
android·音视频·大牛直播sdk·rtsp转rtmp推送·rtsp转发到rtsp服务器·rtsp转rtmp二次编码推送·rtsp二次编码加水印
方白羽6 小时前
Android 16 (Target 36) 应用适配指南
android·app·客户端
微爱帮监所写信寄信7 小时前
微爱帮监狱寄信写信系统后台PHP框架优化实战手册
android·开发语言·人工智能·网络协议·微信·https·php
私人珍藏库7 小时前
[Android] 无印2.2视频解析去水印工具,支持多个平台 2025.12.29更新
android·app·安卓·工具·软件·音乐·music
雨声不在7 小时前
多进程的多语言切换
android
Yang-Never8 小时前
Android 内存泄漏 -> ViewModel持有Activity/Fragment导致的内存泄漏
android·java·开发语言·kotlin·android studio
Android_xiong_st8 小时前
(原创)Android遍历文件方法walk函数介绍
android
Yang-Never9 小时前
Android 内存泄漏 -> LiveData如何解决ViewMode和Activity/Fragment之间的内存泄漏
android·java·开发语言·kotlin·android studio
HeDongDong-9 小时前
Kotlin 协程(Coroutines)详解
android·开发语言·kotlin