android 刷新当前位置

发布于 2025-01-05 22:54:28 字数 5606 浏览 3 评论 0原文

在我的应用程序中,我想接收短信,然后,我想获取手机的位置,当我有位置时,我想通过短信发送它......

现在,我有:

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”时刷新我的位置。

感谢您的帮助

In my application I want to receive SMS and afert that, I want to get the the location of the phone and when I have the location, I want to send it By SMS...

by now, there is what I have :

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

}  

and :
package 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  

 }  
 }  

My problem is that I want to refresh my position when method "send_message" is called.

thank you for your help

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一曲琵琶半遮面シ 2025-01-12 22:54:28

我认为您问的是当您收到短信时如何获取当前位置,以便您可以在另一条短信中发回当前位置。我认为您假设您只需拨打电话即可获得当前位置的报告。

GPS 不是这样工作的。它需要运行一段时间才能从足够多的卫星获取足够的消息,从而能够确定位置以及该位置的精度。因此,没有单个函数调用可以简单地立即获取设备位置。

相反,有必要运行后台任务(我使用异步任务)来侦听位置更新并迭代到当前位置。当监听(和 GPS 硬件)有足够好的修复来节省电池寿命时,我的任务会关闭它。

有关详细信息,请参阅之前的相关答案

可以向位置经理询问当前位置。然而,这是它记录的最后一个位置。如果一段时间内没有应用程序使用 GPS 硬件,则该位置修复可能非常旧,因此根本不准确。在工作中,我的建筑物屏蔽了 GPS 信号(金属包层),因此我的手机目前认为它位于几英里外的我家,那是我上次使用支持 GPS 的应用程序的地方。

由于 SMS 的发送时间是可变的,我认为多花几秒钟来启动 GPS 收集并在响应之前等待足够好的修复可能是正确的。 SMS 不是即时传送工具,因此获得良好修复的额外几秒钟延迟不会增加 SMS 消息的往返时间。

I think that you're asking how to get your current location when you receive an SMS message so that you can send back the current location in another SMS message. I think that you are assuming that your are able to just make a call to get your current location to report back.

GPS doesn't work that way. It needs to run for a little while to get enough messages from enough satellites to be able to determine a position and an accuracy for that position. So there is no single function call to simply get the device location instantly.

Instead it is necessary to run a background task (I use an Async Task) to listen to position updates and the iterate to the current location. My task turns off the listening (and the GPS hardware) when it has a good enough fix to save battery life.

See a previous related answer for more information.

It is possible to ask the Location Manager for the current position. However, this is the last position that it recorded. If no application has used the GPS hardware for a while then that postion fix could be very old and therefore not accurate at all. At work, my building shields the GPS signals (metal cladding) and so my phone currently believes that it is at my home many miles away which is where I last used a GPS enabled app.

As SMS takes a variable time to be delivered I think that it is probably correct to take a few extra seconds to fire off the GPS collection and wait for a good enough fix before responding. SMS is not an instant delivery vehicle so the extra few seconds delay in getting a good fix will not add much to the round-trip time of your SMS messages.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文