Android 接收第三方APP的广播处理gps定位状态

发布于:2025-04-18 ⋅ 阅读:(47) ⋅ 点赞:(0)

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

import android.location.LocationManager;

IntentFilter filter = new IntentFilter();

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

context.registerReceiver(mDockReceiver, filter);

 BroadcastReceiver mDockReceiver = new BroadcastReceiver() {

  //*/add gps
            if (intent.getAction().equals("sys.gps.switch")) {
                int mydata =  intent.getIntExtra("gpsSwitch", 0);//1 open 2 close
                   if (mydata == 1) {
                        Intent intent2 = new Intent("com.android.settings.location.MODE_CHANGING");
                        intent2.putExtra("CURRENT_MODE", Settings.Secure.LOCATION_MODE,Settings.Secure.LOCATION_MODE_OFF);
                        intent2.putExtra("NEW_MODE", Settings.Secure.LOCATION_MODE_SENSORS_ONLY);
                        mContext.sendBroadcast(intent2, android.Manifest.permission.WRITE_SECURE_SETTINGS);
 Settings.Secure.putInt(mContext.getContentResolver(),Settings.Secure.LOCATION_MODE,Settings.Secure.LOCATION_MODE_SENSORS_ONLY);
                        Log.e("LXD","=====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("LXD","=====LOCATION_MODE_OFF=====");
                   }

            }
            //*/

客户第三方APP:

 

int value = 

Intent mIntent= new Intent();

mIntent.setAction("sys.gps.switch");//*/

mIntent.putExtra("gpsSwitch", value?1:2);

context.sendBroadcast(mIntent);


网站公告

今日签到

点亮在社区的每一天
去签到