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

}
//*/

}

};

相关推荐
coder_pig3 小时前
🤡 公司Android老项目升级踩坑小记
android·flutter·gradle
死就死在补习班4 小时前
Android系统源码分析Input - InputReader读取事件
android
死就死在补习班4 小时前
Android系统源码分析Input - InputChannel通信
android
死就死在补习班4 小时前
Android系统源码分析Input - 设备添加流程
android
死就死在补习班4 小时前
Android系统源码分析Input - 启动流程
android
tom4i5 小时前
Launcher3 to Launchpad 01 布局修改
android
雨白5 小时前
OkHttpClient 核心配置详解
android·okhttp
淡淡的香烟5 小时前
Android auncher3实现简单的负一屏功能
android
RabbitYao5 小时前
Android 项目 通过 AndroidStringsTool 更新多语言词条
android·python
RabbitYao6 小时前
使用 Gemini 及 Python 更新 Android 多语言 Excel 文件
android·python