I have a problem with setting GPS service . I want to check if GPS is enable . If it is disable , it will display a dialog to user turn it on . The problem is it not wait for user enable GPS and get location .
I have a class
public class GPSTracker extends Service implements LocationListener{
private final Context mContext;
boolean isGPSEnable = false;
boolean isNetworkEnable = false;
boolean canGetLocation = false;
Location location;
double latitude;
double longitude;
private static final long MIN_DISTANCE_FOR_UPDATE = 10; // 10 meters
private static final long MIN_TIME_BW_UPDATE = 1000 * 60 * 1; // 1 minute
protected LocationManager locationManager;
public GPSTracker(Context ctx) {
this.mContext = ctx;
getLocation();
}
public Location getLocation()
{
try {
locationManager = (LocationManager)mContext.getSystemService(LOCATION_SERVICE);
isGPSEnable = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
isNetworkEnable = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if(!isGPSEnable)
{
showSettingsGPSAlert();
}
else if(!isNetworkEnable)
{
showSettingsNetWorkAlert();
}
else
{
this.canGetLocation = true;
if(isNetworkEnable)
{
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,
MIN_TIME_BW_UPDATE,
MIN_DISTANCE_FOR_UPDATE, this);
Log.d("Network", "Network");
if (locationManager != null) {
location = locationManager
.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
}
}
}
if (isGPSEnable) {
if (location == null) {
locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER,
MIN_TIME_BW_UPDATE,
MIN_DISTANCE_FOR_UPDATE, this);
Log.d("GPS Enabled", "GPS Enabled");
if (locationManager != null) {
location = locationManager
.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
}
}
}
}
}
}catch (Exception ex)
{
Log.e("<<Location Error>>",ex.getMessage());
}
return location;
}
public void stopUsingGPS(){
if(locationManager != null){
locationManager.removeUpdates(GPSTracker.this);
}
}
public double getLatitude(){
if(location != null){
latitude = location.getLatitude();
}
// return latitude
return latitude;
}
public double getLongitude(){
if(location != null){
longitude = location.getLongitude();
}
// return longitude
return longitude;
}
public boolean canGetLocation() {
return this.canGetLocation;
}
public boolean canGetGPS() {
return this.isGPSEnable;
}
public boolean canGetNetwork() {
return this.isNetworkEnable;
}
public void showSettingsGPSAlert(){
AlertDialog.Builder alertDialog = new AlertDialog.Builder(mContext);
// Setting Dialog Title
alertDialog.setTitle("GPS is settings");
// Setting Dialog Message
alertDialog.setMessage("GPS is not enabled. Do you want to go to settings menu?");
// On pressing Settings button
alertDialog.setPositiveButton("Settings", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int which) {
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
mContext.startService(intent);
}
});
// on pressing cancel button
alertDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
// Showing Alert Message
alertDialog.show();
}
public void showSettingsNetWorkAlert(){
AlertDialog.Builder alertDialog = new AlertDialog.Builder(mContext);
// Setting Dialog Title
alertDialog.setTitle("NetWork is settings");
// Setting Dialog Message
alertDialog.setMessage("NetWork is not enabled. Do you want to go to settings menu?");
// On pressing Settings button
alertDialog.setPositiveButton("Settings", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int which) {
Intent intent = new Intent(Settings.ACTION_NETWORK_OPERATOR_SETTINGS);
mContext.startActivity(intent);
}
});
// on pressing cancel button
alertDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
// Showing Alert Message
alertDialog.show();
}
#Override
public IBinder onBind(Intent intent) {
return null;
}
#Override
public void onLocationChanged(Location location) {
}
#Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
#Override
public void onProviderEnabled(String provider) {
}
#Override
public void onProviderDisabled(String provider) {
}
}
And I have class to use it
public class RestaurantListFragment extends ListFragment {
private ArrayList<Restaurant> restaurants;
private SQLDataHelper dataHelper;
private GPSTracker gps;
private double UserLatitude;
private double UserLongitude;
private ProgressDialog progressBar;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.restaurant_list, null);
}
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
gps = new GPSTracker(getActivity());
UserLatitude = gps.getLatitude();
UserLongitude = gps.getLongitude();
Toast.makeText(getActivity(),UserLatitude + "|" + UserLongitude,Toast.LENGTH_SHORT).show();
dataHelper = new SQLDataHelper(getActivity(), "restaurantDB");
restaurants = new ArrayList<Restaurant>();
dataHelper.openDB();
Cursor cursor = dataHelper.query("Restaurant", new String[]{"Id", "ResName", "Logo", "Address", "Latitude", "Longitude"}, null
, null, null, null, null);
if (cursor.moveToFirst()) {
do {
Restaurant restaurant = new Restaurant();
restaurant.setId(cursor.getInt(0));
restaurant.setResName(cursor.getString(1));
restaurant.setLogo(cursor.getString(2));
restaurant.setAddress(cursor.getString(3));
restaurants.add(restaurant);
} while (cursor.moveToNext());
}
//Collections.sort(restaurants);
RestaurantAdapter restaurantAdapter = new RestaurantAdapter(getActivity(),restaurants);
setListAdapter(restaurantAdapter);
}
}
It works fine when GPS is enable . I want the user can turn it on first . After that it show user location . Then it can query database and setListAdapter . Thanks for any suggestion .
Related
[![enter image description here][1]][1]HI all I am having a problem can anyone tell me how do I resolve this , I want to use a constructer in fragment class:
here is my both classes
AppLocationService:
public class AppLocationService extends Service implements LocationListener {
protected LocationManager locationManager;
Location location;
private static final long MIN_DISTANCE_FOR_UPDATE = 10;
private static final long MIN_TIME_FOR_UPDATE = 1000 * 60 * 2;
public AppLocationService(Context context) {
locationManager = (LocationManager) context
.getSystemService(LOCATION_SERVICE);
}
public Location getLocation(String provider) {
if (locationManager.isProviderEnabled(provider)) {
locationManager.requestLocationUpdates(provider,
MIN_TIME_FOR_UPDATE, MIN_DISTANCE_FOR_UPDATE, this);
if (locationManager != null) {
location = locationManager.getLastKnownLocation(provider);
return location;
}
}
return null;
}
#Override
public void onLocationChanged(Location location) {
}
#Override
public void onProviderDisabled(String provider) {
}
#Override
public void onProviderEnabled(String provider) {
}
#Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
#Override
public IBinder onBind(Intent arg0) {
return null;
}
}
here is fragment:
public class AddressFragment extends Fragment {
Button ShowAddress;
TextView tvAddress;
AppLocationService appLocationService;
public AddressFragment(){
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_address, container, false);
tvAddress = (TextView) rootView.findViewById(R.id.input_location);
appLocationService = new AppLocationService(
AddressFragment.this);
ShowAddress = (Button) rootView.findViewById(R.id.btn_location);
ShowAddress.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
Location location = appLocationService
.getLocation(LocationManager.GPS_PROVIDER);
//you can hard-code the lat & long if you have issues with getting it
//remove the below if-condition and use the following couple of lines
//double latitude = 37.422005;
//double longitude = -122.084095
if (location != null) {
double latitude = location.getLatitude();
double longitude = location.getLongitude();
LocationAddress locationAddress = new LocationAddress();
locationAddress.getAddressFromLocation(latitude, longitude,
getApplicationContext(), new GeocoderHandler());
} else {
showSettingsAlert();
}
}
});
return rootView;
}
public void showSettingsAlert() {
AlertDialog.Builder alertDialog = new AlertDialog.Builder(
AddressFragment.this);
alertDialog.setTitle("SETTINGS");
alertDialog.setMessage("Enable Location Provider! Go to settings menu?");
alertDialog.setPositiveButton("Settings",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Intent intent = new Intent(
Settings.ACTION_LOCATION_SOURCE_SETTINGS);
AddressFragment.this.startActivity(intent);
}
});
alertDialog.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
alertDialog.show();
}
private class GeocoderHandler extends Handler {
#Override
public void handleMessage(Message message) {
String locationAddress;
switch (message.what) {
case 1:
Bundle bundle = message.getData();
locationAddress = bundle.getString("address");
break;
default:
locationAddress = null;
}
tvAddress.setText(locationAddress);
}
}
// Inflate the layout for this fragment
#Override
public void onAttach(Activity activity) {
super.onAttach(activity);
}
#Override
public void onDetach() {
super.onDetach();
}
}
To answer your question; A Fragment does not derive from Context. To let your code compile, you should change this
appLocationService = new AppLocationService(
AddressFragment.this);
To this
appLocationService = new AppLocationService(getActivity());
However, you should never call a constructor of a Service yourself. You should start it with Context#startService(Intent) (in your case getActivity().startService(new Intent(getActivity(), AppLocationService.class)). To supply any parameters for your service, take a look at Intent#putExtra(String, String)
I have created a custom layout for my alertdiaog : alertdialog.setView(inflater.inflate(R.layout.custom_gps,null));
I want to replace the default Ok and cancel buttons with my buttons, since it's not possible to use findViewById in a service class, I wanted to know if there is a workaround to handle the clicks on my custom buttons.
I took a look at some old questions but i did not find (yet) any trick to make that happen. Can you guys help ?
I have another workaround on my mind, and it is to extend Activity instead of Service (whice will make findViewByID available, but what changes do i have to apply on my class to start a Service from an activity ?
Any help or indication is welcome !
public class GPSTracker extends Service implements LocationListener {
private final Context mcontext;
boolean isGPSEnabled = false;
boolean isNetworkEnabled = false;
boolean canGetLocation = false;
Location location;
double latitude,longitude;
private Button ok_button,cancel_button;
//the minimum distance to change updates in meters :
private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 10;
//the minimum time between updates (milliseconds) :
private static final long MIN_TIME_BETWEEN_UPDATE = 600000; // 10min
protected LocationManager locationmanager;
public GPSTracker (Context context){
this.mcontext = context;
getLocation();
}
public Location getLocation(){
try {
locationmanager = (LocationManager) mcontext
.getSystemService(LOCATION_SERVICE);
// getting GPS status
isGPSEnabled = locationmanager
.isProviderEnabled(LocationManager.GPS_PROVIDER);
// getting network status
isNetworkEnabled = locationmanager
.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if (!isGPSEnabled && !isNetworkEnabled) {
// no network provider is enabled
} else {
this.canGetLocation = true;
// First get location from Network Provider
if (isNetworkEnabled) {
locationmanager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER,
MIN_TIME_BETWEEN_UPDATE,
MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
Log.d("Network", "Network");
if (locationmanager != null) {
location = locationmanager
.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
}
}
}
// if GPS Enabled get lat/long using GPS Services
if (isGPSEnabled) {
if (location == null) {
locationmanager.requestLocationUpdates(
LocationManager.GPS_PROVIDER,
MIN_TIME_BETWEEN_UPDATE,
MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
Log.d("GPS Enabled", "GPS Enabled");
if (locationmanager != null) {
location = locationmanager
.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
}
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
return location;
}
/*
function to get latitude :
*/
public double getLatitude(){
if(location != null){
latitude = location.getLatitude();
}
return latitude;
}
/*
function to get longitude :
*/
public double getLongitude(){
if (location != null){
longitude = location.getLongitude();
}
return longitude;
}
/**
*
* function to check if gps is enabled
* #return boolean
*
*/
public boolean canGetlocation(){
return this.canGetLocation;
}
/*
function to show settings alert.
*/
public void showSettingsAlert(){
AlertDialog.Builder alertdialog = new AlertDialog.Builder(mcontext);
LayoutInflater inflater = LayoutInflater.from(mcontext);
inflater = (LayoutInflater)mcontext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
alertdialog.setView(inflater.inflate(R.layout.custom_gps,null));
/*alertdialog.setPositiveButton("Settings", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
mcontext.startActivity(intent);
}
});
// on pressing cancel button
/* alertdialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
*/
alertdialog.show();
}
public void stopUsingGPS(){
if(locationmanager != null){
locationmanager.removeUpdates(GPSTracker.this);
}
}
}
You can create a simple class in which you have a method which show the dialog and you need to call that method very simple.ex-
public class AlertDialog {
public static void showdialog(Context context) {
// custom dialog
final Dialog dialog = new Dialog(context);
dialog.setContentView(R.layout.custom);
dialog.setTitle("Title...");
// set the custom dialog components - text, image and button
TextView text = (TextView) dialog.findViewById(R.id.text);
text.setText("Android custom dialog example!");
ImageView image = (ImageView) dialog.findViewById(R.id.image);
image.setImageResource(R.drawable.ic_launcher);
Button dialogButton = (Button) dialog.findViewById(R.id.dialogButtonOK);
// if button is clicked, close the custom dialog
dialogButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.show();
}
}
now in your service you can directly call this method by passing the context.
like-
AlertDialog.showdialog(context);
The GPS class and MainActivity both worked fine until we tried to add a second page. We've tried using intents and fragments and have spent way too many hours/days on trying to figure this out.
We keep getting NullPointerExceptions for either the onClickListener() in MainActivity and when updating any textviews from the GPS class. We know it has something to do with how the fragments work but we're not sure how to fix this.
This is our MainActivity class
public class MainActivity extends ActionBarActivity {
// Get buttons and text fields
public Button showLocation;
// TextViews
public static TextView showLatitude;
public static TextView showLongitude;
public static TextView showDistance;
// GPS class
GPS gps;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment())
.commit();
}
showLocation = (Button) findViewById(R.id.btnShowLocation);
showLatitude = (TextView) findViewById(R.id.txtLatitude);
showLongitude = (TextView) findViewById(R.id.txtLongitude);
showDistance = (TextView) findViewById(R.id.txtDisplayDistance);
// Create gps
gps = new GPS(MainActivity.this);
// show location button click event
showLocation.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
gps.setTotalDistance(0.0);
}
});//END LISTENER
}// END ONCREATE
public void nextPage(View view) {
// In response to button
Intent intent = new Intent(this, ResourcePage.class);
// Set activity intent
startActivity(intent);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
return rootView;
}
}
}
And this is the GPS class
public class GPS extends Service implements LocationListener {
private final Context mContext;
// Flag for GPS/Network/Location status
boolean isGPSEnabled = false;
boolean isNetworkEnabled = false;
boolean canGetLocation = false;
// Location Class
Location location;
// Latitude and longitude save points
private double startLatitude = 0;
private double endLatitude = 1.0;
private double startLongitude = 0;
private double endLongitude = 1.0;
private double totalDistance = 0;
// Results Array
private float[] results = new float[1];
// The minimum distance to change Updates in meters
private static final long MIN_DISTANCE_FOR_UPDATES = 10; // meters
// The minimum time between updates in milliseconds
private static final long MIN_TIME_BETWEEN_UPDATES = 1000; // 1 second
// Location Manager
protected LocationManager locationManager;
// Interface
public GPS(Context context) {
this.mContext = context;
getLocation();
}
public Location getLocation() {
try {
locationManager = (LocationManager) mContext.getSystemService(LOCATION_SERVICE);
// Getting GPS status
isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
// Getting network status
isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
// Checking Flags
if (!isGPSEnabled && !isNetworkEnabled) {
// ^no network provider is enabled do nothing/go on^
}
else {
this.canGetLocation = true;
// First get location from Network Provider
if (isNetworkEnabled) {
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, MIN_TIME_BETWEEN_UPDATES, MIN_DISTANCE_FOR_UPDATES, this);
Log.d("Network", "Network");
if (locationManager != null) {
location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (location != null) {
startLatitude = location.getLatitude();
startLongitude = location.getLongitude();
} //END OF LAST IF BLOCK
} //END OF SECOND IF BLOCK
} //END OF "FIRST" IF BLOCK
// if GPS Enabled get latitude/longitude using GPS Services
if (isGPSEnabled) {
if (location == null) {
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, MIN_TIME_BETWEEN_UPDATES, MIN_DISTANCE_FOR_UPDATES, this);
Log.d("GPS Enabled", "GPS Enabled");
if (locationManager != null) {
location = locationManager
.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location != null) {
startLatitude = location.getLatitude();
startLongitude = location.getLongitude();
}
}
}
}
}//END OF ELSE BLOCK
} catch (Exception e) {
e.printStackTrace();
}
return location;
}
// Call to remove updates from listener
public void stopUsingGPS(){
if(locationManager != null){
locationManager.removeUpdates(GPS.this);
}
}
public double getLatitude(){
if(location != null){
startLatitude = location.getLatitude();
}
// Return latitude
return startLatitude;
}
public double getLongitude(){
if(location != null){
startLongitude = location.getLongitude();
}
// Return longitude
return startLongitude;
}
//Function to check GPS/wifi are enabled
public boolean canGetLocation() {
//return boolean
return this.canGetLocation;
}
// Function to show settings alert dialog if gps is off
public void showSettingsAlert(){
// On pressing Settings button will launch Settings Options
AlertDialog.Builder alertDialog = new AlertDialog.Builder(mContext);
// Setting Dialog Title
alertDialog.setTitle("GPS settings");
// Setting Dialog Message
alertDialog.setMessage("GPS is not enabled. Do you want to go to settings menu?");
// Settings button listener/press
alertDialog.setPositiveButton("Settings", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int which) {
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
mContext.startActivity(intent);
}
});
// on pressing cancel button
alertDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
// Showing Alert Message
alertDialog.show();
}
// Called from listener on gps change of X
#Override
public void onLocationChanged(Location location) {
//Display Latitude and Longitude
if(location != null){
startLatitude = location.getLatitude();
startLongitude = location.getLongitude();
MainActivity.showLatitude.setText(String.valueOf(startLatitude));
MainActivity.showLongitude.setText(String.valueOf(startLongitude));
}
// Get distance between
Location.distanceBetween(startLatitude, startLongitude, endLatitude,endLongitude, results);
// Convert to miles Add distance to total
totalDistance = totalDistance + results[0] * 0.000621371192;
// Display the total
MainActivity.showDistance.setText(String.format("%.2f", totalDistance));
// Move old values to End locations
endLatitude = startLatitude;
endLongitude = startLongitude;
}
// Function to clear total distance (Testing)
public void setTotalDistance(Double savedData){
totalDistance = savedData;
MainActivity.showDistance.setText(String.valueOf(totalDistance));
}
public double getTotalDistance(){
return totalDistance;
}
#Override
public void onProviderDisabled(String provider) {
}
#Override
public void onProviderEnabled(String provider) {
}
#Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
#Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
}
For the layout files we have activity_main.xml which is empty, fragment_main.xml which has all our text fields and buttons and then fragment_resource.xml which has nothing but a textview so we knew things would appear on it.
If you say all your views belong to the fragment then initialize views in fragment not in Activity.
The Activity layout does not have the views and findViewById looks for a view in the current inflated layout. So the initialization fails leading to NullPointerException when you use the view objects.
TextView showLatitude;
TextView showLongitude;
TextView showDistance;
GPS gps;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
showLocation = (Button)rootView.findViewById(R.id.btnShowLocation);
showLatitude = (TextView) rootView.findViewById(R.id.txtLatitude);
showLongitude = (TextView) rootView.findViewById(R.id.txtLongitude);
showDistance = (TextView) rootView.findViewById(R.id.txtDisplayDistance);
gps = new GPS(getActivity()); // wrong bind the service to the activtiy
showLocation.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
gps.setTotalDistance(0.0); // remove this
}
});
return rootView;
}
Also you have
public class GPS extends Service
Its a Service class. You need to bind the service to Activity. But you dogps = new GPS(getActivity());
Replace this:
setContentView(R.layout.activity_main);
By:
setContentView(R.layout.fragment_main);
You have the problem in this method
public void setTotalDistance(Double savedData){
totalDistance = savedData;
MainActivity.showDistance.setText(String.valueOf(totalDistance));
}
You can't just call the MainActivity that way it will return NULL
Pointer exception for showDistance Variable.Because its not a static
class.So you want to do like this.
public void setTotalDistance(Double savedData){
totalDistance = savedData;
((this.mContext)MainActivity).showDistance.setText(String.valueOf(totalDistance));
}
Surely this will make some sense.
I cannot seem to get my current location. In the second half of this dialog I call a class called MyLocation. I have never had any trouble with this class before. Now I have tried several differnt approachs to no avail. But I do beleive my trouble is with the context.
This is the first time using this in an AlertDialog in a Fragment. I think its this line giving me trouble.
myLocation.getLocation(context, locationResult);
I define context in my public View onCreateView
context = getActivity().getApplicationContext();
The alert is working as intended just the public void gotLocation does not seem to be getting called.
public void showLocationDialog() {
new AlertDialog.Builder(this.getActivity()).setItems(C,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialoginterface, int i) {
if (i == 0) {
LayoutInflater inflater = LayoutInflater
.from(getActivity());
final View textenter = inflater.inflate(
R.layout.dialog_location, null);
final EditText userinput = (EditText) textenter
.findViewById(R.id.etLocation);
final AlertDialog.Builder builder = new AlertDialog.Builder(
getActivity());
builder.setView(textenter).setTitle(
"Enter Location or where abouts");
builder.setPositiveButton("Set",
new DialogInterface.OnClickListener() {
#Override
public void onClick(
DialogInterface dialog, int id) {
if (userinput != null){
tvLocation.setText(userinput.getText().toString());
dialog.cancel();
} else {
dialog.cancel();
showLocationDialog();
Log.e("NULL", "Dismiss");
}
}
}).setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(
DialogInterface dialog, int id) {
dialog.cancel();
}
});
builder.show();
} else if (i == 1) {
LocationResult locationResult = new LocationResult() {
#Override
public void gotLocation(Location location) {
// Got the location!
location.getLatitude();
location.getLongitude();
String loc = location.getLatitude() + ","
+ location.getLongitude();
etLocation.setText(loc);
}
};
MyLocation myLocation = new MyLocation();
myLocation.getLocation(context, locationResult);
}
}
}).show();
}
MyLocation:
public class MyLocation {
Timer timer1;
LocationManager lm;
LocationResult locationResult;
boolean gps_enabled = false;
boolean network_enabled = false;
public boolean getLocation(Context context, LocationResult result) {
// I use LocationResult callback class to pass location value from
// MyLocation to user code.
locationResult = result;
if (lm == null)
lm = (LocationManager) context
.getSystemService(Context.LOCATION_SERVICE);
// exceptions will be thrown if provider is not permitted.
try {
gps_enabled = lm.isProviderEnabled(LocationManager.GPS_PROVIDER);
} catch (Exception ex) {
}
try {
network_enabled = lm
.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
} catch (Exception ex) {
}
// don't start listeners if no provider is enabled
if (!gps_enabled && !network_enabled)
return false;
if (gps_enabled)
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0,
locationListenerGps);
if (network_enabled)
lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0,
locationListenerNetwork);
timer1 = new Timer();
timer1.schedule(new GetLastLocation(), 20000);
return true;
}
LocationListener locationListenerGps = new LocationListener() {
public void onLocationChanged(Location location) {
timer1.cancel();
locationResult.gotLocation(location);
lm.removeUpdates(this);
lm.removeUpdates(locationListenerNetwork);
}
public void onProviderDisabled(String provider) {
}
public void onProviderEnabled(String provider) {
}
public void onStatusChanged(String provider, int status, Bundle extras) {
}
};
LocationListener locationListenerNetwork = new LocationListener() {
public void onLocationChanged(Location location) {
timer1.cancel();
locationResult.gotLocation(location);
lm.removeUpdates(this);
lm.removeUpdates(locationListenerGps);
}
public void onProviderDisabled(String provider) {
}
public void onProviderEnabled(String provider) {
}
public void onStatusChanged(String provider, int status, Bundle extras) {
}
};
class GetLastLocation extends TimerTask {
#Override
public void run() {
lm.removeUpdates(locationListenerGps);
lm.removeUpdates(locationListenerNetwork);
Location net_loc = null, gps_loc = null;
if (gps_enabled)
gps_loc = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (network_enabled)
net_loc = lm
.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
// if there are both values use the latest one
if (gps_loc != null && net_loc != null) {
if (gps_loc.getTime() > net_loc.getTime())
locationResult.gotLocation(gps_loc);
else
locationResult.gotLocation(net_loc);
return;
}
if (gps_loc != null) {
locationResult.gotLocation(gps_loc);
return;
}
if (net_loc != null) {
locationResult.gotLocation(net_loc);
return;
}
locationResult.gotLocation(null);
}
}
public static abstract class LocationResult {
public abstract void gotLocation(Location location);
}
}
Link your fragment with your dialog.
In YourFragment.java:
public class YourFragment extends Fragment {
YourFragment mYourFragment;
#Override
protected void onCreate(Bundle savedInstanceState) {
mYourFragment = this;
...
}
}
In YourDialog.java:
public class ClearDialogModern extends DialogFragment {
YourFragment mContent;
...
public void setContent(YourFragment content) {
mContent = content;
}
On creation of YourDialog:
newYourDialog = new YourDialog();
newYourDialog.setContent(mContent);
How to getLocation from YourFragment.java inside YourDialog.java:
mContent.getLocation();
i have a main activity whit navigation drawer (whit frame layout)
private void selectItem(int position) {
// Getting reference to the FragmentManager
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction ft = fragmentManager.beginTransaction();
switch (position) {
case 0:
MapFragment mapFragment= new MapFragment();
/**
// Creating a Bundle object
Bundle data = new Bundle();
// Setting the index of the currently selected item of mDrawerList
data.putInt("position", position);
// Setting the position to the fragment
mapFragment.setArguments(data);
// Creating a fragment transaction
ft = fragmentManager.beginTransaction();
*/
// Adding a fragment to the fragment transaction
ft.replace(R.id.content_frame, mapFragment);
ft.commit();
break;
case 1:
GpsDatiFragment gpsFragment= new GpsDatiFragment();
ft.replace(R.id.content_frame,gpsFragment);
ft.commit();
break;
case 2:
AltroFragment altroFragment= new AltroFragment();
ft.replace(R.id.content_frame, altroFragment);
ft.commit();
break;
case 3:
finish();
default:
break;
}
here all ok, In One fragment there is the map and in another there is a details of gps and location (altitude, latitude, time to fix ecc)....then i have create a service that implement location listener and i'll want that it send info of onlocationchanded and gpsstatus to fragment 1 (map whit location) and 2 (altitude speed latitude time to fix) but i dont know how to do.....how sync the info for both fragment??? thanks
edit:
this is my service
public class GPSTracker extends Service implements LocationListener {
private final Context mContext;
String provider;
Criteria criteria;
// flag for GPS status
boolean isGPSEnabled = false;
// flag for network status
boolean isNetworkEnabled = false;
// flag for GPS status
boolean canGetLocation = false;
Location location; // location
double latitude; // latitude
double longitude; // longitude
// The minimum distance to change Updates in meters
private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 1; // 10 meters
// The minimum time between updates in milliseconds
private static final long MIN_TIME_BW_UPDATES = 400; // 1 minute
// Declaring a Location Manager
protected LocationManager locationManager;
public GPSTracker(Context context) {
this.mContext = context;
getLocation();
}
public Location getLocation() {
try {
locationManager = (LocationManager) mContext
.getSystemService(LOCATION_SERVICE);
// getting GPS status
isGPSEnabled = locationManager
.isProviderEnabled(LocationManager.GPS_PROVIDER);
// getting network status
isNetworkEnabled = locationManager
.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
//locationManager.addGpsStatusListener(this);
//Criteria criteria= new Criteria();
//criteria.setAccuracy(Criteria.ACCURACY_FINE);
//provider= locationManager.getBestProvider(criteria, false);
//location= locationManager.getLastKnownLocation(provider);
//locationManager.requestLocationUpdates(provider, 400, 1, this);
if (isGPSEnabled){
if (location == null) {
locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER,
MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
Log.d("GPS Enabled", "GPS Enabled");
if (locationManager != null) {
location = locationManager
.getLastKnownLocation(LocationManager.GPS_PROVIDER);
}
}else{
locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER,
MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
}
}else if (isNetworkEnabled) {
locationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER,
MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
Log.d("Network", "Network");
if (locationManager != null) {
location = locationManager
.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
}
}
} catch (Exception e) {
e.printStackTrace();
}
return location;
}
/**
* Stop using GPS listener
* Calling this function will stop using GPS in your app
* */
public void stopUsingGPS(){
if(locationManager != null){
locationManager.removeUpdates(GPSTracker.this);
}
}
/**
* Function to get latitude
* */
public double getLatitude(){
if(location != null){
latitude = location.getLatitude();
}
// return latitude
return latitude;
}
/**
* Function to get longitude
* */
public double getLongitude(){
if(location != null){
longitude = location.getLongitude();
}
// return longitude
return longitude;
}
/**
* Function to check GPS/wifi enabled
* #return boolean
* */
/*
public boolean canGetLocation() {
return this.canGetLocation;
}*/
/**
* Function to show settings alert dialog
* On pressing Settings button will lauch Settings Options
* */
public void showSettingsAlert(){
AlertDialog.Builder alertDialog = new AlertDialog.Builder(mContext);
// Setting Dialog Title
alertDialog.setTitle("GPS is settings");
// Setting Dialog Message
alertDialog.setMessage("GPS is not enabled. Do you want to go to settings menu?");
// On pressing Settings button
alertDialog.setPositiveButton("Settings", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int which) {
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
mContext.startActivity(intent);
}
});
// on pressing cancel button
alertDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
// Showing Alert Message
alertDialog.show();
}
#Override
public void onLocationChanged(Location location) {
//getLocation();
}
#Override
public void onProviderDisabled(String provider) {
getLocation();
//locationManager.requestLocationUpdates(locationManager.NETWORK_PROVIDER, 400, 1, this);
}
#Override
public void onProviderEnabled(String provider) {
getLocation();
}
#Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
#Override
public IBinder onBind(Intent arg0) {
return null;
}
}
and this is my fragment for map:
public class MapFragment extends SupportMapFragment {
GoogleMap mapView;
Intent intent;
// GPSTracker class
GPSTracker gps;
Location location ;
#Override
public void onCreate(Bundle arg0) {
super.onCreate(arg0);
// create class object
getActivity().startService(new Intent(getActivity(), GPSTracker.class));
}
#Override
public View onCreateView(LayoutInflater mInflater, ViewGroup arg1,
Bundle arg2) {
return super.onCreateView(mInflater, arg1, arg2);
}
#Override
public void onInflate(Activity arg0, AttributeSet arg1, Bundle arg2) {
super.onInflate(arg0, arg1, arg2);
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
gps = new GPSTracker(getActivity());
// check if GPS enabled
//location = gps.getLocation();
double latitude = gps.getLatitude();
double longitude = gps.getLongitude();
mapView = getMap();
MarkerOptions markerOptions = new MarkerOptions();
markerOptions.draggable(true);
markerOptions.position(new LatLng(latitude, longitude));
markerOptions.icon(BitmapDescriptorFactory.defaultMarker());
mapView.addMarker(markerOptions);
// \n is for new line
Context context = getActivity().getApplicationContext();
Toast.makeText(context, "Your Location is - \nLat: " + latitude + "\nLong: " + longitude, Toast.LENGTH_LONG).show();
// can't get location
// GPS or Network is not enabled
// Ask user to enable GPS/network in settings
// gps.showSettingsAlert();
}
}
this is fragment for deatils:
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.activity_main, container, false);
longitudeField= (TextView) rootView.findViewById(R.id.textView4);
altitudine= (TextView) rootView.findViewById(R.id.textView6);
precisione= (TextView) rootView.findViewById(R.id.textView8);
speed= (TextView) rootView.findViewById(R.id.textView10);
timeFix= (TextView) rootView.findViewById(R.id.textView12);
satFixed= (TextView) rootView.findViewById(R.id.textView14);
elencoSat= (TextView) rootView.findViewById(R.id.textView15);
latitudeField= (TextView) rootView.findViewById(R.id.textView2);
gps= new GPSTracker(getActivity());
update();
return rootView;
}
/* Request updates at startup */
#Override
public void onResume() {
super.onResume();
//update();
//locationManager.requestLocationUpdates(provider, 400, 1, this);
}
public void update(){
location = gps.getLocation();
lat= location.getLatitude();
lng= location.getLongitude();
pre= (int) location.getAccuracy();
vel= location.getSpeed();
latitudeField.setText(String.valueOf(lat));
longitudeField.setText(String.valueOf(lng));
//altitudine.setText(String.valueOf(alt)+" metri");
precisione.setText(String.valueOf(pre)+" metri");
speed.setText(String.valueOf(vel)+" km/h");
}
}
You are creating a new fragment every time selectItem() method is called. Instead of doing that you should rather use method findFragmentByTag() to obtain fragment you created earlier and create new one only if there is no such fragment.
Then you can simply store in your activity the variable for each fragment and pass required information to selected fragments.