tm.getDeviceId() is deprecated?

核能气质少年 提交于 2019-11-26 09:54:30

问题


I\'m getting the IMEI and device Id\'s, so here I am getting a problem getDeviceId() is deprecated.

TelephonyManager tm = (TelephonyManager) 
                 getSystemService(this.TELEPHONY_SERVICE);

imei = tm.getDeviceId();
device = Settings.Secure.getString(this.getContentResolver(),
           Settings.Secure.ANDROID_ID);

回答1:


getDeviceId()

Returns the unique device ID of a subscription, for example, the IMEI for GSM and the MEID for CDMA phones. Return null if device ID is not available.

This method was deprecated in API level 26.

Use (@link getImei} which returns IMEI for GSM

or (@link getMeid} which returns MEID for CDMA.

for more information read TelephonyManager

Try this to get IMEI

 @RequiresApi(api = Build.VERSION_CODES.O)
 TelephonyManager tm = (TelephonyManager)
            getSystemService(this.TELEPHONY_SERVICE);
    String imei = tm.getImei();

OR

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            String imei = telephonyMgr.getImei();
} else {
            String imei = telephonyMgr.getDeviceId();
}

Try this to get MEID

@RequiresApi(api = Build.VERSION_CODES.O)
TelephonyManager tm = (TelephonyManager)
            getSystemService(this.TELEPHONY_SERVICE);

    String meid=tm.getMeid();

OR

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            String meid=tm.getMeid();
} 



回答2:


This is my solution:

@SuppressWarnings("deprecation")
private String getIMEINumber() {
    String IMEINumber = "";
    if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.READ_PHONE_STATE) == PackageManager.PERMISSION_GRANTED) {
        TelephonyManager telephonyMgr = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            IMEINumber = telephonyMgr.getImei();
        } else {
            IMEINumber = telephonyMgr.getDeviceId();
        }
    }
    return IMEINumber;
}



回答3:


Kotlin Code for getting DeviceId ( IMEI ) with handling with permission & compatibility check for all android versions :

 val  telephonyManager = getSystemService(Context.TELEPHONY_SERVICE) as TelephonyManager
    if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_PHONE_STATE)
        == PackageManager.PERMISSION_GRANTED) {
        // Permission is  granted
        val imei : String? = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
             telephonyManager.imei
         } else { // older OS  versions
             telephonyManager.getDeviceId()
         }

        imei?.let {
            Log.i("Log", "DeviceId=$imei" )
        }

    } else {  // Permission is not granted

    }

Also add this permission to AndroidManifest.xml :

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



回答4:


Should not it be something like this?

            if (Build.VERSION.SDK_INT >= 26) {
                if (telMgr.getPhoneType() == TelephonyManager.PHONE_TYPE_CDMA) {
                    deviceId = telMgr.getMeid();
                } else if (telMgr.getPhoneType() == TelephonyManager.PHONE_TYPE_GSM) {
                    deviceId = telMgr.getImei();
                } else {
                    deviceId = ""; // default!!!
                }
            } else {
                deviceId = telMgr.getDeviceId();
            }



回答5:


copy and paste my this program and understood. we face issue here only in Permission (Run time and Check Permission Type );- Now i complete it and paste here a accurate program:

import android.*;
import android.Manifest;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.pm.PackageManager;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import java.util.UUID;

public class Login extends AppCompatActivity {

    private Button loginBtn;
    private TextView textView;
    private String IMEINumber;




    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        loginBtn = (Button) findViewById(R.id.loginBtn);
        textView = (TextView) findViewById(R.id.textView);
        final int reqcode = 1;


        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
        {
            String[] per = {Manifest.permission.READ_PHONE_STATE};
            requestPermissions(per, reqcode);
            loginBtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    TelephonyManager tm = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
                    if (ActivityCompat.checkSelfPermission(Login.this, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {
                        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                            IMEINumber = tm.getImei();
                            textView.setText(IMEINumber);
                        }
                    } else {
                        IMEINumber = tm.getDeviceId();
                        textView.setText(IMEINumber);
                    }
                }
            });


        }
    }

}


来源:https://stackoverflow.com/questions/46744104/tm-getdeviceid-is-deprecated

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!