2012-02-15 72 views
1

在我的應用我想收到的短信和afert,我想拿到手機的位置,當我的位置,我想送它通過短信......機器人刷新當前位置

現在,有什麼我:

package com.receiver; 
import android.content.BroadcastReceiver; 
import android.content.ComponentName; 
import android.content.Context; 
import android.content.Intent; 
import android.os.Bundle; 
import android.telephony.SmsMessage; 
import android.util.Log; 
import android.widget.Toast; 

public class SMSReceiver extends BroadcastReceiver{ 

    private final String ACTION_RECEIVE_SMS = "android.provider.Telephony.SMS_RECEIVED"; 
    private String numero; 
    private String msg; 

    @Override 
    public void onReceive(Context context, Intent intent){ 
    Log.i("ReceiveBootCompleted","****** Boot terminer ********************"); 
    Log.i("ReceiveBootCompleted"," ***** lancement du service Test **************"); 
    context.startService(new Intent().setComponent(new ComponentName(context.getPackageName(), SMSReceiver.class.getName()))); 

    if (intent.getAction().equals(ACTION_RECEIVE_SMS)){ 
     Bundle bundle = intent.getExtras(); 
     if (bundle != null){ 
      Object[] pdus = (Object[]) bundle.get("pdus"); 
      final SmsMessage[] messages = new SmsMessage[pdus.length]; 
      for (int i = 0; i < pdus.length; i++){ 
      messages[i] = SmsMessage.createFromPdu((byte[]) pdus[i]); 
      } 
      if (messages.length > -1){ 
       final String messageBody = messages[0].getMessageBody(); 
       final String phoneNumber = >messages[0].getDisplayOriginatingAddress(); 
       if(messageBody.equals("mollo")){ 
        app.smartfinder.SmartFinderActivity.send_message(phoneNumber); 
       } 
      } 
     } 
    } 

} 

和: 包app.smartfinder;

import java.io.IOException; 
import java.util.List; 
import android.app.Activity; 
import android.content.Intent; 
import android.location.Address; 
import android.location.Geocoder; 
import android.location.Location; 
import android.location.LocationListener; 
import android.location.LocationManager; 
import android.os.Bundle; 
import android.text.Html; 
import android.text.method.LinkMovementMethod; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.view.Window; 
import android.widget.TextView; 
import android.widget.Toast; 
public class SmartFinderActivity extends Activity implements LocationListener { 
    LocationManager lm; 
    private Location location; 
    private double lat = 0; 
    private double lng = 0; 
    TextView position; 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState){ 

    requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS); 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

    lm = (LocationManager) this.getSystemService(LOCATION_SERVICE); 
     lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 10000, 0, this); 
     lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 10000, 0, this); 
     position = (TextView)findViewById(R.id.var_current_pos); 
    } 

    public void onClick_conf(View view){ 
     Toast.makeText(SmartFinderActivity.this, "config", Toast.LENGTH_LONG).show(); 
     Intent settingsActivity = new Intent(getBaseContext(),    Preferences.class); 
     startActivity(settingsActivity); 
    } 
    public void onClick_hist (View view){ 
     Toast.makeText(SmartFinderActivity.this, "history", Toast.LENGTH_LONG).show(); 
    } 

    public void onLocationChanged(Location location) { 

    lat = location.getLatitude(); 
     lng = location.getLongitude(); 
     afficherAdresse(); 

    position.setOnClickListener(new OnClickListener() { public void onClick(View v) { 
       Intent intent_map = new Intent(getBaseContext(), Map.class); 
       intent_map.putExtra("lat", lat); 
       intent_map.putExtra("lng", lng); 
       startActivity(intent_map); 
      } 
     }); 
    lm.removeUpdates(this); 
    } 
    public void onProviderDisabled(String provider) { 
     // TODO Auto-generated method stub 
    } 
    public void onProviderEnabled(String provider) { 
     // TODO Auto-generated method stub 
    } 
    public void onStatusChanged(String provider, int status, Bundle extras) { 
     // TODO Auto-generated method stub 
    } 

    public void afficherAdresse() { 
     Geocoder geo = new Geocoder(SmartFinderActivity.this); 
     try { 
      List<Address> adresses = geo.getFromLocation(lat,lng,1); 

    if(adresses != null && adresses.size() == 1){ 
       Address adresse = adresses.get(0); 
       //if geocoder find a adresse, we can use it 
       position.setText(adresse.getAddressLine(0) + " " + adresse.getPostalCode() + " " + adresse.getLocality()); 
      } 
      else { 
       //else: no adress 
       position.setText("L'adresse n'a pu être déterminée"); 
      } 
     } catch (IOException e) { 
      e.printStackTrace(); 
      position.setText("L'adresse n'a pu être déterminée"); 
     } 
    } 

    public static void send_message(String phoneNumber) { 

    // `refresh Location : that's the problem ! ` 

    // send method : not difficult 

} 
} 

我的問題是,當調用方法「send_message」時,我想刷新我的位置。

感謝你的幫助

回答

0

我認爲你詢問如何讓你的當前位置,當您收到一條短信,讓您可以在其他短信發回的當前位置。我認爲你假設你只能打一個電話來讓你的當前位置報告回來。

GPS不能這樣工作。它需要運行一段時間才能從足夠的衛星獲得足夠的信息,以確定該位置的位置和精度。所以沒有一個簡單的函數調用來即時獲取設備位置。

相反,需要運行一個後臺任務(我使用異步任務)來偵聽位置更新以及迭代到當前位置。當它有足夠的修復以節省電池使用時間時,我的任務會關閉監聽(和GPS硬件)。

查看a previous related answer瞭解更多信息。

可以向位置經理詢問當前位置。但是,這是它記錄的最後一個位置。如果沒有應用程序使用GPS硬件一段時間,那麼該位置修復可能會非常老,因此根本不準確。在工作中,我的建築物屏蔽了GPS信號(金屬覆蓋層),所以我的手機現在認爲它位於數英里以外的我家,這是我上次使用GPS功能的應用程序的地方。

由於SMS需要一段可變時間才能發送,我認爲在響應之前花費額外的幾秒鐘時間才能啓動GPS收集並等待足夠的修復,這可能是正確的。短信不是即時交付工具,因此獲得良好解決方案的額外幾秒鐘延遲不會增加短信的往返時間。

+0

謝謝你的回答,我會嘗試 – pierro 2012-02-17 20:48:41