文档章节

获取手机设备信息  IMEI

BigGod
 BigGod
发布于 2016/06/12 10:06
字数 1103
阅读 719
收藏 0

Phonegap  获取手机设备信息  IMEI

phonegap的Device 提供有:

device.model     :返回设备的模型或产品的名称

device.cordova  :返回cordova的版本

device.uuid        :返回手机 uuid 

device.version   :返回系统版本  

device.platform  :返回手机的平台信息  (android/ios 等等)

唯独没有Imei 的获取方法 

这里提供增加一个imei的获取

Device调用方法参考 https://www.npmjs.com/package/cordova-plugin-device

<1> 我们从命令行进入 到工程目录下的  plugins文件夹 

<2> 现在开始下载插件  

cordova plugin add org.apache.cordova.device

<3> 添加android 平台工程 

cordova platform add android

<4> 编译android工程

cordova build

至此  devices 已经生成...

要获取imei  需要改动 原生的Device   2个地方

第一个是  assets 目录下 www/plugins 里面的org.apache.cordova.device / device.js

cordova.define("org.apache.cordova.device.device", function(require, exports, module) { /*
 *
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 *
*/

var argscheck = require('cordova/argscheck'),
    channel = require('cordova/channel'),
    utils = require('cordova/utils'),
    exec = require('cordova/exec'),
    cordova = require('cordova');

channel.createSticky('onCordovaInfoReady');
// Tell cordova channel to wait on the CordovaInfoReady event
channel.waitForInitialization('onCordovaInfoReady');

/**
 * This represents the mobile device, and provides properties for inspecting the model, version, UUID of the
 * phone, etc.
 * @constructor
 */
function Device() {
    this.available = false;
    this.platform = null;
    this.version = null;
    this.uuid = null;
    this.cordova = null;
    this.model = null;
    //添加imei
    this.imei = null;

    var me = this;

    channel.onCordovaReady.subscribe(function() {
        me.getInfo(function(info) {
            //ignoring info.cordova returning from native, we should use value from cordova.version defined in cordova.js
            //TODO: CB-5105 native implementations should not return info.cordova
            var buildLabel = cordova.version;
            me.available = true;
            me.platform = info.platform;
            me.version = info.version;
            me.uuid = info.uuid;
            me.cordova = buildLabel;
            me.model = info.model;
            //添加imei
            me.imei = info.imei;
            channel.onCordovaInfoReady.fire();
        },function(e) {
            me.available = false;
            utils.alert("[ERROR] Error initializing Cordova: " + e);
        });
    });
}

/**
 * Get device info
 *
 * @param {Function} successCallback The function to call when the heading data is available
 * @param {Function} errorCallback The function to call when there is an error getting the heading data. (OPTIONAL)
 */
Device.prototype.getInfo = function(successCallback, errorCallback) {
    argscheck.checkArgs('fF', 'Device.getInfo', arguments);
    exec(successCallback, errorCallback, "Device", "getDeviceInfo", []);
};

module.exports = new Device();

});
 

第二个是 在 src 当中 修改device类

/*
       Licensed to the Apache Software Foundation (ASF) under one
       or more contributor license agreements.  See the NOTICE file
       distributed with this work for additional information
       regarding copyright ownership.  The ASF licenses this file
       to you under the Apache License, Version 2.0 (the
       "License"); you may not use this file except in compliance
       with the License.  You may obtain a copy of the License at

         http://www.apache.org/licenses/LICENSE-2.0

       Unless required by applicable law or agreed to in writing,
       software distributed under the License is distributed on an
       "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
       KIND, either express or implied.  See the License for the
       specific language governing permissions and limitations
       under the License.
*/
package org.apache.cordova.device;

import java.util.TimeZone;

import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaInterface;
import org.apache.cordova.CordovaPlugin;
import org.apache.cordova.CordovaWebView;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.provider.Settings;
import android.telephony.TelephonyManager;
import android.util.Log;

public class Device extends CordovaPlugin {
    public static final String TAG = "Device";

    public static String cordovaVersion = "dev";              // Cordova version
    public static String platform;                            // Device OS
    public static String uuid;                                // Device UUID

