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 Thread That Has Not Called Looper Prepare in Android
in mlocManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, 0, 0, mlocListener);
.
我已经看到了很多这类问题的答案,但它与我的任务无关。我试图在背景中获得gps位置,但我得到例外,因为在线程中没有调用Looper的线程中创建处理程序在Android中的mlocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,0,mlocListener);.
public class GPSLocation extends AsyncTask<Void, Void, Void>
{
@Override
protected void onPreExecute()
{
super.onPreExecute();
progressDialog = new ProgressDialog(RoadMaintenanceActivity.this);
progressDialog.setCancelable(true);
progressDialog.setMessage("Getting GPS Location...");
progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
progressDialog.setProgress(1);
progressDialog.show();
}
@Override
protected void onProgressUpdate(Void... values) {
super.onProgressUpdate(values);
// Things to be done while execution of long running operation is in progress. For example updating ProgessDialog
}
@Override
protected void onPostExecute(Void result)
{
progressDialog.cancel();
}
@Override
protected Void doInBackground(Void... params) {
boolean isGps = false;
while(!isGps)
{
LocationManager mlocManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
LocationListener mlocListener = new MyLocationListener();
mlocManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, 0, 0, mlocListener);
if(longitude !=0 && latitude!=0)
{
isGps = true;
sendSMS();
}
}
return null;
}
}
I am not sure why we cant call that inside the doBackground() method.
我不知道为什么我们不能在doBackground()方法中调用它。
Thanks for your help guys.
谢谢你的帮助。
3 个解决方案
#1
5
Finally i figured out the problem, I think this will help some one like me
最后我想出了问题,我认为这会对像我这样的人有所帮助
public class GPSLocation extends AsyncTask<Void, Void, Void>
{
boolean running =true;
@Override
protected void onPreExecute()
{
super.onPreExecute();
progressDialog = new ProgressDialog(RoadMaintenanceActivity.this);
progressDialog.setOnCancelListener(new DialogInterface.OnCancelListener(){
public void onCancel(DialogInterface dialog) {
getgps.cancel(true);
}
});
LocationManager mlocManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
LocationListener mlocListener = new MyLocationListener();
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();
}
@Override
protected void onProgressUpdate(Void... values) {
super.onProgressUpdate(values);
// Things to be done while execution of long running operation is in progress. For example updating ProgessDialog
}
@Override
protected void onPostExecute(Void result)
{
progressDialog.cancel();
}
@Override
protected Void doInBackground(Void... params) {
boolean isDataSubmitted = false;
while(!isDataSubmitted)
{
if(longitude !=0 && latitude!=0)
{
sendSMS();
isDataSubmitted = true;
}
}
return null;
}
}
By having Locationmanager in onPreExecute()
the exception get rid out from my application. We can get the gps in onpreexecute rather than doinbackground()
.
通过在onPreExecute()中使用Locationmanager,异常将从我的应用程序中删除。我们可以在onpreexecute而不是doinbackground()中获取gps。
#2
3
You can't do that.The mlocListener requires a Looper thread to operate.
你不能这样做.mlocListener需要一个Looper线程才能运行。
in doInBackground Call Looper.prepare();
在doInBackground中调用Looper.prepare();
So your code will become something like this.
所以你的代码将变成这样的东西。
@Override
protected Void doInBackground(Void... params) {
Looper.myLooper().prepare();
boolean isGps = false;
-----------------
#3
1
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
这似乎适合我现在正在尝试做的事情。你有机会向我展示完整的消息来源吗?我目前已将其用于我的代码,但我想看看你如何获得GPS协议并开始异步
ProgressDialog progressDialog;
double longitude, latitude;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_confirm_screen);
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);
Boolean locationCheck = sp.getBoolean("LOCATION", false);
if(locationCheck){
}
else
{
sendEmail();
playSound();
}
}
public class GPSLocation extends AsyncTask<Void, Void, Void>
{
boolean running =true;
@Override
protected void onPreExecute()
{
super.onPreExecute();
progressDialog = new ProgressDialog(ConfirmScreen.this);
progressDialog.setOnCancelListener(new DialogInterface.OnCancelListener(){
public void onCancel(DialogInterface dialog) {
getgps.cancel(true);
}
});
LocationManager mlocManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
LocationListener mlocListener = new MyLocationListener();
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();
}
@Override
protected void onProgressUpdate(Void... values) {
super.onProgressUpdate(values);
// Things to be done while execution of long running operation is in progress. For example updating ProgessDialog
}
@Override
protected void onPostExecute(Void result)
{
progressDialog.cancel();
}
@Override
protected Void doInBackground(Void... params) {
boolean isDataSubmitted = false;
while(!isDataSubmitted)
{
if(longitude !=0 && latitude!=0)
{
sendEmail();
isDataSubmitted = true;
}
}
return null;
}
}
public void backHome(View view)
{
Intent intent = new Intent (this, MainScreen.class);
startActivity(intent);
}
// Method to start playing and looping a sound.
public void playSound()
{
MediaPlayer clickSound = MediaPlayer.create(this, R.raw.warning);
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);
Boolean soundCheck = sp.getBoolean("SOUND", false);
if (soundCheck)
{
clickSound.start();
}
}// method end
public void sendEmail()
{
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);
String nameValue = sp.getString("NAME", "failed to get name");
String emailValue = sp.getString("EMAIL", "failed to get email");
Intent i = new Intent(Intent.ACTION_SEND);
i.setType("message/rfc822");
i.putExtra(Intent.EXTRA_EMAIL, new String[]{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();
}
}
public void sendEmail(String a, String b, String c)
{
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);
String nameValue = sp.getString("NAME", "failed to get name");
String emailValue = sp.getString("EMAIL", "failed to get email");
Intent i = new Intent(Intent.ACTION_SEND);
i.setType("message/rfc822");
i.putExtra(Intent.EXTRA_EMAIL, new String[]{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();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_confirm_screen, menu);
return true;
}
}
#1
5
Finally i figured out the problem, I think this will help some one like me
最后我想出了问题,我认为这会对像我这样的人有所帮助
public class GPSLocation extends AsyncTask<Void, Void, Void>
{
boolean running =true;
@Override
protected void onPreExecute()
{
super.onPreExecute();
progressDialog = new ProgressDialog(RoadMaintenanceActivity.this);
progressDialog.setOnCancelListener(new DialogInterface.OnCancelListener(){
public void onCancel(DialogInterface dialog) {
getgps.cancel(true);
}
});
LocationManager mlocManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
LocationListener mlocListener = new MyLocationListener();
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();
}
@Override
protected void onProgressUpdate(Void... values) {
super.onProgressUpdate(values);
// Things to be done while execution of long running operation is in progress. For example updating ProgessDialog
}
@Override
protected void onPostExecute(Void result)
{
progressDialog.cancel();
}
@Override
protected Void doInBackground(Void... params) {
boolean isDataSubmitted = false;
while(!isDataSubmitted)
{
if(longitude !=0 && latitude!=0)
{
sendSMS();
isDataSubmitted = true;
}
}
return null;
}
}
By having Locationmanager in onPreExecute()
the exception get rid out from my application. We can get the gps in onpreexecute rather than doinbackground()
.
通过在onPreExecute()中使用Locationmanager,异常将从我的应用程序中删除。我们可以在onpreexecute而不是doinbackground()中获取gps。
#2
3
You can't do that.The mlocListener requires a Looper thread to operate.
你不能这样做.mlocListener需要一个Looper线程才能运行。
in doInBackground Call Looper.prepare();
在doInBackground中调用Looper.prepare();
So your code will become something like this.
所以你的代码将变成这样的东西。
@Override
protected Void doInBackground(Void... params) {
Looper.myLooper().prepare();
boolean isGps = false;
-----------------
#3
1
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
这似乎适合我现在正在尝试做的事情。你有机会向我展示完整的消息来源吗?我目前已将其用于我的代码,但我想看看你如何获得GPS协议并开始异步
ProgressDialog progressDialog;
double longitude, latitude;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_confirm_screen);
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);
Boolean locationCheck = sp.getBoolean("LOCATION", false);
if(locationCheck){
}
else
{
sendEmail();
playSound();
}
}
public class GPSLocation extends AsyncTask<Void, Void, Void>
{
boolean running =true;
@Override
protected void onPreExecute()
{
super.onPreExecute();
progressDialog = new ProgressDialog(ConfirmScreen.this);
progressDialog.setOnCancelListener(new DialogInterface.OnCancelListener(){
public void onCancel(DialogInterface dialog) {
getgps.cancel(true);
}
});
LocationManager mlocManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
LocationListener mlocListener = new MyLocationListener();
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();
}
@Override
protected void onProgressUpdate(Void... values) {
super.onProgressUpdate(values);
// Things to be done while execution of long running operation is in progress. For example updating ProgessDialog
}
@Override
protected void onPostExecute(Void result)
{
progressDialog.cancel();
}
@Override
protected Void doInBackground(Void... params) {
boolean isDataSubmitted = false;
while(!isDataSubmitted)
{
if(longitude !=0 && latitude!=0)
{
sendEmail();
isDataSubmitted = true;
}
}
return null;
}
}
public void backHome(View view)
{
Intent intent = new Intent (this, MainScreen.class);
startActivity(intent);
}
// Method to start playing and looping a sound.
public void playSound()
{
MediaPlayer clickSound = MediaPlayer.create(this, R.raw.warning);
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);
Boolean soundCheck = sp.getBoolean("SOUND", false);
if (soundCheck)
{
clickSound.start();
}
}// method end
public void sendEmail()
{
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);
String nameValue = sp.getString("NAME", "failed to get name");
String emailValue = sp.getString("EMAIL", "failed to get email");
Intent i = new Intent(Intent.ACTION_SEND);
i.setType("message/rfc822");
i.putExtra(Intent.EXTRA_EMAIL, new String[]{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();
}
}
public void sendEmail(String a, String b, String c)
{
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);
String nameValue = sp.getString("NAME", "failed to get name");
String emailValue = sp.getString("EMAIL", "failed to get email");
Intent i = new Intent(Intent.ACTION_SEND);
i.setType("message/rfc822");
i.putExtra(Intent.EXTRA_EMAIL, new String[]{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();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_confirm_screen, menu);
return true;
}
}