Pikaa Pikaa - 3 months ago 10
Java Question

Jump to activity without button click

I am trying to go to next activity. After checking if the GPS is enabled
without any button click I am trying to jump to next activity.
But its not working

Is there any way to jump to nexy activity after checking if GPS is enabled
or after turning on GPS.

Help

Thanks.

Main.java

package app.example.joy;

import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.Button;
import android.app.Activity;
import android.content.Intent;

public class Main extends Activity {

private LocationManager manager;
private LocationListener listner;

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);


manager = (LocationManager) getSystemService(LOCATION_SERVICE);
listner = new LocationListener() {

@Override
public void onStatusChanged(String s, int i, Bundle bundle) {

// TODO Auto-generated method stub

}

@Override
public void onProviderEnabled(String s) {

Intent i = new Intent(this, NextActivity.class);
startActivity(i);


}

@Override
public void onProviderDisabled(String s) {
// TODO Auto-generated method stub
Intent i = new Intent(
android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(i);

}

@Override
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub

float latitude = (float) location.getLatitude();
float longitude = (float) location.getLongitude();

Intent i = new Intent(Main.this, NextActivity.class);
i.putExtra("Long", longitude);
i.putExtra("Lat", latitude);
startActivity(i);

}

};

}

}

Answer

I think the problem is that you've forgotten to even use the LocationManager that you instantiated. Try adding these two lines of code after instantiating the listener (at the end of your onCreate):

manager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, listener);
manager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, listener);

These will register for location updates using the named provider, and a pending intent.

Comments