用代码写IMEI

Posted copyworker

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了用代码写IMEI相关的知识,希望对你有一定的参考价值。

pm:销售要有手动写IMEI的功能,

wo:WTF? 用SN Writer不就是手动吗?

pm:那用代码写

wo:WTF?那怎么写?

pm:你问我啊?

wo:我马上写!

布局:

两个EditText

两个Button

 

MainActivity.java

package com.yyyy.demo;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.os.AsyncResult;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.PowerManager;
import android.os.RemoteException;
import android.os.ServiceManager;
import android.os.SystemProperties;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.android.internal.telephony.ITelephony;
import com.android.internal.telephony.Phone;
import com.android.internal.telephony.PhoneConstants;
import com.android.internal.telephony.PhoneFactory;
import android.telephony.SubscriptionManager;

public class IMEIACTIVITY extends Activity implements OnClickListener {
private Phone mPhone = null;
private PowerManager pm;
static final String LOG_TAG = "IMEI EN";

private Button mBtnImei;
private Button mBtnReboot;

private EditText mEditImei1;
private EditText mEditImei2;

private boolean mAlive = false;

private AlertDialog mAlertDialog;

@Override
public void onDestroy() {
  mAlive = false;
  super.onDestroy();
  if (mAlertDialog != null && mAlertDialog.isShowing()) {
    mAlertDialog.dismiss();
  }
}

@Override
public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.imei);

  Log.v(LOG_TAG, "onCreate");
  mAlive = true;

  mPhone = PhoneFactory.getDefaultPhone();

  pm = (PowerManager) getSystemService(Context.POWER_SERVICE);

  mBtnReboot = (Button) findViewById(R.id.BtnReboot);
  mBtnReboot.setOnClickListener(this);

  // Log.v(LOG_TAG, "Default IMEI:" + imei);
  mEditImei1 = (EditText) findViewById(R.id.IMEI_TXT1);
  mEditImei2 = (EditText) findViewById(R.id.IMEI_TXT2);
  mBtnImei = (Button) findViewById(R.id.WriteIMEI);
  mBtnImei.setOnClickListener(this);

  AlertDialog.Builder builder = new AlertDialog.Builder(IMEIACTIVITY.this);
  mAlertDialog = builder.create();
  mAlertDialog.setButton(DialogInterface.BUTTON_POSITIVE, "OK",
  (DialogInterface.OnClickListener) null);
}

@Override
public void onResume() {
  super.onResume();

}

@Override
public void onClick(View arg0) {
  Log.v(LOG_TAG, "onClick:" + arg0.getId());

  if (arg0.getId() == mBtnImei.getId()) {
    String imeiString1[] = { "AT+EGMR=1,", "" };
    String imeiString2[] = { "AT+EGMR=1,", "" };

    imeiString1[0] = "AT+EGMR=1,7,\"" + mEditImei1.getText() + "\"";
    imeiString2[0] = "AT+EGMR=1,10,\"" + mEditImei2.getText() + "\"";
    mPhone.invokeOemRilRequestStrings(imeiString1,
    mResponseHander.obtainMessage(1));
    Log.v(LOG_TAG, "IMEI String:" + imeiString1[0]);
    mPhone.invokeOemRilRequestStrings(imeiString2,
    mResponseHander.obtainMessage(2));
    Log.v(LOG_TAG, "IMEI String:" + imeiString2[0]);

  }

  if (arg0 == mBtnReboot) {
    rebootAlert();
  }

}

private Handler mResponseHander = new Handler() {
  @Override
  public void handleMessage(Message msg) {

    if (!mAlive) {
      return;
    }

    if (mAlertDialog.isShowing()) {
      mAlertDialog.dismiss();
    }

    AsyncResult ar;
    ar = (AsyncResult) msg.obj;
    switch (msg.what) {

    case 1:

      if (ar.exception == null) {
        mAlertDialog.setTitle("IMEI WRITE");
        mAlertDialog.setMessage("The IMEI is writen successfully.");
        mAlertDialog.show();
      } else {
        mAlertDialog.setTitle("IMEI WRITE");
        mAlertDialog
          .setMessage("Fail to write IMEI due to radio unavailable or something else.");
        mAlertDialog.show();
      }  
      break;
    case 2:
      if (ar.exception == null) {
        Log.d(LOG_TAG, " IMEI2 Success");
      } else {
        Log.d(LOG_TAG, " IMEI2 Fail");
      }
    default:
      break;
    }
  }
};

private void rebootAlert() {
  DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() {
  @Override
  public void onClick(DialogInterface dialog, int which) {
    switch (which) {
      case DialogInterface.BUTTON_POSITIVE:

        pm.reboot("");
        Toast.makeText(getApplicationContext(),
        "Restart Device...", Toast.LENGTH_LONG).show();
      break;
      case DialogInterface.BUTTON_NEGATIVE:
      break;
      default:
      break;
    }
  }
};
AlertDialog.Builder builder = new AlertDialog.Builder(IMEIACTIVITY.this);
  builder.setMessage("Reboot?")
  .setPositiveButton("Yes", dialogClickListener)
  .setNegativeButton("No", dialogClickListener)
  .setTitle("Warning").show();
}

}

 

AndroidManifest.xml

android:sharedUserId="android.uid.phone"必须有

android:process="com.android.phone"注册Activity的时候必须有

 

以上是关于用代码写IMEI的主要内容,如果未能解决你的问题,请参考以下文章

关于安卓系统4.0/5.0/6.0获取单卡手机,双卡手机的imei1,imei2,meid(用反射来实现,史上最详细,最全面获取)--binbinyang

IMEI:358884047296053 IMSI:460027104813435 查下。三星S5830

如何通过代码编程获取设备的 IMEI/ESN 号码但在 android > 6

苹果12屏幕测试代码指令?

在 React Native 中获取 IMEI 号码

怎样才能识别三星手机是港版的机