1. 程式人生 > >用代碼寫IMEI

用代碼寫IMEI

ada ble cte fin actor ota android sts sage

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