我想实现一个功能
就是给老人手机里装一个应用。如果老人走失了,给他发送一条短信,比如发“12345”,老人手机会回发给你一条短信,是他的定位地址
当然硬是要给男朋友装那也没办法,有点贱贱的
下面是实现
添加权限,最后两条是读短信和发短信的权限
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.USE_CREDENTIALS" />
<uses-permission android:name="android.permission.MANAGE_ACCOUNTS" />
<uses-permission android:name="android.permission.AUTHENTICATE_ACCOUNTS" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="com.android.launcher.permission.READ_SETTINGS" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.BROADCAST_STICKY" />
<uses-permission android:name="android.permission.WRITE_SETTINGS" />
<uses-permission android:name="android.permission.SEND_SMS" />
<uses-permission android:name="android.permission.RECEIVE_SMS" />
1.建一个BroadcastReceiver,监听短信
下面有个判断很奇怪,if(content.toString().equals("12345")||number.toString().equals("10010")),为什么要加10010呢?
因为我只有一张SIM卡,给10010发短信人家会回我。。。然后我可以把地址发给他,然后他又会回我,然后我再发,死循环了,是不是好蠢,玩完就把代码改了
这里只是小测验
Intent it=new Intent(context,SendMessageService.class);
it.putExtra("number",number.toString());
context.startService(it);
把号码传到service里,这个service是用来定位的,然后再发短信
public class MessageReceiver extends BroadcastReceiver {
public MessageReceiver() {
}
@Override
public void onReceive(Context context, Intent intent) {
// TODO: This method is called when the BroadcastReceiver is receiving
// an Intent broadcast.
//throw new UnsupportedOperationException("Not yet implemented");
StringBuilder number=new StringBuilder();
StringBuilder content=new StringBuilder();
Bundle bundle=intent.getExtras();
// Log.e("MessageReceiver","bundle2="+intent.getExtras().toString());
if(bundle!=null)
{
Log.e("SJJ","bundle="+bundle.size());
Object[] pdus=(Object[]) bundle.get("pdus");
SmsMessage[] smsMessages=new SmsMessage[pdus.length];
for(int i=0;i<pdus.length;i++)
{
smsMessages[i]=SmsMessage.createFromPdu((byte[]) pdus[i]);
}
number.append(smsMessages[0].getDisplayOriginatingAddress());
//number.append(" ");
//content.append(smsMessages[0].getDisplayMessageBody());
for(SmsMessage message:smsMessages)
{
//number.append(message.getDisplayOriginatingAddress());
//number.append(" ");
content.append(message.getDisplayMessageBody());
}
}
if(content.toString().equals("12345")||number.toString().equals("10010"))
{
Intent it=new Intent(context,SendMessageService.class);
it.putExtra("number",number.toString());
context.startService(it);
}
Log.e("SJJ","number="+number.toString()+" content="+content.toString());
}
}
2.建立个定位的Service
onReceiveLocation方法里获得location
content=location.getAddrStr();获得地址
下面这个方法发送短信,才短短几行代码。。
void sendMessage(String number,String content)
{
PendingIntent pi= PendingIntent.getService(SendMessageService.this,
0, new Intent(), 0);
smsMg.sendTextMessage(number, null, content,
pi, null);
}
@Override
public void onStart(Intent intent, int startId) {
super.onStart(intent, startId);
if (intent != null) {
// final String action = intent.getAction();
// if (ACTION_FOO.equals(action)) {
// final String param1 = intent.getStringExtra(EXTRA_PARAM1);
// final String param2 = intent.getStringExtra(EXTRA_PARAM2);
// handleActionFoo(param1, param2);
// } else if (ACTION_BAZ.equals(action)) {
// final String param1 = intent.getStringExtra(EXTRA_PARAM1);
// final String param2 = intent.getStringExtra(EXTRA_PARAM2);
// handleActionBaz(param1, param2);
// }
number=intent.getStringExtra("number");
smsMg=SmsManager.getDefault();
mLocationClient = new LocationClient(getApplicationContext()); //声明LocationClient类
mLocationClient.registerLocationListener( myListener ); //注册监听函数
LocationClientOption option = new LocationClientOption();
option.setLocationMode(LocationClientOption.LocationMode.Hight_Accuracy);//设置定位模式
option.setCoorType("bd09ll");//返回的定位结果是百度经纬度,默认值gcj02
option.setScanSpan(5000);//设置发起定位请求的间隔时间为5000ms
option.setIsNeedAddress(true);//返回的定位结果包含地址信息
option.setNeedDeviceDirect(true);//返回的定位结果包含手机机头的方向
mLocationClient.setLocOption(option);
mLocationClient.start();
if (mLocationClient != null && mLocationClient.isStarted())
{
Log.e("SJJ", "mLocationClient.start();");
mLocationClient.requestLocation();
}
else
Log.e("SJJ", "locClient is null or not started");
Log.e("SJJ","service get number ="+number);
}
}
public class MyLocationListener implements BDLocationListener {
@Override
public void onReceiveLocation(BDLocation location) {
if (location == null)
return ;
StringBuffer sb = new StringBuffer(256);
sb.append("time : ");
sb.append(location.getTime());
sb.append("\nerror code : ");
sb.append(location.getLocType());
sb.append("\nlatitude : ");
sb.append(location.getLatitude());
sb.append("\nlontitude : ");
sb.append(location.getLongitude());
sb.append("\nradius : ");
sb.append(location.getRadius());
if (location.getLocType() == BDLocation.TypeGpsLocation){
sb.append("\nspeed : ");
sb.append(location.getSpeed());
sb.append("\nsatellite : ");
sb.append(location.getSatelliteNumber());
} else if (location.getLocType() == BDLocation.TypeNetWorkLocation){
sb.append("\naddr : ");
sb.append(location.getAddrStr());
}
content=location.getAddrStr();
Log.e("SJJ","service"+content);
sendMessage(number,content);
mLocationClient.stop();
SendMessageService.this.stopSelf();
}
}
void sendMessage(String number,String content)
{
PendingIntent pi= PendingIntent.getService(SendMessageService.this,
0, new Intent(), 0);
smsMg.sendTextMessage(number, null, content,
pi, null);
}
}
看下结果
先给10010发条短信,然后这个app就自动把地址回过去了。。。对,我在上海。。。
如果你要给男朋友装怎么办。。。不要写Activity啊,偷偷给他装,然后就有个service再默默的跑,连图标都没有。。。不行,这样太贱了