ionic开发——获取手机唯一标识imei码

首先,我们要添加了cordova-plugin-device插件,具体流程可以查看ionic开发获取手机设备信息点击打开链接

这篇说明如何获取手机的imei码

我们需要在cordova-plugin-device中添加一些获取imei码的代码

首先,在plugins/cordova-plugin-device/www目录下,将device.js文件根据下面做相应更改

/*
 *
 * 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;
    this.manufacturer = null;
    this.isVirtual = null;
    this.serial = null;
    this.imei = null;//添加获取imei功能
    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;
            me.isVirtual = info.isVirtual;
            me.manufacturer = info.manufacturer || 'unknown';
            me.serial = info.serial || 'unknown';
	    me.imei = info.imei;//添加获取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();




然后,在plugins/cordova-plugin-device/src/android/目录下,找到device.java文件根据以下做相应更改

/*  
       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.CordovaWebView;  
import org.apache.cordova.CallbackContext;  
import org.apache.cordova.CordovaInterface;  
import org.apache.cordova.CordovaPlugin;  
import org.apache.cordova.CordovaWebView;  
import org.apache.cordova.CordovaInterface;  
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 ("getDeviceInfo".equals(action)) {  
            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());  
            r.put("manufacturer", this.getManufacturer());  
            r.put("isVirtual", this.isVirtual());  
            r.put("serial", this.getSerialNumber());  
            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;  
    }  
  
    public String getManufacturer() {  
        String manufacturer = android.os.Build.MANUFACTURER;  
        return manufacturer;  
    }  
  
    public String getSerialNumber() {  
        String serial = android.os.Build.SERIAL;  
        return serial;  
    }  
  
    /**  
     * 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;  
    }  
  
    public boolean isVirtual() {  
    return android.os.Build.FINGERPRINT.contains("generic") ||  
        android.os.Build.PRODUCT.contains("sdk");  
    }  
  
}  



最后,在platform/android/AndroidManifest.xml中添加权限

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



上面的都更改好之后


device.imei :返回手机 imei码


如果一些android手机获取到的imei码是000000000000000,这是一些国产的非量产的手机(水货)正常的现象,可以拿其他手机进行测试


获取更多手机设备信息请查看ionic开发——获取手机设备信息点击打开链接

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 6
    评论
1 简介 1、Native APP(原生) 2、Hybrid APP(混合) 3、React Native Hybrid App按网页语言与程序语言的混合,通常分为三种类型:多View混合型,单View混合型,Web主体型。 2 技术 2.1 技术选型 Ionic 2.1.1 Ionic介绍 Ionic是一款基于Angular、Cordova的强大的HTML5移动应用开发框架,可以快速创建一个跨平台的移动医用。可以快速开发移动APP、移动WEB页面、微信公众平台应用,混合APP WEB页面。 2.1.2 Ionic和cordova、Angular关系 Ionic = cordova + Angular + ionic Css Ionic 是完全基于谷歌的 Angular 框架,在 Angular 基础上面做了一些封装,让我们可以更快速和容易的开发移动的项目。Ionic 调用原生的功能是基于 Cordova,Cordova 提供了使用JavaScript 调用 Native 功能,ionic 自己也封装了一套漂亮的 CSS UI 库。 2.1.3 开发工具 Visual Studio Code 2.1.4 管理工具 Git 2.2 环境搭建 2.2.1 概述 2.2.2 JDK 2.2.2.1 版本 jdk1.8 2.2.2.2 配置 JAVA_HOME PATH CLASSPATH 2.2.3 Apache ant 2.2.3.1 版本 1.9.12 2.2.3.2 解压 2.2.3.3 配置 ANT_HOME: D:\android\apache-ant-1.10.4 Path: %ANT_HOME%\bin 2.2.3.4 查看安装版本 ant –v 2.2.4 android SDK 2.2.4.1 版本 r24.4.1 2.2.4.2 配置 ANDROID_SDK_HOME: D:\Android\android-sdk-windows Path:%ANDROID_SDK_HOME%\platform-tools;%ANDROID_SDK_HOME%\tools 2.2.4.3 SDK 通过SDK Manager下载相关的版本 2.2.4.4 查看信息 android -h 2.2.5 node.js 2.2.5.1 版本 8.11.3 2.2.5.2 安装 2.2.5.3 查看版本 node –v npm -v 2.2.6 npm/cnpm 2.2.6.1 安装npm nodejs已经集成了npm 2.2.6.2 安装 cnpm npm install -g cnpm --registry=https://registry.npm.taobao.org 2.2.7 cordova 2.2.7.1 版本 8.0.0 2.2.7.2 安装 npm install -g cordova 可以使用cnpm代替npm cnpm install -g cordova ionic 2.2.7.3 查看版本 cordova -v 2.2.8 ionic 2.2.8.1 版本 3.20.0 2.2.8.2 安装 npm install –g ionic 可以使用cnpm代替npm cnpm install -g cordova ionic 2.2.8.3 查看版本 ionic –v 2.2.8.4 查看信息 ionic info 3 项目 3.1 创建 3.1.1 新建 3.1.2 启动 3.1.3 打包 3.1.3.1 添加 3.1.3.2 生产release版apk(签名后安装) 3.1.3.3 生成debug版apk 3.1.4 签名 3.1.4.1 生成签名文件 keytool -genkey -v -keystore testapp.keystore -alias testapp.keystore -keyalg RSA -validity 20000 3.1.4.2 apk签名 jarsigner -verbose -keystore testapp.keystore -signedjar app-release-signed.apk app-release-unsigned.apk testapp.keystore 3.1.5 安装 安卓手机安装使用

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Mickey_于浩

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值