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=====");
}

}
//*/

}

};

相关推荐
酷在前行7 分钟前
【R绘图】Nature Communications 半眼图复刻:分布、区间与多面板排版(保姆级教程)
android·开发语言·r语言
2501_9327502642 分钟前
Android registerForActivityResult 详解
android
小孔龙3 小时前
VSync 与同步屏障:doFrame() 的优先调度
android·性能优化
kfzjw0083 小时前
Android 的一帧完整渲染流程:从 VSYNC 到 Screen 的全链路解密
android
wWYy.4 小时前
Mysql:覆盖索引
android·数据库·mysql
Kapaseker4 小时前
如果我要从 List 中获取一段元素
android·kotlin
三少爷的鞋4 小时前
一次朋友圈发布流程,理解 Kotlin 协程为什么重新定义异步代码组织方式
android
FungLeo5 小时前
Flutter/Android Release 包连不上网?AndroidManifest INTERNET 权限排查实录
android·flutter
wWYy.7 小时前
Mysql:一行数据是怎么存储的?
android·数据库·mysql
漏刻有时14 小时前
PHP GeoJSON转PNG地图渲染程序开发笔记、源码解读、问题复盘与整改方案
android·笔记·php