当我向某人发送短信时,我如何获得其他用户的位置作为回报?

时间:2012-04-05 19:03:19

标签: android sms broadcastreceiver

代码显示了我到目前为止所做的事情。现在我需要做的是,当我发送短信给那个人我得到他的位置,我也想在谷歌地图上显示位置!

清单

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.mehul.googlemaps"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk android:minSdkVersion="8" />
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.SEND_SMS"/>
<uses-permission android:name="android.permission.RECEIVE_SMS"/>

<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name">   

    <uses-library android:name="com.google.android.maps" />
    <activity
        android:name=".Main"
        android:label="@string/app_name" >
         <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:name=".HelloWorld" />

</application>

</manifest>

Main.java

import java.io.IOException;
import java.util.List;
import java.util.Locale;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;
import com.google.android.maps.MyLocationOverlay;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.drawable.Drawable;
import android.location.Address;
import android.location.Criteria;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class Main extends MapActivity implements LocationListener {
/** Called when the activity is first created. */
MapView map;
long start;
long stop;
int x, y;
GeoPoint touchedPoint;
Drawable d;
List<Overlay> overlayList;
LocationManager lm;
String towers;
int lat ;
int longi; 





@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);



    Button button = (Button)findViewById(R.id.button1);
    button.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            // TODO Auto-generated method stub

            Intent intent = new Intent(Main.this, HelloWorld.class);
            startActivity(intent);
        }
    });

    map = (MapView)findViewById(R.id.mv);
    map.setBuiltInZoomControls(true);

    Touchy t = new Touchy();
    overlayList = map.getOverlays();
    overlayList.add(t);

    d = getResources().getDrawable(R.drawable.pinn);

    //Placing pintpoint
    lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    Criteria crit = new Criteria();

    towers = lm.getBestProvider(crit, false);
    Location location = lm.getLastKnownLocation(towers);

    if (location != null){
        lat = (int) (location.getLatitude() *1E6);
        longi= (int) (location.getLongitude() *1E6);



        GeoPoint ourLocation = new GeoPoint(lat,longi);
        OverlayItem overlayItem = new OverlayItem(ourLocation, "Hi!!", "2nd");
        CustomPinPoint custom = new CustomPinPoint(d, Main.this);   
        custom.insertPinpoint(overlayItem);
        overlayList.add(custom);
    }else{
        Toast.makeText(Main.this, "Couldnt get provider", Toast.LENGTH_SHORT).show();
    }





}

@Override
protected void onPause() {
    // TODO Auto-generated method stub

    super.onPause();
    lm.removeUpdates(this);
}


 @Override
protected void onResume() {
    // TODO Auto-generated method stub

    super.onResume();
    lm.requestLocationUpdates(towers, 500, 1, this );
}


    @Override
protected boolean isRouteDisplayed() {
    // TODO Auto-generated method stub
    return false;
}



class Touchy extends Overlay{
    public boolean onTouchEvent(MotionEvent e, MapView m){
    if (e.getAction() == MotionEvent.ACTION_DOWN){
        start = e.getEventTime();

    }
    if (e.getAction() == MotionEvent.ACTION_UP){
        stop = e.getEventTime();
        x = (int) e.getX();
        y = (int) e.getY();
        touchedPoint = map.getProjection().fromPixels(x, y);

    }
    if (stop - start > 1500){
        AlertDialog alert = new AlertDialog.Builder(Main.this).create();
        alert.setTitle("Pick Option");


        alert.setButton("Hello", new DialogInterface.OnClickListener() {

            public void onClick(DialogInterface dialog, int which) {
                // TODO Auto-generated method stub

                OverlayItem overlayItem = new OverlayItem(touchedPoint, "Hi!!", "2nd");
                CustomPinPoint custom = new CustomPinPoint(d, Main.this);   
                custom.insertPinpoint(overlayItem);
                overlayList.add(custom);



        }
        });
        alert.setButton3("Get Address", new DialogInterface.OnClickListener() {

            public void onClick(DialogInterface dialog, int which) {
                // TODO Auto-generated method stub
            Geocoder geocoder = new Geocoder(getBaseContext(), Locale.getDefault());
            try{
                List<Address> address = geocoder.getFromLocation(touchedPoint.getLatitudeE6() / 1E6, touchedPoint.getLongitudeE6() / 1E6 , 1);
                if (address.size() > 0){
                    String display = "";
                    for (int i = 0; i<address.get(0).getMaxAddressLineIndex(); i++){

                        display += address.get(0).getAddressLine(i) + "\n";
                    }
                    Toast t = Toast.makeText(getBaseContext(), display, Toast.LENGTH_LONG);
                    t.show();
                }
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }finally{
            }
        }});
        alert.setButton2("Toggle View", new DialogInterface.OnClickListener() {

            public void onClick(DialogInterface dialog, int which) {
                // TODO Auto-generated method stub

                if (map.isSatellite()){
                    map.setSatellite(false);
                    map.setStreetView(true);
                }else{
                    map.setStreetView(false);
                    map.setSatellite(true);
                }
            }
});
        alert.setButton("Place a Pin", new DialogInterface.OnClickListener() {

            public void onClick(DialogInterface dialog, int which) {
                // TODO Auto-generated method stub

                OverlayItem overlayItem = new OverlayItem(touchedPoint, "Hi!!", "2nd");
                CustomPinPoint custom = new CustomPinPoint(d, Main.this);   
                custom.insertPinpoint(overlayItem);
                overlayList.add(custom);

            }


        });
        alert.show();
        return true;
    }

        return false;
    }

}



public void onLocationChanged(Location l) {
    // TODO Auto-generated method stub
    lat = (int) (l.getLatitude() *1E6);
    longi = (int) (l.getLongitude() *1E6);
    GeoPoint ourLocation = new GeoPoint(lat,longi);
    OverlayItem overlayItem = new OverlayItem(ourLocation, "Hi!!", "2nd");
    CustomPinPoint custom = new CustomPinPoint(d, Main.this);   
    custom.insertPinpoint(overlayItem);
    overlayList.add(custom);

}




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

}

}

Helloworld.java

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Toast;


public class HelloWorld extends Activity
{

        protected void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        setContentView(R.layout.helloworld);

        Intent sendIntent = new Intent(Intent.ACTION_VIEW);
        sendIntent.putExtra("sms_body", "Content of the SMS goes here..."); 
        sendIntent.setType("vnd.android-dir/mms-sms");
        startActivity(sendIntent);
            }


}

如果需要其他任何内容,请与我联系。

1 个答案:

答案 0 :(得分:0)

假设其他人也将安装您的应用,您可以创建一个BroadcastReceiver,它将响应android.provider.Telephony.SMS_RECEIVED操作。接收方应该实现onReceive,检测消息是否来自您的应用程序(我猜在消息体中使用一些特殊密钥?),从收到的Intent中的附加内容中提取消息和始发电话号码,然后触发一个位置监听器,然后发回短信......

同样的BroadcastReceiver也可以检测消息中是否有纬度/经度,将坐标存储在某个持久存储中,并发送广播意图告诉MapActivity用新坐标更新它的地图。