    private static final String ANDROID_PLATFORM = "Android";
    private static final String AMAZON_PLATFORM = "amazon-fireos";
    private static final String AMAZON_DEVICE = "Amazon";

    /**
     * Constructor.
     */
    public Device() {
    }

    /**
     * Sets the context of the Command. This can then be used to do things like
     * get file paths associated with the Activity.
     *
     * @param cordova The context of the main Activity.
     * @param webView The CordovaWebView Cordova is running in.
     */
    public void initialize(CordovaInterface cordova, CordovaWebView webView) {
        super.initialize(cordova, webView);
        Device.uuid = getUuid();
    }

    /**
     * Executes the request and returns PluginResult.
     *
     * @param action            The action to execute.
     * @param args              JSONArry of arguments for the plugin.
     * @param callbackContext   The callback id used when calling back into JavaScript.
     * @return                  True if the action was valid, false if not.
     */
    public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
        if (action.equals("getDeviceInfo")) {
            JSONObject r = new JSONObject();
            r.put("uuid", Device.uuid);
            r.put("version", this.getOSVersion());
            r.put("platform", this.getPlatform());
            r.put("cordova", Device.cordovaVersion);
            r.put("model", this.getModel());
            //添加imei 的返回值
            r.put("imei", this.imei());
            callbackContext.success(r);
        }
        else {
            return false;
        }
        return true;
    }

    //--------------------------------------------------------------------------
    // LOCAL METHODS
    //--------------------------------------------------------------------------
    
    // 获取本地Imei号码 
    private String imei() {
//    	String 	Imei = ((TelephonyManager) cordova.getActivity().getSystemService(cordova.getActivity().TELEPHONY_SERVICE))
//    			.getDeviceId();
//		return Imei;
    	
    	   TelephonyManager systemService = (TelephonyManager)cordova.getActivity().getSystemService(Context.TELEPHONY_SERVICE);
    	   String deviceId = systemService.getDeviceId();
    	   Log.i("123", deviceId);
    	   
		return systemService.getDeviceId();
	}

	/**
     * Get the OS name.
     * 
     * @return
     */
    public String getPlatform() {
        String platform;
        if (isAmazonDevice()) {
            platform = AMAZON_PLATFORM;
        } else {
            platform = ANDROID_PLATFORM;
        }
        return platform;
    }

    /**
     * Get the device's Universally Unique Identifier (UUID).
     *
     * @return
     */
    public String getUuid() {
        String uuid = Settings.Secure.getString(this.cordova.getActivity().getContentResolver(), android.provider.Settings.Secure.ANDROID_ID);
        return uuid;
    }

    /**
     * Get the Cordova version.
     *
     * @return
     */
    public String getCordovaVersion() {
        return Device.cordovaVersion;
    }

    public String getModel() {
        String model = android.os.Build.MODEL;
        return model;
    }

    public String getProductName() {
        String productname = android.os.Build.PRODUCT;
        return productname;
    }

    /**
     * Get the OS version.
     *
     * @return
     */
    public String getOSVersion() {
        String osversion = android.os.Build.VERSION.RELEASE;
        return osversion;
    }

    public String getSDKVersion() {
        @SuppressWarnings("deprecation")
        String sdkversion = android.os.Build.VERSION.SDK;
        return sdkversion;
    }

    public String getTimeZoneID() {
        TimeZone tz = TimeZone.getDefault();
        return (tz.getID());
    }

    /**
     * Function to check if the device is manufactured by Amazon
     * 
     * @return
     */
    public boolean isAmazonDevice() {
        if (android.os.Build.MANUFACTURER.equals(AMAZON_DEVICE)) {
            return true;
        }
        return false;
    }

}

 

最后 在androidmanifest.xml 当中 添加权限

    <uses-permission android:name="android.permission.READ_PHONE_STATE" />

这是 获取imei必要的权限

