How To Get Gps Location Using Asynctask?
I have seen many answers for this type of question but its not related with my task. I am trying to get gps location in background but i got exception as Cant Create Handler Inside
Solution 1:
Finally i figured out the problem, I think this will help some one like me
publicclassGPSLocationextendsAsyncTask<Void, Void, Void>
{
boolean running =true;
@OverrideprotectedvoidonPreExecute()
{
super.onPreExecute();
progressDialog = newProgressDialog(RoadMaintenanceActivity.this);
progressDialog.setOnCancelListener(newDialogInterface.OnCancelListener(){
publicvoidonCancel(DialogInterface dialog) {
getgps.cancel(true);
}
});
LocationManager mlocManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
LocationListener mlocListener = newMyLocationListener();
mlocManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, 0, 0, mlocListener);
progressDialog.setCancelable(true);
progressDialog.setMessage("Getting GPS Location...");
progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
progressDialog.setProgress(1);
progressDialog.show();
}
@OverrideprotectedvoidonProgressUpdate(Void... values) {
super.onProgressUpdate(values);
// Things to be done while execution of long running operation is in progress. For example updating ProgessDialog
}
@OverrideprotectedvoidonPostExecute(Void result)
{
progressDialog.cancel();
}
@OverrideprotectedVoiddoInBackground(Void... params) {
boolean isDataSubmitted = false;
while(!isDataSubmitted)
{
if(longitude !=0 && latitude!=0)
{
sendSMS();
isDataSubmitted = true;
}
}
returnnull;
}
}
By having Locationmanager in onPreExecute()
the exception get rid out from my application. We can get the gps in onpreexecute rather than doinbackground()
.
Solution 2:
You can't do that.The mlocListener requires a Looper thread to operate.
in doInBackground Call Looper.prepare();
So your code will become something like this.
@OverrideprotectedVoiddoInBackground(Void... params) {
Looper.myLooper().prepare();
boolean isGps = false;
-----------------
Solution 3:
this seems suitable for what I'm trying to do at the moment. Any chance you can show me the full source? I currently have it worked into my code, but I'd like to see how you are getting the GPS co ords and starting the async
ProgressDialog progressDialog;
double longitude, latitude;
@OverridepublicvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_confirm_screen);
SharedPreferencessp= PreferenceManager.getDefaultSharedPreferences(this);
BooleanlocationCheck= sp.getBoolean("LOCATION", false);
if(locationCheck){
}
else
{
sendEmail();
playSound();
}
}
publicclassGPSLocationextendsAsyncTask<Void, Void, Void>
{
booleanrunning=true;
@OverrideprotectedvoidonPreExecute()
{
super.onPreExecute();
progressDialog = newProgressDialog(ConfirmScreen.this);
progressDialog.setOnCancelListener(newDialogInterface.OnCancelListener(){
publicvoidonCancel(DialogInterface dialog) {
getgps.cancel(true);
}
});
LocationManagermlocManager= (LocationManager)getSystemService(Context.LOCATION_SERVICE);
LocationListenermlocListener=newMyLocationListener();
mlocManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, 0, 0, mlocListener);
progressDialog.setCancelable(true);
progressDialog.setMessage("Getting GPS Location...");
progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
progressDialog.setProgress(1);
progressDialog.show();
}
@OverrideprotectedvoidonProgressUpdate(Void... values) {
super.onProgressUpdate(values);
// Things to be done while execution of long running operation is in progress. For example updating ProgessDialog
}
@OverrideprotectedvoidonPostExecute(Void result)
{
progressDialog.cancel();
}
@Overrideprotected Void doInBackground(Void... params) {
booleanisDataSubmitted=false;
while(!isDataSubmitted)
{
if(longitude !=0 && latitude!=0)
{
sendEmail();
isDataSubmitted = true;
}
}
returnnull;
}
}
publicvoidbackHome(View view)
{
Intentintent=newIntent (this, MainScreen.class);
startActivity(intent);
}
// Method to start playing and looping a sound.publicvoidplaySound()
{
MediaPlayerclickSound= MediaPlayer.create(this, R.raw.warning);
SharedPreferencessp= PreferenceManager.getDefaultSharedPreferences(this);
BooleansoundCheck= sp.getBoolean("SOUND", false);
if (soundCheck)
{
clickSound.start();
}
}// method endpublicvoidsendEmail()
{
SharedPreferencessp= PreferenceManager.getDefaultSharedPreferences(this);
StringnameValue= sp.getString("NAME", "failed to get name");
StringemailValue= sp.getString("EMAIL", "failed to get email");
Intenti=newIntent(Intent.ACTION_SEND);
i.setType("message/rfc822");
i.putExtra(Intent.EXTRA_EMAIL, newString[]{emailValue});
i.putExtra(Intent.EXTRA_SUBJECT, "Email sent from DON'T PANIC - A Chris O'Brien Project");
i.putExtra(Intent.EXTRA_TEXT, "Hi there\n" + nameValue + " is in mortal danger. They didn't include co-ords as they assume you know where they are..\nKind Regards\nDon't Panic! \n\n\n");
try
{ startActivity(Intent.createChooser(i, "Send mail...."));
}
catch (android.content.ActivityNotFoundException ex){
Toast.makeText(ConfirmScreen.this, "There are no email clients installed or set up", Toast.LENGTH_SHORT).show();
}
}
publicvoidsendEmail(String a, String b, String c)
{
SharedPreferencessp= PreferenceManager.getDefaultSharedPreferences(this);
StringnameValue= sp.getString("NAME", "failed to get name");
StringemailValue= sp.getString("EMAIL", "failed to get email");
Intenti=newIntent(Intent.ACTION_SEND);
i.setType("message/rfc822");
i.putExtra(Intent.EXTRA_EMAIL, newString[]{emailValue});
i.putExtra(Intent.EXTRA_SUBJECT, "Email sent from DON'T PANIC - A Chris O'Brien Project");
i.putExtra(Intent.EXTRA_TEXT, "Hi there\n" + nameValue + " is in mortal danger. Please see the co-ords attached and run to their rescue!" +
" If you don't see any co-ords, they didn't check the box and assume you know where they are.\nKind Regards\nDon't Panic! \n\n\n" +
a + b + c);
try
{ startActivity(Intent.createChooser(i, "Send mail...."));
}
catch (android.content.ActivityNotFoundException ex){
Toast.makeText(ConfirmScreen.this, "There are no email clients installed or set up", Toast.LENGTH_SHORT).show();
}
}
@OverridepublicbooleanonCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_confirm_screen, menu);
returntrue;
}
}
Post a Comment for "How To Get Gps Location Using Asynctask?"