为什么显示gps提供程序未知。这是打开和关闭gps的正确方法吗?我需要在我的应用中使用切换gps。
这是我的程序代码
这是我的主要活动类
package com.example.gpstest;
import android.location.LocationManager;
import android.os.Bundle;
import android.widget.CompoundButton;
import android.widget.Switch;
import android.widget.Toast;
import android.app.Activity;
import android.content.Context;
public class MainActivity2 extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
Switch toggle = (Switch) findViewById(R.id.gps_switch);
toggle.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked)
{
togglegps(true);
Toast.makeText(getApplicationContext(), "GPS Enabled!", Toast.LENGTH_LONG).show();
}
else
{
togglegps(false);
Toast.makeText(getApplicationContext(), "GPS Disabled!", Toast.LENGTH_LONG).show();
}
}
});
}
public void togglegps(boolean status) {
LocationManager locationManager = (LocationManager) this.getSystemService(LOCATION_SERVICE);
if (status == true && !locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
locationManager.setTestProviderEnabled(LocationManager.GPS_PROVIDER, true);
}
else if (status == false && locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
locationManager.setTestProviderEnabled(LocationManager.GPS_PROVIDER, false);
}
}
}
logcat的
出现以下错误:
01-13 15:19:20.547: D/dalvikvm(2030): GC_FOR_ALLOC freed 50K, 7% free 2584K/2764K, paused 131ms, total 154ms
01-13 15:19:21.347: D/dalvikvm(2030): GC_FOR_ALLOC freed 3K, 6% free 3054K/3220K, paused 20ms, total 21ms
01-13 15:19:21.607: D/gralloc_goldfish(2030): Emulator without GPU emulation detected.
01-13 15:19:25.148: E/InputEventReceiver(2030): Exception dispatching input event.
01-13 15:19:25.148: D/AndroidRuntime(2030): Shutting down VM
01-13 15:19:25.158: W/dalvikvm(2030): threadid=1: thread exiting with uncaught exception (group=0xb2f09648)
01-13 15:19:25.208: E/AndroidRuntime(2030): FATAL EXCEPTION: main
01-13 15:19:25.208: E/AndroidRuntime(2030): java.lang.IllegalArgumentException: Provider "gps" unknown
01-13 15:19:25.208: E/AndroidRuntime(2030): at android.os.Parcel.readException(Parcel.java:1435)
01-13 15:19:25.208: E/AndroidRuntime(2030): at android.os.Parcel.readException(Parcel.java:1385)
01-13 15:19:25.208: E/AndroidRuntime(2030): at android.location.ILocationManager$Stub$Proxy.setTestProviderEnabled(ILocationManager.java:1012)
01-13 15:19:25.208: E/AndroidRuntime(2030): at android.location.LocationManager.setTestProviderEnabled(LocationManager.java:1269)
01-13 15:19:25.208: E/AndroidRuntime(2030): at com.example.gpstest.MainActivity2.toggleWiFi(MainActivity2.java:40)
01-13 15:19:25.208: E/AndroidRuntime(2030): at com.example.gpstest.MainActivity2$1.onCheckedChanged(MainActivity2.java:26)
01-13 15:19:25.208: E/AndroidRuntime(2030): at android.widget.CompoundButton.setChecked(CompoundButton.java:126)
01-13 15:19:25.208: E/AndroidRuntime(2030): at android.widget.Switch.setChecked(Switch.java:666)
01-13 15:19:25.208: E/AndroidRuntime(2030): at android.widget.Switch.animateThumbToCheckedState(Switch.java:645)
01-13 15:19:25.208: E/AndroidRuntime(2030): at android.widget.Switch.stopDrag(Switch.java:635)
01-13 15:19:25.208: E/AndroidRuntime(2030): at android.widget.Switch.onTouchEvent(Switch.java:595)
01-13 15:19:25.208: E/AndroidRuntime(2030): at android.view.View.dispatchTouchEvent(View.java:7384)
01-13 15:19:25.208: E/AndroidRuntime(2030): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2211)
01-13 15:19:25.208: E/AndroidRuntime(2030): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1954)
01-13 15:19:25.208: E/AndroidRuntime(2030): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2211)
01-13 15:19:25.208: E/AndroidRuntime(2030): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1954)
01-13 15:19:25.208: E/AndroidRuntime(2030): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2211)
01-13 15:19:25.208: E/AndroidRuntime(2030): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1954)
01-13 15:19:25.208: E/AndroidRuntime(2030): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2211)
01-13 15:19:25.208: E/AndroidRuntime(2030): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1954)
01-13 15:19:25.208: E/AndroidRuntime(2030): at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1966)
01-13 15:19:25.208: E/AndroidRuntime(2030): at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1418)
01-13 15:19:25.208: E/AndroidRuntime(2030): at android.app.Activity.dispatchTouchEvent(Activity.java:2424)
01-13 15:19:25.208: E/AndroidRuntime(2030): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1914)
01-13 15:19:25.208: E/AndroidRuntime(2030): at android.view.View.dispatchPointerEvent(View.java:7564)
01-13 15:19:25.208: E/AndroidRuntime(2030): at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:3883)
01-13 15:19:25.208: E/AndroidRuntime(2030): at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:3778)
01-13 15:19:25.208: E/AndroidRuntime(2030): at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3379)
01-13 15:19:25.208: E/AndroidRuntime(2030): at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3429)
01-13 15:19:25.208: E/AndroidRuntime(2030): at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3398)
01-13 15:19:25.208: E/AndroidRuntime(2030): at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3483)
01-13 15:19:25.208: E/AndroidRuntime(2030): at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3406)
01-13 15:19:25.208: E/AndroidRuntime(2030): at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:3540)
01-13 15:19:25.208: E/AndroidRuntime(2030): at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3379)
01-13 15:19:25.208: E/AndroidRuntime(2030): at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3429)
01-13 15:19:25.208: E/AndroidRuntime(2030): at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3398)
01-13 15:19:25.208: E/AndroidRuntime(2030): at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3406)
01-13 15:19:25.208: E/AndroidRuntime(2030): at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3379)
01-13 15:19:25.208: E/AndroidRuntime(2030): at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:5419)
01-13 15:19:25.208: E/AndroidRuntime(2030): at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:5399)
01-13 15:19:25.208: E/AndroidRuntime(2030): at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:5370)
01-13 15:19:25.208: E/AndroidRuntime(2030): at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:5493)
01-13 15:19:25.208: E/AndroidRuntime(2030): at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:182)
01-13 15:19:25.208: E/AndroidRuntime(2030): at android.view.InputEventReceiver.nativeConsumeBatchedInputEvents(Native Method)
01-13 15:19:25.208: E/AndroidRuntime(2030): at android.view.InputEventReceiver.consumeBatchedInputEvents(InputEventReceiver.java:174)
01-13 15:19:25.208: E/AndroidRuntime(2030): at android.view.ViewRootImpl.doConsumeBatchedInput(ViewRootImpl.java:5472)
01-13 15:19:25.208: E/AndroidRuntime(2030): at android.view.ViewRootImpl$ConsumeBatchedInputRunnable.run(ViewRootImpl.java:5512)
01-13 15:19:25.208: E/AndroidRuntime(2030): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
01-13 15:19:25.208: E/AndroidRuntime(2030): at android.view.Choreographer.doCallbacks(Choreographer.java:562)
01-13 15:19:25.208: E/AndroidRuntime(2030): at android.view.Choreographer.doFrame(Choreographer.java:530)
01-13 15:19:25.208: E/AndroidRuntime(2030): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
01-13 15:19:25.208: E/AndroidRuntime(2030): at android.os.Handler.handleCallback(Handler.java:730)
01-13 15:19:25.208: E/AndroidRuntime(2030): at android.os.Handler.dispatchMessage(Handler.java:92)
01-13 15:19:25.208: E/AndroidRuntime(2030): at android.os.Looper.loop(Lo