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

}
//*/

}

};

相关推荐
与籍同行40 分钟前
开发过程中遇到Selinux问题分析
android·selinux
韩仔搭建1 小时前
安卓端互动娱乐房卡系统调试实录:从UI到协议的万字深拆(第一章)
android·ui·娱乐
limingade2 小时前
手机打电话时如何将通话对方的声音在手机上识别成文字
android·智能手机·语音识别·funasr·蓝牙电话·ai电话机器人·funasr安卓移植和部署
努力学习的小廉3 小时前
深入了解linux系统—— 基础IO(上)
android·linux·运维
tmacfrank3 小时前
Android 性能优化入门(一)—— 数据结构优化
android·数据结构·性能优化
东风西巷3 小时前
Screen Mirroring App:轻松实现手机与电视的无缝投屏
android·智能手机·生活·软件需求
每次的天空4 小时前
Android第三次面试总结之网络篇补充
android·网络·面试
uwvwko16 小时前
BUUCTF——web刷题第一页题解
android·前端·数据库·php·web·ctf
fzxwl16 小时前
隆重推荐(Android 和 iOS)UI 自动化工具—Maestro
android·ui·ios
LittleLoveBoy18 小时前
踩坑:uiautomatorviewer.bat 打不开
android