ANDROID下获取IMSI及IMEI码

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>




  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值