0
我尝试连接位置服务而不使用位置设置,但任何时候我运行项目时都会出现错误。我提到下面的代码支持Android的GPS
public class Practice2 extends Activity implements LocationListener{
private static final String TAG = "Profile";
protected static final int REQUEST_CHECK_SETTINGS = 0x1;
protected LocationManager locationManager;
protected LocationListener locationListener;
protected Context context;
protected boolean gps_enabled,network_enabled;
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.practice2);
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, (android.location.LocationListener) this);
}
@Override
public void onLocationChanged(Location location) {
}
public void onProviderDisabled(String provider) {
Log.d("Latitude","disable");
}
public void onProviderEnabled(String provider) {
Log.d("Latitude","enable");
}
public void onStatusChanged(String provider, int status, Bundle extras) {
Log.d("Latitude","status");
}}
请让我带一个示例编码如何可以启用位置服务,而无需导航到位置服务。
这是我写的logcat
04-04 06:48:35.537: E/AndroidRuntime(813): FATAL EXCEPTION: main
04-04 06:48:35.537: E/AndroidRuntime(813): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.Test/com.example.Test.Practice2}: java.lang.ClassCastException: com.example.Test.Practice2 cannot be cast to android.location.LocationListener
04-04 06:48:35.537: E/AndroidRuntime(813): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
04-04 06:48:35.537: E/AndroidRuntime(813): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
04-04 06:48:35.537: E/AndroidRuntime(813): at android.app.ActivityThread.access$600(ActivityThread.java:123)
04-04 06:48:35.537: E/AndroidRuntime(813): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
04-04 06:48:35.537: E/AndroidRuntime(813): at android.os.Handler.dispatchMessage(Handler.java:99)
04-04 06:48:35.537: E/AndroidRuntime(813): at android.os.Looper.loop(Looper.java:137)
04-04 06:48:35.537: E/AndroidRuntime(813): at android.app.ActivityThread.main(ActivityThread.java:4424)
04-04 06:48:35.537: E/AndroidRuntime(813): at java.lang.reflect.Method.invokeNative(Native Method)
04-04 06:48:35.537: E/AndroidRuntime(813): at java.lang.reflect.Method.invoke(Method.java:511)
04-04 06:48:35.537: E/AndroidRuntime(813): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
04-04 06:48:35.537: E/AndroidRuntime(813): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
04-04 06:48:35.537: E/AndroidRuntime(813): at dalvik.system.NativeStart.main(Native Method)
04-04 06:48:35.537: E/AndroidRuntime(813): Caused by: java.lang.ClassCastException: com.example.Test.Practice2 cannot be cast to android.location.LocationListener
04-04 06:48:35.537: E/AndroidRuntime(813): at
com.example.Test.Practice2.onCreate(Practice2.java:45)
04-04 06:48:35.537: E/AndroidRuntime(813): at android.app.Activity.performCreate(Activity.java:4466)
04-04 06:48:35.537: E/AndroidRuntime(813): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
04-04 06:48:35.537: E/AndroidRuntime(813): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
04-04 06:48:35.537: E/AndroidRuntime(813): ... 11 more
由于提前
您将需要工具LocationListener的,你是铸造这LocationListener的。 locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,0, (android.location.LocationListener)this); – Lavakush
提供此文件的完整代码。 –
我做了所有这些,但同样的错误, – user3669034