插件获取参数的方法:

    document.addEventListener("deviceready", onDeviceReady, false);
    function onDeviceReady() {
      $scope.devicePlatform = device.platform;
      $scope.model = device.model;
      $scope.deviceID = device.uuid;
      $scope.deviceVersion = device.version;
      $scope.deviceManufacturer = device.manufacturer;
      $scope.isSim = device.isVirtual;
      $scope.string = device.serial;
      $scope.imei = device.imei;

      console.log("设备model:", $scope.model);
      console.log("devicePlatform:", $scope.devicePlatform);
      console.log("deviceID:", $scope.deviceID);
      console.log("版本信息:", $scope.deviceVersion);
      console.log("设备制造商:", $scope.deviceManufacturer);
      console.log("isSim:", $scope.isSim);
      console.log("serial:", $scope.string);
      console.log("imei:", $scope.imei);

      $log.debug("device信息:", angular.toJson(device));

}
 

© 著作权归作者所有

BigGod
粉丝 1
博文 13
码字总数 4842
作品 0
程序员
私信 提问
Android编程获取手机的IMEI

手机在生产时,每部手机均有一个唯一的标识(ID),国际上采用国际移动设备身份码(IMEI, International Mobile Equipment Identity)。IMEI是由15位数字组成的"电子串号",它与每台手机一一对应...

嘿嘿嘿IT
03/31
55
0
Android 获得设备状态信息、Mac地址、IP地址

前言 在我们APP开发时,经常会遇到要获取手机状态信息的场景,像升级时获取版本号,像发生异常时要收集手机信息等等。有些软件还要根据Mac地址来判定当前用户以前是否登录过。也有些需要通过...

晚茜
2018/12/28
0
0
android手机获取手机唯一识别号

一些特殊的代码,对于手机还是挺有用的 显示IMEI: #06# 开启语音拨号记录 ##8351## 关闭语音拨号记录 ##8350## 手机信息/设置 ##4636## 现场测试 ##7262626## PUK解锁 (从紧急拨号界面) 05<...

xiahuawuyu
2012/10/13
1K
0
android 获取imei(国际移动设备身份码)号码详解及其应用场景

IMEI简介: IMEI(International Mobile Equipment Identity)是国际移动设备身份码的缩写,国际移动装备辨识码,是由15位数字组成的"电子串号",它与每台手机一一对应,而且该码是全世界唯一的...

长平狐
2012/09/03
361
0
Android 支付SDK所需参数

需要获取的参数:获取手机网络、获取手机ip、获取IMSI、获取IMEI、获取安卓版本、获取设备品牌、获取设备制造商 1.手机网络类型,对吧? 2.手机IP,就是公网的IP吧,这个估计需要我去访问第三...

大凡哥哥
2017/05/11
4
2

没有更多内容

加载失败,请刷新页面

加载更多

《Designing.Data-Intensive.Applications》笔记 四

第九章 一致性与共识 分布式系统最重要的的抽象之一是共识(consensus):让所有的节点对某件事达成一致。 最终一致性(eventual consistency)只提供较弱的保证,需要探索更高的一致性保证(stro...

丰田破产标志
今天
6
0
docker 使用mysql

1, 进入容器 比如 myslq1 里面进行操作 docker exec -it mysql1 /bin/bash 2. 退出 容器 交互: exit 3. mysql 启动在容器里面,并且 可以本地连接mysql docker run --name mysql1 --env MY...

之渊
今天
7
0
python数据结构

1、字符串及其方法(案例来自Python-100-Days) def main(): str1 = 'hello, world!' # 通过len函数计算字符串的长度 print(len(str1)) # 13 # 获得字符串首字母大写的...

huijue
今天
5
0
OSChina 周日乱弹 —— 我,小小编辑,食人族酋长

Osc乱弹歌单(2019)请戳(这里) 【今日歌曲】 @宇辰OSC :分享娃娃的单曲《飘洋过海来看你》: #今日歌曲推荐# 《飘洋过海来看你》- 娃娃 手机党少年们想听歌,请使劲儿戳(这里) @宇辰OSC...

小小编辑
今天
1K
11
MongoDB系列-- SpringBoot 中对 MongoDB 的 基本操作

SpringBoot 中对 MongoDB 的 基本操作 Database 库的创建 首先 在MongoDB 操作客户端 Robo 3T 中 创建数据库: 增加用户User: 创建 Collections 集合(类似mysql 中的 表): 后面我们大部分都...

TcWong
今天
40
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部