Reputation: 99
I try to connect Location service without using Location setting but any time I have run the project it gives error. I mentioned the code below
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");
}}
Please let me with a sample coding how I can Enable location service without navigating to location service.
These are my written 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
Thanks in Advance
Upvotes: 0
Views: 103
Reputation: 1508
I tried your source code, It is working Fine.
You can also refer Source :
Location - developer.android.com
LocationUpdates - developer.android.com
Sample of location update from developer.android.com
BasicLocationSample from developer.android.com
Upvotes: 1