2012-02-15 76 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