android电话管理器权限

olmpazwi  于 2021-06-27  发布在  Java
关注(0)|答案(1)|浏览(451)

我试图编码一个快速的应用程序,给我所需的3g值,当我点击一个按钮。但首先我需要检查我是否连接到3g网络。但是我的权限有一些问题。我有以下代码:

public void calculate(View view) {
    TextView rscp = (TextView) findViewById(R.id.RSCP);
    TextView rssi = (TextView) findViewById(R.id.RSSI);
    TextView ecno = (TextView) findViewById(R.id.EcNo);
    TelephonyManager tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
    if (Arrays.stream(values_3G).anyMatch(n -> n == tm.getDataNetworkType())) {
        for (CellInfo cellInfo : tm.getAllCellInfo()) {
            if (cellInfo instanceof CellInfoWcdma) {
                CellSignalStrengthWcdma cellSignalStrength = ((CellInfoWcdma) cellInfo).getCellSignalStrength();
                rscp.setText(cellSignalStrength.getDbm());
                ecno.setText(cellSignalStrength.getEcNo());
                int rssiValue = -113 + 2 * cellSignalStrength.getAsuLevel();
                rssi.setText(rssiValue);
            }
        }
    } else {
        rscp.setText(0);
        rssi.setText(0);
        ecno.setText(0);
        Log.i(TAG, "No 3G Mobile connection detected!");
        Toast.makeText(getApplicationContext(), "Connect to 3G", Toast.LENGTH_SHORT).show();

    }

}
``` `tm.getDataNetworkType()` 正在给我以下关于read\u phone\u state的问题:

Call requires permission which may be rejected by user: code should explicitly check to see if permission is available (with checkPermission) or explicitly handle a potential SecurityException

如果我按照说明在android studio中检查权限,我会得到以下结果:

if (ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return;
}

我需要在括号里填什么?
jvidinwx

jvidinwx1#

创建权限请求代码的全局final int

final int PHONE_REQUEST_CODE = 101;

未经批准的,申请许可

if (ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {

    requestPermissions(new String[]{Manifest.permission.READ_PHONE_STATE},
                       PHONE_REQUEST_CODE); // triggers onRequestPermissionsResult()

} else {
     // calculate(myView); // Do whatever you want as the permission is already granted
}

和覆盖 onRequestPermissionsResult() 活动中

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    if (grantResults.length == 0)
        return;

    if (requestCode == PHONE_REQUEST_CODE) {
        if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
           // calculate(myView); // Do whatever you want after the permission is granted
}

并在manifest.xml中添加权限。

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

相关问题