位置 sendExtraCommands - 方法不起作用并关闭应用程序

location sendExtraCommands - Method does not work and shutts down the app

我尝试使用 sendExtraCommand() 方法以编程方式清除智能手机的 gps 芯片。在我的清单文件中,我添加了发送额外命令的权限,如下所示:

<uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS"/>

我这样调用方法本身:

locationManager.sendExtraCommand(LocationManager.GPS_PROVIDER, "delete_aiding_data", null)

当我在模拟器或智能手机上尝试时,应用程序崩溃了。我不知道我做错了什么,所以如果有人能看看我的问题,我将不胜感激。

我唯一的activity,MainActivity:

    public class MainActivity extends AppCompatActivity {

        private TextView textView;
        private Button button;
        private LocationManager locationManager;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

            textView = (TextView) findViewById(R.id.txtView1);
            textView.setText("Gps Reset: ");

            button = (Button) findViewById(R.id.button);

            locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);

            boolean enabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
    System.out.println(enabled);

            button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    sendXtraCommand();
                }
            });

        }

        private void sendXtraCommand(){
            boolean reset = locationManager.sendExtraCommand(LocationManager.GPS_PROVIDER, "delete_aiding_data", null);
            if(reset){
                textView.append("true");
            }else{
                textView.append("false");
            }
        }
    }

这是我的清单文件:

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.felix.resetgps">

        <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
        <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
        <uses-feature android:name="android.hardware.location.gps" />
        <uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS"/>
        <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
        <uses-permission android:name="android.permission.INTERNET"/>

        <application
            android:allowBackup="true"
            android:icon="@mipmap/ic_launcher"
            android:label="@string/app_name"
            android:supportsRtl="true"
            android:theme="@style/AppTheme">
            <activity android:name=".MainActivity">
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />

                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
        </application>

    </manifest>

这是我在控制台上收到的消息:

    11/25 14:32:41: Launching app
    $ adb shell am start -n         "com.example.felix.resetgps/com.example.felix.resetgps.MainActivity" -a         android.intent.action.MAIN -c android.intent.category.LAUNCHER -D
    Waiting for application to come online: com.example.felix.resetgps | com.example.felix.resetgps.test
    Waiting for application to come online: com.example.felix.resetgps | com.example.felix.resetgps.test
    Connecting to com.example.felix.resetgps
    I/art: Not late-enabling -Xcheck:jni (already on)
    W/art: Unexpected CPU variant for X86 using defaults: x86
    W/ActivityThread: Application com.example.felix.resetgps is waiting for the debugger on port 8100...
    I/System.out: Sending WAIT chunk

          [ 11-25 13:32:41.598  1794: 1815 D/         ]
          HostConnection::get() New Host Connection established 0x8bf7f400, tid 1815
    I/art: Debugger is active
    Connected to the target VM, address: 'localhost:8615', transport: 'socket'
    I/System.out: Debugger has connected
    I/System.out: waiting for debugger to settle...
    I/System.out: waiting for debugger to settle...
    I/System.out: waiting for debugger to settle...
    I/System.out: waiting for debugger to settle...
    I/System.out: waiting for debugger to settle...
    I/System.out: waiting for debugger to settle...
    I/System.out: waiting for debugger to settle...
    I/System.out: debugger has settled (1396)
    W/System: ClassLoader referenced unknown path: /data/app/com.example.felix.resetgps-1/lib/x86
    I/InstantRun: Instant Run Runtime started. Android package is com.example.felix.resetgps, real application class is null.
    W/System: ClassLoader referenced unknown path: /data/app/com.example.felix.resetgps-1/lib/x86
    I/art: Starting a blocking GC Instrumentation
    W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
    I/System.out: true
    I/OpenGLRenderer: Initialized EGL, version 1.4
    D/OpenGLRenderer: Swap behavior 1
    D/AndroidRuntime: Shutting down VM
    E/AndroidRuntime: FATAL EXCEPTION: main
              Process: com.example.felix.resetgps, PID: 4869
              java.lang.SecurityException: "gps" location provider requires ACCESS_FINE_LOCATION permission.
                  at android.os.Parcel.readException(Parcel.java:1683)
                  at android.os.Parcel.readException(Parcel.java:1636)
                  at android.location.ILocationManager$Stub$Proxy.sendExtraCommand(ILocationManager.java:1274)
                  at android.location.LocationManager.sendExtraCommand(LocationManager.java:2226)
                  at com.example.felix.resetgps.MainActivity.sendXtraCommand(MainActivity.java:51)
                  at com.example.felix.resetgps.MainActivity.access[=12=]0(MainActivity.java:16)
                  at com.example.felix.resetgps.MainActivity.onClick(MainActivity.java:44)
                  at android.view.View.performClick(View.java:5610)
                  at android.view.View$PerformClick.run(View.java:22265)
                  at android.os.Handler.handleCallback(Handler.java:751)
                  at android.os.Handler.dispatchMessage(Handler.java:95)
                  at android.os.Looper.loop(Looper.java:154)
                  at android.app.ActivityThread.main(ActivityThread.java:6077)
                  at java.lang.reflect.Method.invoke(Native Method)
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
    Disconnected from the target VM, address: 'localhost:8615', transport: 'socket'

如果你仔细看log cat你会发现

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>

不见了。

对于Android 运行 on Android 6.0 (API level 23) 及以上,我们需要添加运行时权限才能工作。

参考这个答案来实现它: