Android MTK平台 实现用命令写手机IMEI号

转载:https://blog.csdn.net/marine8888/article/details/7958531

涉及内容:

1. 获取Phone对象;

2. AT command;

3.Java正则表达式;

实现思路:

从拨号盘输入含有IMEI号的字串,提取字串中的IMEI,通过Intent,将IMEI号传入写IMEI号的Activity。

*************************************************************************************

1. 获取Phone对象;

刚开始想在拨号盘的应用中获取Phone对象耗费了很多时间,还是没有搞定,

最后不得不另写一个APK来完成写IMEI号的功能。如有朋友知道如何在任何文件中获取Phone对象,麻烦告知。


关于AndroidManifest.xml:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.wirteimei"
    android:sharedUserId="android.uid.phone" //必须包含该属性
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="15" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity android:name=".MainActivity"
            android:process="com.android.phone" //必须包含该属性

            android:label="@string/title_activity_main" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
               <!-- <category android:name="android.intent.category.LAUNCHER" /> -->
            </intent-filter>
        </activity>
    </application>

</manifest>
  
  

另需使用platform给Apk签名;


有了上述条件,下面就是实现写IMEI号的功能了。

2. 写IMEI号

a. 获取IMEI号;

通过拨号盘获取输入的Imei号,例如在拨号盘输入一组特定形式的字串,形式如*#123456789012345#6666#,

然后实现对该类型字串的检测,这里使用的是正则表达式;

private static final String PATTERN_IMEI_CMD = "^\\*\\#\\d{15}\\#6{4}\\#$";//检测是否是写IMEI号的字串
private static final String PATTERN_IMEI_VALUE = "\\d{15}";//提取IMEI号

Pattern pattern = Pattern.compile(PATTERN_IMEI_CMD);
        Matcher matcher = pattern.matcher(input);
        if(matcher.matches()){
            Pattern pn = Pattern.compile(PATTERN_IMEI_VALUE);
            Matcher mc = pn.matcher(matcher.group());
            String imeiString = "";
            while(mc.matches()){
                imeiString = mc.group();
            }

            Log.d(TAG,"_______________imeiString = "+ imeiString);
            if(imeiString == null || imeiString.isEmpty()){
                return false;
            }
            Intent it = new Intent();
            it.setClassName("com.example.writeimei", "com.example.writeimei.MainActivity");
            it.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            it.putExtra("IMEI_STRING", imeiString);
            context.startActivity(it);
        }
  
  



b.写入IMEI号;

写入IMEI号的Activity,MainActivity.java:

package com.example.wirteimei;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.android.internal.telephony.Phone;
import com.android.internal.telephony.PhoneFactory;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.os.AsyncResult;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;


public class MainActivity extends Activity {

    private static final int EVENT_WRITE_IMEI = 7;
    private static final String TAG = "MainActivity";
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Intent intent = getIntent();
        String imeiString = intent.getStringExtra("IMEI_STRING");

        Log.d(TAG,"____imeiString = "+imeiString);
        String[] ImeiString = new String[2];
  
  
  //AT Command
        ImeiString[0] = "AT+EGMR=1,7,\"" + imeiString + "\"";//single sim card
        Phone phone = PhoneFactory.getDefaultPhone();//获取Phone对象
        phone.invokeOemRilRequestStrings(ImeiString, new WriteImeiHandler().obtainMessage(EVENT_WRITE_IMEI));//发送更新IMEI号AT command
    }

    private String getImeiString(String input){
        Pattern pattern = Pattern.compile("^\\*\\#\\d{15}\\#6{4}\\#$");
        Matcher matcher = pattern.matcher(input);
        while(matcher.matches()){
            Pattern pattern1 = Pattern.compile("\\d{15}");
            Matcher matcher1 = pattern1.matcher(matcher.group());
            while(matcher1.find()){
                return matcher1.group();
            }
        }
        return null;
    }

    class WriteImeiHandler extends Handler{
        BtnClickListener mListener = new BtnClickListener();

        public void handleMessage(Message message){
            AsyncResult ar = (AsyncResult)message.obj;
            switch(message.what){
            case EVENT_WRITE_IMEI:
                if (ar.exception == null) {
                    AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
                    builder.setTitle("IMEI WRITE");
                    builder.setMessage("The IMEI is writen successfully.");
                    builder.setPositiveButton("OK" , mListener);
                    builder.setCancelable(false);
                    builder.create().show();
                } else {
                    AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
                    builder.setTitle("IMEI WRITE");
                    builder.setMessage("Fail to write IMEI due to radio unavailable or something else.");
                    builder.setPositiveButton("OK" , mListener);
                    builder.setCancelable(false);
                    builder.create().show();
                }
            }
        }
    }

    class BtnClickListener implements DialogInterface.OnClickListener{
        public void onClick(DialogInterface dialog, int which){
            MainActivity.this.finish();
        }
    }

}

  
  





猜你喜欢

转载自blog.csdn.net/zengrunxiu/article/details/82153658