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

}
//*/

}

};

相关推荐
小蜜蜂嗡嗡1 小时前
Android Studio flutter项目运行、打包时间太长
android·flutter·android studio
aqi001 小时前
FFmpeg开发笔记(七十一)使用国产的QPlayer2实现双播放器观看视频
android·ffmpeg·音视频·流媒体
zhangphil3 小时前
Android理解onTrimMemory中ComponentCallbacks2的内存警戒水位线值
android
你过来啊你3 小时前
Android View的绘制原理详解
android
移动开发者1号6 小时前
使用 Android App Bundle 极致压缩应用体积
android·kotlin
移动开发者1号6 小时前
构建高可用线上性能监控体系:从原理到实战
android·kotlin
ii_best11 小时前
按键精灵支持安卓14、15系统,兼容64位环境开发辅助工具
android
美狐美颜sdk11 小时前
跨平台直播美颜SDK集成实录:Android/iOS如何适配贴纸功能
android·人工智能·ios·架构·音视频·美颜sdk·第三方美颜sdk
恋猫de小郭15 小时前
Meta 宣布加入 Kotlin 基金会,将为 Kotlin 和 Android 生态提供全新支持
android·开发语言·ios·kotlin
aqi0016 小时前
FFmpeg开发笔记(七十七)Android的开源音视频剪辑框架RxFFmpeg
android·ffmpeg·音视频·流媒体