1.先看下布局的
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/tv"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
2.活动的实现SIMTestActivity.java
package com.wang;
import android.app.Activity;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.widget.TextView;
public class SIMTestActivity extends Activity {
private TelephonyManager telephonyManager;
private TextView textView;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//获得电话相关信息的系统服务
telephonyManager =(TelephonyManager)getSystemService(TELEPHONY_SERVICE);
// 得到SIM卡的信息
String info=getInfo(telephonyManager);
textView=(TextView)findViewById(R.id.tv);
textView.setText(info);
}
public static String getInfo(TelephonyManager tm) {
StringBuffer sb=new StringBuffer();
//获得SIM卡的当前状态
sb.append("SIM卡的当前状态是:");
if (tm.getSimState()==TelephonyManager.SIM_STATE_READY) {
sb.append("正常").append("\n\n");
}else if (tm.getSimState()==TelephonyManager.SIM_STATE_ABSENT) {
sb.append("N/A:没有SIM卡").append("\n\n");
} else {
sb.append("N/A:SIM卡状态异常").append("\n\n");
}
sb.append("SIM卡的编号:");
if (tm.getSimSerialNumber()!=null) {
sb.append(tm.getSimSerialNumber()).append("\n\n");
} else {
sb.append("N/A:获取失败").append("\n\n");
}
//获得SIM卡运营商名称
sb.append("SIM卡运营商名称");
if (tm.getSimOperatorName().equals("")) {
sb.append("N/A:获取失败").append("\n\n");
} else {
sb.append(tm.getSimOperatorName()).append("\n\n");
}
//获取SIM卡所属的国家
sb.append("SIM卡所属的国家:");
if (tm.getSimCountryIso().equals("")) {
sb.append("N/A:获取失败").append("\n\n");
} else {
sb.append(tm.getSimCountryIso()).append("\n\n");
}
// 获得手机号码
sb.append("手机号码:");
if (tm.getLine1Number()!=null) {
sb.append(tm.getLine1Number()).append("\n\n");
} else {
sb.append("N/A:获取失败").append("\n\n");
}
return sb.toString();
}
}
3.需要用到的类有Position.java
package com.wang;
import java.security.KeyStore.LoadStoreParameter;
import android.content.Context;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationManager;
public class Position {
private static LocationManager locationmanger;
private static Location location;
private static String relocationProvider;
public static String getposition(Context context){
Context con = null;
locationmanger=(LocationManager)con.getSystemService(con.LOCATION_SERVICE);
location =getLocationProvider(locationmanger);
if (location!=null) {
StringBuffer buffer=new StringBuffer();
buffer.append("latitude:");
buffer.append(Double.toString(location.getLatitude()));
buffer.append(",longitude:");
buffer.append(Double.toString(location.getLongitude()));
return buffer.toString();
} else {
return null;
}
}
private static Location getLocationProvider(LocationManager locationmanger2) {
Location reLocation=null;
Criteria criteria=new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
criteria.setBearingRequired(false);
criteria.setPowerRequirement(Criteria.POWER_LOW);
relocationProvider=locationmanger.getBestProvider(criteria, true);
reLocation=locationmanger.getLastKnownLocation(relocationProvider);
return reLocation;
}
}
4.需要用到的类有:PowerBoot.java调用这个类的时候是在手机卡被换掉是时候
package com.wang;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.telephony.TelephonyManager;
import android.telephony.SmsManager;
public class PowerBoot extends BroadcastReceiver {
TelephonyManager telephonyManager;
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
final Context con=context;
if (telephonyManager.getSimSerialNumber()!=null) {
//预先设置好的SIM卡编码
if (telephonyManager.getSimSerialNumber()!="898607c3161047753733") {
new Runnable() {
public void run() {
// TODO Auto-generated method stub
String position=Position.getposition(con);
SmsManager smsManager=SmsManager.getDefault();
//预先设置好自己的电话号码
smsManager.sendTextMessage("15290336267", null, "主人你丢失的手机所在的位置是:"+position, null, null);
//smsManager.sendTextMessage(destinationAddress, scAddress, text, sentIntent, deliveryIntent)
}
}.run();
}
}
}
}
5.需要用到的类有:SMSlistener.java 调用这个类的时候是在手机找不到的时候,会调用这个类
package com.wang;
import java.text.SimpleDateFormat;
import java.util.Date;
import com.wang.Position;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.telephony.SmsManager;
import android.telephony.SmsMessage;
public class SMSlistener extends BroadcastReceiver {
private static String position;
public void onReceive(Context context, Intent intent) {
//这个方法一旦返回,android将回收BroadcastReceiver
final Context con=context;
Object[] pdus=(Object[])intent.getExtras().get("puds");
if (pdus!=null&&pdus.length>0) {
SmsMessage [] messages=new SmsMessage[pdus.length];
for (int i = 0; i < pdus.length; i++) {
byte[] pdu=(byte[])pdus[i];
messages[i]=SmsMessage.createFromPdu(pdu);
}
for (SmsMessage msg: messages) {
final SmsMessage sms=msg;
String content =msg.getMessageBody();
final String sender=msg.getOriginatingAddress();
if (content.startsWith(":getlocation")) {
new Runnable() {
public void run() {
// TODO Auto-generated method stub
position=Position.getposition(con);
if (position!=null) {
SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String sendcontent=format.format(new Date(sms.getTimestampMillis())+" 主人你的手机所在的位置是:"+position);
SmsManager smsManager=SmsManager.getDefault();
//回复一条短信
smsManager.sendTextMessage(sender, null, sendcontent, null, null);
}
}
}.run();
}
}
}
}
}
6.运行结果如下:
7.感觉这个程序是实用性不大,因为如果手机丢了,返回一个经纬度,虽然可以具体知道什么位置,但是,手机是否找到还真的不好说。