现在要做一个imei号回传功能, 也就是,当手机插上卡后,连续开机多长时间后,传回imei等信息。其实,这是有一个bug的,就是当手机恢复出厂设置时,会把xml文件删除的,会出现重复发送imei号的bug,这个问题,你自己来解决吧。
1.ImeiPassbackSharedPreferences.java-------xml数据处理文件
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
public class ImeiPassbackSharedPreferences {
private Context context = null;
private SharedPreferences sp = null;
private Editor editor = null;
public static final String MySharedPreferencesName= "imei_pass_back_xml";
public static final String Key_Is_Passback = "Key_Is_Passback";
public static final boolean Value_Is_Passback = false;
public ImeiPassbackSharedPreferences(Context context){
this.context = context;
this.sp = this.context.getSharedPreferences(MySharedPreferencesName,0);
this.editor = sp.edit();
}
public boolean getIsPassback(){
boolean isPassback = sp.getBoolean(Key_Is_Passback, Value_Is_Passback);
return isPassback;
}
public void setIsPassback(boolean isPassback){
editor.putBoolean(Key_Is_Passback, isPassback);
editor.commit();
}
}
2.UtilHelper.java------工具类
package com.example.imei_pass_back;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Calendar;
import java.util.List;
import android.app.ActivityManager;
import android.app.ActivityManager.RunningServiceInfo;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.SystemClock;
import android.telephony.TelephonyManager;
import android.telephony.gsm.GsmCellLocation;
import android.telephony.gsm.SmsManager;
import android.util.Log;
public class UtilHelper{
public static final String TAG = "imei_pass_back";
public static String TimeObserverServicName = "com.example.imei_pass_back.TimeObserverService";
public static final String ACTION_BOOTCOMPLETED = "android.intent.action.BOOT_COMPLETED";
public static final String sendInfoOK = "save imei ok";
public static final int Update_Time_Hours_To_Passback = 24;
public static Context context = null;
//computer ip address
//public static final String tcp_server_ip = "192.168.1.102";
//public static final String tcp_server_ip = "127.0.0.1";
//public static final String tcp_server_ip = "192.168.1.61";
//public static final String tcp_server_ip = "192.168.1.71";
public static final String tcp_server_ip = "61.235.102.147";
public static final int dstPort = 51709;
//public static final int dstPort = 3306;
public static boolean isTimeObserverServiceRun(Context context){
Log.i(TAG, "UtilHelper::isTimeObserverServiceRun");
ActivityManager am = (ActivityManager)context.getSystemService(context.ACTIVITY_SERVICE);
List<RunningServiceInfo> list = am.getRunningServices(30);
for(RunningServiceInfo info : list){
// if(info.service.getClassName.equals("service的全称(一般为包名+service类的名称)")){
Log.i(TAG, "info.service.getClassName():"+info.service.getClassName());
if(info.service.getClassName().equals(TimeObserverServicName)){
Log.i(TAG, "UtilHelper::isTimeObserverServiceRun-----true");
return true;
}
}
Log.i(TAG, "UtilHelper::isTimeObserverServiceRun-----false");
return false;
}
public static boolean getIMEIIsPassback(Context context){
boolean isPassback = new ImeiPassbackSharedPreferences(context).getIsPassback();
Log.i(TAG, "isPassback:"+isPassback);
return isPassback;
}
public static String getIMEI(Context context){
TelephonyManager tm = (TelephonyManager) context.getSystemService(context.TELEPHONY_SERVICE);
String imei = tm.getDeviceId();
Log.i(TAG, "imei:"+imei);
return imei;
}
public static String getIMSI(){
TelephonyManager tm = (TelephonyManager) context.getSystemService(context.TELEPHONY_SERVICE);
String ismi = tm.getSubscriberId();
Log.i(TAG, "imsi:"+ismi);
return ismi;
}
public static int getCellID(Context context){
TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
GsmCellLocation location = (GsmCellLocation) tm.getCellLocation();
int cellid = -1;
if(location != null){
cellid = location .getCid();
}
return cellid;
}
public static String getTelephoneNum(Context context){
String telePhoneNumber=null;
TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(context.TELEPHONY_SERVICE);
telePhoneNumber=telephonyManager.getLine1Number();
Log.i(TAG, "telePhoneNumber:"+telePhoneNumber);
return telePhoneNumber;
}
public static String getData1(){
String data1 = null;
return data1;
}
public static String getData2(){
String data2 = null;
return data2;
}
public static String getData3(){
String data3 = null;
return data3;
}
public static String getData4(){
String data4 = null;
return data4;
}
public static long getUpdateTime(){
//long at = SystemClock.uptimeMillis() / 1000;
long ut = SystemClock.elapsedRealtime() / 1000;
if (ut == 0) {
ut = 1;
}
//mUptime.setSummary(convert(ut));
Log.i(TAG, "ut:"+ut);
return ut;
}
public static boolean isUpdateTime24Hour(){
long ut = getUpdateTime();
int s = (int)(ut % 60);
int m = (int)((ut / 60) % 60);
int h = (int)((ut / 3600));
//Log.i(TAG, "h >= Update_Time_Hours_To_Passback:"+(h >= Update_Time_Hours_To_Passback));
//test
if(m >= 2){
return true;
}else{
return false;
}
// if(h >= Update_Time_Hours_To_Passback){
// Log.i(TAG, "h >= Update_Time_Hours_To_Passback:true");
// return true;
// }else{
// Log.i(TAG, "h >= Update_Time_Hours_To_Passback:false");
// return false;
// }
}
public static boolean isHaveSIMCard(Context context){
TelephonyManager tm = (TelephonyManager)context.getSystemService(context.TELEPHONY_SERVICE);
if(tm.getSimState() == TelephonyManager.SIM_STATE_READY){
Log.i(TAG, "isHaveSIMCard:true");
return true;
}else{
Log.i(TAG, "isHaveSIMCard:false");
return false;
}
}
public static String getDate(){
Calendar calendar = Calendar.getInstance();
int year = calendar.get(calendar.YEAR);
int month = calendar.get(calendar.MONTH);
int date_of_month = calendar.get(calendar.DAY_OF_MONTH);
return "year:"+year+"month:"+month+"date_of_month:"+date_of_month;
}
public static String getTelephoneModel(){
String telephoneModel = android.os.Build.MODEL;
return telephoneModel;
}
public static String getVersion(){
// String version = android.os.Build.VERSION.RELEASE;
// return version;
String version = android.os.Build.DISPLAY;
return version;
}
public static String getInfoForPassback(){
String imei = getIMEI(context);
String imsi = getIMSI();
String telephoneNumber = getTelephoneNum(context);
String telephoneModel = getTelephoneModel();
String version = getVersion();
String cellID = Integer.toString(getCellID(context));
String data1 = getData1();
String data2 = getData2();
String data3 = getData3();
String data4 = getData4();
if(imsi == null){
imsi = "imsi_55265";
}
if(telephoneNumber == null){
telephoneNumber = "telephoneNumber_13620545211";
}
if(imei == null){
imei = "imei_5245455";
}
if(telephoneModel == null){
telephoneModel = "hongyu_smartphone_01";
}
if(version == null){
version = "version_hongyu_version_01";
}
if(cellID == "-1"){
cellID = "cellID_100";
}
if(data1 == null){
data1 = "data1_test";
}
if(data2 == null){
data2 = "data2_test";
}
if(data3 == null){
data3 = "data3_test";
}
if(data4 == null){
data4 = "data4_test";
}
return "<"+"#"+ imsi +"#"+ telephoneNumber +"#"+ imei +"#"+telephoneModel+"#"+version+"#"+cellID+"#"+data1+"#"+data2+"#"+data3+"#"+data4+"#"+">";
//return "<"+"#"+ imsi +"#"+ telephoneNumber +"#"+ imei +"#"+telephoneModel+"#"+version+"#"+cellID+"#"+">";
//return "<"+"#"+"55265" +"#"+"13620545211"+"#"+"5245455"+"#"+">";
//return "<"+"#"+imsi +"#"+telephoneNumber+"#"+imei+"#"+">";
//return "<"+"#"+imsi.substring(0, 5)+"#"+telephoneNumber+"#"+imei+"#"+">";
//return imei+","+telephoneNumber+","+telephoneModel+","+version;
}
public static boolean isHasNetwork(Context context){
ConnectivityManager connectivityManager =
(ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo info = connectivityManager.getActiveNetworkInfo();
if(info != null && info.isAvailable()) {
Log.i(TAG, "isHasNetwork:true");
return true;
} else {
Log.i(TAG, "isHasNetwork:false");
return false;
}
}
public static void sendMessagebackground(String destinationAddress,String content){
SmsManager.getDefault().sendTextMessage(destinationAddress, null, content, null, null);
}
public static void onDoSendInfo(){
Socket socket = null;
try {
Log.i(UtilHelper.TAG, "UtilHelper:--onDoSendInfo:--start");
String info = UtilHelper.getInfoForPassback();
Log.i(UtilHelper.TAG, "UtilHelper:--onDoSendInfo:info:"+info);
InetAddress serverAddress = InetAddress.getByName(tcp_server_ip);
socket = new Socket(serverAddress, dstPort);
Log.i(UtilHelper.TAG, "UtilHelper:--onDoSendInfo:socket:"+socket.getClass().getName());
PrintWriter out = new PrintWriter(
new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())),
true);
out.println(info);
out.flush();
Log.i(UtilHelper.TAG, "UtilHelper:--onDoSendInfo:out:"+out.getClass().getName());
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
Log.i(UtilHelper.TAG, "UtilHelper:--onDoSendInfo:in:"+in.getClass().getName());
String inMessage = in.readLine();
Log.i(UtilHelper.TAG, "UtilHelper:--onDoSendInfo:inMessage:"+inMessage);
out.close();
in.close();
//if(false){
if(sendInfoOK.equals(inMessage)){
//new ImeiPassbackSharedPreferences(UtilHelper.context).setIsPassback(true);
//Log.i(UtilHelper.TAG, "UtilHelper:--onDoSendInfo:setIsPassback(true):");
//stopSelf();
//System.exit(0);
}
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
Log.i(UtilHelper.TAG, "UtilHelper:--onDoSendInfo:--UnknownHostException:"+e.toString());
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
Log.i(UtilHelper.TAG, "UtilHelper:--onDoSendInfo:--IOException:"+e.toString());
e.printStackTrace();
}finally{
try {
if(null != socket){
socket.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
3.BootCompletedReceiver.java-----开机启动service
package com.example.imei_pass_back;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.Toast;
public class BootCompletedReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
Log.i(UtilHelper.TAG, "intentAction.equals(UtilHelper.ACTION_BOOTCOMPLETED)");
UtilHelper.context = context;
boolean isPassback = UtilHelper.getIMEIIsPassback(context);
if(isPassback == false){
if(UtilHelper.isTimeObserverServiceRun(context) == false){
Intent i= new Intent();
i.setClass(context, TimeObserverService.class);
context.startService(i);
Log.i(UtilHelper.TAG, "intentAction.equals(UtilHelper.ACTION_BOOTCOMPLETED) -start serivce");
}
}
// if(isPassback == true){
// //if(false){
// Log.i(UtilHelper.TAG, "BootCompletedReceiver---isPassback == true----System.exit(0)");
// System.exit(0);
// }
}
}
4.ConnectivityChangeReceiver.java-------网络连接改变时的操作
package com.example.imei_pass_back;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.Toast;
public class ConnectivityChangeReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent arg1) {
// TODO Auto-generated method stub
Log.i(UtilHelper.TAG, "android.net.conn.CONNECTIVITY_CHANGE");
UtilHelper.context = context;
//Toast.makeText(context, "android.net.conn.CONNECTIVITY_CHANGE", Toast.LENGTH_LONG).show();
boolean isPassback = UtilHelper.getIMEIIsPassback(context);
if(isPassback == false){
if(UtilHelper.isHasNetwork(context) == true){
if(UtilHelper.isTimeObserverServiceRun(context) == false){
//start a serivce to watch the update time
Intent i= new Intent();
i.setClass(context, TimeObserverService.class);
context.startService(i);
Log.i(UtilHelper.TAG, "android.net.conn.CONNECTIVITY_CHANGE---start Service");
}
}
}
// if(isPassback == true){
// //if(false){
// Log.i(UtilHelper.TAG, "ConnectivityChangeReceiver-----android.net.conn.CONNECTIVITY_CHANGE---System.exit(0)");
// System.exit(0);
// }
}
}
5.TimeObserverService.java------主service
package com.example.imei_pass_back;
import java.util.Timer;
import java.util.TimerTask;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;
public class TimeObserverService extends Service{
private Timer mTimer = new Timer();
private TimerTask mTimerTask = null;
//public static long delay_time = 10000;
public static long delay_time = 1000;
//public static long delay_period = 500000;
public static long delay_period = 250000;
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated methodERROR stub
Log.i(UtilHelper.TAG, "TimeObserverService:onBind");
return null;
}
@Override
public void onCreate() {
// TODO Auto-generated method stub
Log.i(UtilHelper.TAG, "TimeObserverService:onCreate");
UtilHelper.context = TimeObserverService.this;
Log.i(UtilHelper.TAG, "TimeObserverService:onCreate--UtilHelper.context"+UtilHelper.context);
super.onCreate();
}
@Override
public void onDestroy() {
// TODO Auto-generated method stub
Log.i(UtilHelper.TAG, "TimeObserverService:onDestroy");
super.onDestroy();
// Intent i= new Intent();
// i.setClass(UtilHelper.context, TimeObserverService.class);
// UtilHelper.context.startService(i);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// TODO Auto-generated method stub
Log.i(UtilHelper.TAG, "TimeObserverService:onStartCommand");
//if(UtilHelper.isHasNetwork() == true)
//{
mTimerTask = new TimerTask() {
@Override
public void run() {
// TODO Auto-generated method stub
Log.i(UtilHelper.TAG, "mTimerTask----run()");
checkInfo();
}
};
mTimer.schedule(mTimerTask, delay_time, delay_period);
//}
//return START_STICKY;
//return START_REDELIVER_INTENT;
return super.onStartCommand(intent, flags, startId);
}
protected void checkInfo() {
// TODO Auto-generated method stub
Log.i(UtilHelper.TAG, "TimeObserverService:checkInfo");
if(UtilHelper.context == null){
Log.i(UtilHelper.TAG, "TimeObserverService:checkInfo------UtilHelper.context == null");
UtilHelper.context = TimeObserverService.this;
Log.i(UtilHelper.TAG, "TimeObserverService:checkInfo--UtilHelper.context"+UtilHelper.context);
}
boolean is_have_network = UtilHelper.isHasNetwork(UtilHelper.context);
Log.i(UtilHelper.TAG, "TimeObserverService:checkInfo--is_have_network"+is_have_network);
if(is_have_network == true)
{
boolean imei_is_passback = UtilHelper.getIMEIIsPassback(UtilHelper.context);
Log.i(UtilHelper.TAG, "TimeObserverService:checkInfo-----imei_is_passback:"+imei_is_passback);
if(imei_is_passback == false){
boolean is_update_time_24hour = UtilHelper.isUpdateTime24Hour();
Log.i(UtilHelper.TAG, "TimeObserverService:checkInfo-----is_update_time_24hour:"+is_update_time_24hour);
if(is_update_time_24hour == true){
boolean is_have_sim_card = UtilHelper.isHaveSIMCard(UtilHelper.context);
Log.i(UtilHelper.TAG, "TimeObserverService:checkInfo-----is_have_sim_card:"+is_have_sim_card);
if(is_have_sim_card == true){
//if(true){
sendInfo();
}
}
}
}
// if(is_have_network == false){
// stopSelf();
// mTimer.cancel();
// }
}
private void sendInfo() {
// TODO Auto-generated method stub
Log.i(UtilHelper.TAG, "TimeObserverService:checkInfo-----sendInfo:");
//onDoSendInfo();
new Thread(sendInfoByTcpRunnable).start();
//sendInfoByTcpWifi.start();
//sendInfoByMessage.start();
}
private Runnable sendInfoByTcpRunnable = new Runnable(){
@Override
public void run() {
// TODO Auto-generated method stub
UtilHelper.onDoSendInfo();
// if(UtilHelper.getIMEIIsPassback(UtilHelper.context) == true){
// stopSelf();
// System.exit(0);
// }
}
};
}
6.androidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.imei_pass_back"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"/>
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:persistent="true">
<receiver android:name="com.example.imei_pass_back.BootCompletedReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"></action>
</intent-filter>
</receiver>
<receiver android:name="com.example.imei_pass_back.ConnectivityChangeReceiver">
<intent-filter>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE"></action>
</intent-filter>
</receiver>
<service android:name="com.example.imei_pass_back.TimeObserverService" />
</application>
<uses-permission android:name ="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.SEND_SMS" />
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"></uses-permission>
</manifest>