ANDROID下获取IMSI及IMEI码

狄宾实
2023-12-01

IMSIInternational Mobile SubscriberIdentification Number

      国际移动用户识别码

IMEIInternational Mobile Equipment Identity

      国际移动设备身份码


1. 创建插件类GetImsiPlugin

package com.em.getImsi;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.content.Context;
import android.telephony.TelephonyManager;
import android.util.Log;
import com.phonegap.api.Plugin;
import com.phonegap.api.PluginResult;
import com.phonegap.api.PluginResult.Status;

/**
 * 获取android的imsi(国际移动用户识别码)和imei(国际移动装备辨识码)
 * 
 * @author yeliping
 * 
 */
public class GetImsiPlugin extends Plugin {
	
	/** get Action */
	public static final String ACTION = "get";
	@Override
	public PluginResult execute(String action, JSONArray arg1, String arg2) {
		PluginResult result = null;
		if (ACTION.equals(action)) {
			//在插件中,获取mTelephonyMgr
			TelephonyManager mTelephonyMgr = (TelephonyManager) this.ctx
					.getSystemService(Context.TELEPHONY_SERVICE);
			Log.d("getImsi", "get mTelephonyMgr " + mTelephonyMgr.toString());
			String imsi = mTelephonyMgr.getSubscriberId();
			String imei = mTelephonyMgr.getDeviceId();
			
			//组件JSONObject对象,用作PluginResult实例化的参数
			JSONObject fileInfo = new JSONObject();
			try {
				fileInfo.put("imsi", imsi);
				fileInfo.put("imei", imei);
			} catch (JSONException e) {
				Log.d("DirectoryListPlugin", "Got JSON Exception "  
                        + e.getMessage());  
                result = new PluginResult(Status.JSON_EXCEPTION);
			}
			Log.d("getImsi", "Returning " + fileInfo.toString());
			result = new PluginResult(Status.OK, fileInfo);
		}else {  
            result = new PluginResult(Status.INVALID_ACTION);  
            Log.d("DirectoryListPlugin", "Invalid action : " + action  
                    + " passed");  
        }  
		return result;
	}
}

使用this.ctx.getSystemService(Context.TELEPHONY_SERVICE)获取管理手机的服务对象TelephonyManager,调用getSubscriberId()获取IMSI码,调用getDeviceId获取IMEI码。

    Action匹配失败返回newPluginResult(Status.INVALID_ACTION);在操作正常的情况下,将IMSI码、IMEI码值采用键值对的方式存放在JSONObject对象中,并用这个对象实例化PluginResult对象返回。

2. 新建phonegap应用

将插件类GetImsiPluginexport为getImsi.jar引入到这个项目路径下;在res/xml/plugin.xml中声明插件

<plugin  name="GetImsiPlugin"  value="com.em.getImsi.GetImsiPlugin"/>

3. 创建插件类js

/** 
 *   
 * @return device imsi and imei 
 */  
var getImsi = {   
/** 
 * @param 
 * @param successCallback The callback which will be called when get action is successful 
 * @param failureCallback The callback which will be called when get action encouters an error 
 */  
    get: function(params,successCallback, failureCallback) {  
        return PhoneGap.exec(successCallback,        //Success callback from the plugin  
                             failureCallback,        //Error callback from the plugin  
                             'GetImsiPlugin',  		//Tell PhoneGap to run "GetImsiPlugin" Plugin ,defined at plugins.xml
                             'get',                 //Tell plugin, which action we want to perform  
                             [params]);           	//params  
    }  
};  

4. 在index.html调用插件类的方法

<!DOCTYPE html> 
<html> 
    <head> 
        <meta charset="utf-8"> 
        <title>getImsi</title> 
 
        <script type="text/javascript" charset="utf-8" src="phonegap.js"></script>
         <script type="text/javascript" charset="utf-8" src="getImsi.js"></script>
        <script  type="text/javascript">
	// 等待加载PhoneGap    
	document.addEventListener("deviceready", onDeviceReady, false);
	
	// PhoneGap加载完成 
	function onDeviceReady() {
		    getImsi.get( 
					null,                          //params null
					function(r){printResult(r);},  //success callback 
					function(e){log(e)}            //fail callback
			); 
		
	}
	//打印结果
	function printResult(fileInfo){    
		var element = document.getElementById('deviceProperties');
		element.innerHTML = 'Device Name: '     + device.name     + '<br />' + 
			'Device PhoneGap: ' + device.phonegap + '<br />' + 
			'Device Platform: ' + device.platform + '<br />' + 
			'Device UUID: '     + device.uuid     + '<br />' + 
			'Device Version: '  + device.version  + '<br />' +
			'Device IMSI: ' + fileInfo.imsi + '<br />' +
			'Device IMEI: ' + fileInfo.imei + '<br />';
	}  
</script>
        
    </head> 
    <body>
         <p id="deviceProperties">Loading device properties...</p>
    </body> 
</html>




 类似资料: