I am reading GPS info and sending it to the network every 10 seconds.
What I am doing is using an handler which runs my service class GPSTracker
public void startGpsService() {
handler = new Handler();
task = new Runnable() {
@Override
public void run() {
gps = new GPSTracker(getApplicationContext());
if (gps.canGetLocation()) {
... do something
} else {
// GPS or Network is not enabled
gps.showSettingsAlert();
}
handler.postDelayed(task, 10000);
}
};
task.run();
}
if my GPS setting are not enabled gps.showSettingsAlert(); is executed
public class GPSTracker extends Service implements LocationListener {
private final Context mContext;
// Declaring a Location Manager
protected LocationManager locationManager;
public GPSTracker(Context context) {
this.mContext = context;
getLocation();
}
...
/**
* 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();
}
...
My problem is that alertDialog.show(); raises this exception:
08-21 09:49:23.906: E/AndroidRuntime(4088): java.lang.RuntimeException: Unable to start activity ComponentInfo{...}: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
How to solve it? Similar solutions found on here and here dont fix my problem as in that case it happens inside activity classes.
0 comments:
Post a Comment