2012-06-14 135 views
1

我已經看到了很多這類問題的答案,但它與我的任務無關。我想要在後臺獲得GPS位置,但我得到了Cant Create Handler Inside Thread That Has Not Called Looper Prepare in AndroidmlocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, mlocListener);的例外。如何使用AsyncTask獲取GPS位置?

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; 


     } 

    } 

我不知道爲什麼我們不能在doBackground()方法中調用它。

感謝您的幫助球員。

回答

5

最後我想通了這個問題,我認爲這將幫助一些人喜歡我

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;  
       } 
    } 

通過具有onPreExecute()的LocationManager異常從我的應用程序擺脫出來。我們可以通過onpreexecute獲取gps,而不是doinbackground()

3

你不能這樣做。mlocListener需要一個Looper線程來操作。

在doInBackground呼叫Looper.prepare();

所以你的代碼會變得這樣的事情。

@Override 
     protected Void doInBackground(Void... params) { 
      Looper.myLooper().prepare(); 
      boolean isGps = false; 
      ----------------- 
+0

謝謝您的回答其實我解決了這個問題。現在它的一個不同'SmsManager smsManager = SmsManager.getDefault(); \t \t \t \t \t \t \t \t \t \t smsManager.sendTextMessage(sms_phonenumber,NULL,消息,sentPI,NULL); '它說空指針異常..你有什麼想法...謝謝 – GoCrazy

+0

你應該用你最近的錯誤創建一個新的問題,因爲它很難解決我們看不到的問題。 – Guardanis

+0

@cruceo感謝您的評論..我已經解決了這個問題.. – GoCrazy

1

這似乎適合我現在要做的事情。任何機會,你可以告訴我完整的來源?目前,我有它的工作爲我的代碼,但我想看看你是如何得到GPS聯合ORDS和啓動異步

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; 
} 






}