作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用这个简单的代码来检索一些电话设备数据,但是由于某种原因,每当我在模拟器上打开应用程序时,它都会弹出一条消息,说 "Unfortunately *APP NAME* has closed"
然后它关闭应用程序。
这是我正在使用的代码:
TelephonyManager tm=(TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
String IMEINumber=tm.getDeviceId();
Telephonymanager
方法,但没有任何效果,除了:
"getPhoneType();"
getDeviceId: Neither user 10058 nor current process has android.permission.READ_PHONE_STATE
虽然我在 list 中添加了一个权限:
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
最佳答案
你在运行吗Android M
?如果是这样,这是因为声明 permissions
是不够的。在 manifest
.对于一些 permissions
,您必须在 run-time
中明确询问用户:
检查此Link对于 Run time Permission
.
代码 fragment 。
Define this
Globally
in yourActivity
.
private static final int PERMISSION_REQUEST_CODE = 1;
Do this stuff in
onCreate
.
// Here, thisActivity is the current activity
if (ContextCompat.checkSelfPermission(TestingActivity.this,
Manifest.permission.READ_PHONE_STATE)
!= PackageManager.PERMISSION_GRANTED) {
// Should we show an explanation?
if (ActivityCompat.shouldShowRequestPermissionRationale(TestingActivity.this,
Manifest.permission.READ_PHONE_STATE)) {
// Show an expanation to the user *asynchronously* -- don't block
// this thread waiting for the user's response! After the user
// sees the explanation, try again to request the permission.
} else {
// No explanation needed, we can request the permission.
ActivityCompat.requestPermissions(TestingActivity.this,
new String[]{Manifest.permission.READ_PHONE_STATE},
PERMISSION_REQUEST_CODE);
// MY_PERMISSIONS_REQUEST_READ_CONTACTS is an
// app-defined int constant. The callback method gets the
// result of the request.
}
}
Finally
overrride onRequestPermissionsResult
.
@Override
public void onRequestPermissionsResult(int requestCode,
String permissions[], int[] grantResults) {
switch (requestCode) {
case PERMISSION_REQUEST_CODE: {
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// permission was granted, yay!
} else {
// permission denied, boo! Disable the
// functionality that depends on this permission.
}
return;
}
// other 'case' lines to check for other
// permissions this app might request
}
}
关于Android TelephonyManager 不适用于 android studio 模拟器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37428262/
我是一名优秀的程序员,十分优秀!