为什么即使我已经给出了代码中的所有权限,该代码也没有在emulator中运行? TelephonyManager.java package com.lovepurohit.project15; /** *用于telephony manager的程序,该程序意味着访问相关的详细信息 *sim卡和电话号码,如IMEI号码、设备类型等 * *为此,我们使用包中的TelephonyManager类 *android.telephony.telephonymanagerativity; */ 导入android.content.Context; 导入android.os.Bundle; 导入android.support.v7.app.AppActivity; //导入我们的新包 导入android.telephony.TelephonyManager; 导入android.widget.TextView; /** *由Love于2017年7月4日创作。 */ 公共类TelephonyManager实用性扩展了AppCompatActivity{ 文本视图文本视图; @凌驾 创建时受保护的void(Bundle savedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.activity\u manager\u telephony); //获取身份证 textView=(textView)findViewById(R.id.textView); //访问服务以执行与电话相关的操作 TelephonyManager tm=(TelephonyManager)getSystemService(Context.TELEPHONY_服务); /** *获取电话类型 *手机是cdma还是gsm */ int type=tm.getPhoneType(); //获取其他详细信息,如IMEI编号和设备id等 字符串IMEINumber=tm.getDeviceId(); //正在获取订户ID 字符串subscriberID=tm.getDeviceId(); //获取Sim卡序列号 字符串SIMSerialNumber=tm.getSimSerialNumber(); //获取网络详细信息 字符串networkCountryISO=tm.getNetworkCountryIso(); //获取sim卡国家/地区信息 字符串SIMCountryISO=tm.getSimCountryIso(); //获取运行应用程序的软件 字符串softwareVersion=tm.getDeviceSoftwareVersion(); //获取正在运行应用的语音邮件号码 字符串voiceMailNumber=tm.getVoiceMailNumber(); 字符串strPhoneType=“”; 开关(类型){ 案例(TelephonyManager.PHONE\u TYPE\u CDMA): strPhoneType=“CDMA”; 打破 案例(TelephonyManager.PHONE\u TYPE\u GSM): strPhoneType=“GSM”; 打破 案例(TelephonyManager.PHONE\u TYPE\u NONE): strPhoneType=“无”; 打破 } 布尔值isRoaming=tm.isNetworkRoaming(); String phoneInfo=“电话详细信息:\n”; phoneInfo+=“\n IMEI编号:”+IMEINumber; phoneInfo+=“\n SubscriberID:”+SubscriberID; phoneInfo+=“\n SIM卡序列号:”+SIM卡序列号; phoneInfo+=“\n网络国家/地区ISO:”+networkCountryISO; phoneInfo+=“\n SIMCountryISO:”+SIMCountryISO; phoneInfo+=“\n软件版本:”+softwareVersion; phoneInfo+=“\n VoiceMailNumber:”+VoiceMailNumber; phoneInfo+=“\n电话类型:”+strPhoneType; phoneInfo+=“\n正在漫游?:”+正在漫游; textView.setText(phoneInfo); } } AndroidManifest.xml
我必须在真正的设备上运行它吗?我已经添加了所有权限,但它在模拟器中不起作用。我使用的是API 23,它是棉花糖,你可以在onCreate方法中这样做。我这样做是为了写外部存储,你可以在读电话状态下做同样的事情为什么即使我已经给出了代码中的所有权限,该代码也没有在emulator中运行? TelephonyManager.java package com.lovepurohit.project15; /** *用于telephony manager的程序,该程序意味着访问相关的详细信息 *sim卡和电话号码,如IMEI号码、设备类型等 * *为此,我们使用包中的TelephonyManager类 *android.telephony.telephonymanagerativity; */ 导入android.content.Context; 导入android.os.Bundle; 导入android.support.v7.app.AppActivity; //导入我们的新包 导入android.telephony.TelephonyManager; 导入android.widget.TextView; /** *由Love于2017年7月4日创作。 */ 公共类TelephonyManager实用性扩展了AppCompatActivity{ 文本视图文本视图; @凌驾 创建时受保护的void(Bundle savedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.activity\u manager\u telephony); //获取身份证 textView=(textView)findViewById(R.id.textView); //访问服务以执行与电话相关的操作 TelephonyManager tm=(TelephonyManager)getSystemService(Context.TELEPHONY_服务); /** *获取电话类型 *手机是cdma还是gsm */ int type=tm.getPhoneType(); //获取其他详细信息,如IMEI编号和设备id等 字符串IMEINumber=tm.getDeviceId(); //正在获取订户ID 字符串subscriberID=tm.getDeviceId(); //获取Sim卡序列号 字符串SIMSerialNumber=tm.getSimSerialNumber(); //获取网络详细信息 字符串networkCountryISO=tm.getNetworkCountryIso(); //获取sim卡国家/地区信息 字符串SIMCountryISO=tm.getSimCountryIso(); //获取运行应用程序的软件 字符串softwareVersion=tm.getDeviceSoftwareVersion(); //获取正在运行应用的语音邮件号码 字符串voiceMailNumber=tm.getVoiceMailNumber(); 字符串strPhoneType=“”; 开关(类型){ 案例(TelephonyManager.PHONE\u TYPE\u CDMA): strPhoneType=“CDMA”; 打破 案例(TelephonyManager.PHONE\u TYPE\u GSM): strPhoneType=“GSM”; 打破 案例(TelephonyManager.PHONE\u TYPE\u NONE): strPhoneType=“无”; 打破 } 布尔值isRoaming=tm.isNetworkRoaming(); String phoneInfo=“电话详细信息:\n”; phoneInfo+=“\n IMEI编号:”+IMEINumber; phoneInfo+=“\n SubscriberID:”+SubscriberID; phoneInfo+=“\n SIM卡序列号:”+SIM卡序列号; phoneInfo+=“\n网络国家/地区ISO:”+networkCountryISO; phoneInfo+=“\n SIMCountryISO:”+SIMCountryISO; phoneInfo+=“\n软件版本:”+softwareVersion; phoneInfo+=“\n VoiceMailNumber:”+VoiceMailNumber; phoneInfo+=“\n电话类型:”+strPhoneType; phoneInfo+=“\n正在漫游?:”+正在漫游; textView.setText(phoneInfo); } } AndroidManifest.xml,java,android,android-studio,Java,Android,Android Studio,我必须在真正的设备上运行它吗?我已经添加了所有权限,但它在模拟器中不起作用。我使用的是API 23,它是棉花糖,你可以在onCreate方法中这样做。我这样做是为了写外部存储,你可以在读电话状态下做同样的事情 if (ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
if (ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
if (ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
//Show Information about why you need the permission
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle("Need Storage Permission");
builder.setMessage("We needs storage permission to proceed");
builder.setPositiveButton("Allow", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, EXTERNAL_STORAGE_PERMISSION_CONSTANT);
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
builder.show();
} else if (permissionStatus.getBoolean(Manifest.permission.WRITE_EXTERNAL_STORAGE,false)) {
//Previously Permission Request was cancelled with 'Dont Ask Again',
// Redirect to Settings after showing Information about why you need the permission
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle("Storage Permission");
builder.setMessage("We need storage permission.");
builder.setPositiveButton("Grant", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
sentToSettings = true;
Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
Uri uri = Uri.fromParts("package", getPackageName(), null);
intent.setData(uri);
startActivityForResult(intent, REQUEST_PERMISSION_SETTING);
Toast.makeText(getBaseContext(), "Go to Permissions to Grant Storage", Toast.LENGTH_LONG).show();
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
builder.show();
} else {
//just request the permission
ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, EXTERNAL_STORAGE_PERMISSION_CONSTANT);
}
SharedPreferences.Editor editor = permissionStatus.edit();
editor.putBoolean(Manifest.permission.WRITE_EXTERNAL_STORAGE,true);
editor.commit();
} else {
//You already have the permission, just go ahead.
textView = (TextView) findViewById(R.id.textView);
//Accessing the service for performing operation related to telephony
TelephonyManager tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
/**
* Getting the phone Type
* whether phone is cdma or gsm
*/
int type = tm.getPhoneType();
//Getting other details like IMEI number and device id and etc
String IMEINumber = tm.getDeviceId();
//getting subscriber ID
String subscriberID = tm.getDeviceId();
//Getting Sim serial number
String SIMSerialNumber = tm.getSimSerialNumber();
//Getting network details
String networkCountryISO = tm.getNetworkCountryIso();
//Getting sim country info
String SIMCountryISO = tm.getSimCountryIso();
//Getting the software on which app is running
String softwareVersion = tm.getDeviceSoftwareVersion();
//Getting the voice mail number on which app is running
String voiceMailNumber = tm.getVoiceMailNumber();
String strPhoneType = "";
switch (type) {
case (TelephonyManager.PHONE_TYPE_CDMA):
strPhoneType = "CDMA";
break;
case (TelephonyManager.PHONE_TYPE_GSM):
strPhoneType = "GSM";
break;
case (TelephonyManager.PHONE_TYPE_NONE):
strPhoneType = "NONE";
break;
}
boolean isRoaming = tm.isNetworkRoaming();
String phoneInfo = "Phone Details: \n";
phoneInfo += "\n IMEI Number: " + IMEINumber;
phoneInfo += "\n SubscriberID: " + subscriberID;
phoneInfo += "\n SIM Serial Number: " + SIMSerialNumber;
phoneInfo += "\n Network Country ISO: " + networkCountryISO;
phoneInfo += "\n SIMCountryISO: " + SIMCountryISO;
phoneInfo += "\n SoftwreVersion: " + softwareVersion;
phoneInfo += "\n VoiceMailNumber: " + voiceMailNumber;
phoneInfo += "\n Phone Type: " + strPhoneType;
phoneInfo += "\n Is Roaming? : " + isRoaming;
textView.setText(phoneInfo);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_PERMISSION_SETTING) {
if (ActivityCompat.checkSelfPermission(MainActivity.this, android.Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
// Do the exact thing as in else condition above
}
}
}
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == EXTERNAL_STORAGE_PERMISSION_CONSTANT) {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// Do the exact thing as in else condition above
} else {
if (ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
//Show Information about why you need the permission
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle("Need Storage Permission");
builder.setMessage("This app needs storage permission");
builder.setPositiveButton("Grant", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, EXTERNAL_STORAGE_PERMISSION_CONSTANT);
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
builder.show();
} else {
Toast.makeText(getBaseContext(),"Unable to get Permission",Toast.LENGTH_LONG).show();
}
}
}
}
您必须为实际设备请求运行时许可(请参阅)。 模拟设备没有得到您要求的某些内容,可能会返回null,但其他内容应该会给您提供值。例如,在我的示例中,当我运行以下操作时,模拟设备ID返回00000000000:
TelephonyManager telephonyManager = (TelephonyManager) this.getSystemService(Context.TELEPHONY_SERVICE);
int duration = Toast.LENGTH_SHORT;
DeviceId = telephonyManager.getDeviceId() ;
String testToastInfo = DeviceId + "\n";
Toast.makeText(this,testToastInfo , duration).show();
希望这有帮助 API级别23的运行时权限代码在哪里?此应用程序无法在Emulator上运行。Emulator没有网络sim卡。试着在实际设备上运行它。请阅读-总结是,这不是一个理想的方式来解决志愿者,可能会适得其反获得答案。请不要将此添加到您的问题中。
if (ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
if (ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
//Show Information about why you need the permission
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle("Need Storage Permission");
builder.setMessage("We needs storage permission to proceed");
builder.setPositiveButton("Allow", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, EXTERNAL_STORAGE_PERMISSION_CONSTANT);
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
builder.show();
} else if (permissionStatus.getBoolean(Manifest.permission.WRITE_EXTERNAL_STORAGE,false)) {
//Previously Permission Request was cancelled with 'Dont Ask Again',
// Redirect to Settings after showing Information about why you need the permission
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle("Storage Permission");
builder.setMessage("We need storage permission.");
builder.setPositiveButton("Grant", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
sentToSettings = true;
Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
Uri uri = Uri.fromParts("package", getPackageName(), null);
intent.setData(uri);
startActivityForResult(intent, REQUEST_PERMISSION_SETTING);
Toast.makeText(getBaseContext(), "Go to Permissions to Grant Storage", Toast.LENGTH_LONG).show();
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
builder.show();
} else {
//just request the permission
ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, EXTERNAL_STORAGE_PERMISSION_CONSTANT);
}
SharedPreferences.Editor editor = permissionStatus.edit();
editor.putBoolean(Manifest.permission.WRITE_EXTERNAL_STORAGE,true);
editor.commit();
} else {
//You already have the permission, just go ahead.
textView = (TextView) findViewById(R.id.textView);
//Accessing the service for performing operation related to telephony
TelephonyManager tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
/**
* Getting the phone Type
* whether phone is cdma or gsm
*/
int type = tm.getPhoneType();
//Getting other details like IMEI number and device id and etc
String IMEINumber = tm.getDeviceId();
//getting subscriber ID
String subscriberID = tm.getDeviceId();
//Getting Sim serial number
String SIMSerialNumber = tm.getSimSerialNumber();
//Getting network details
String networkCountryISO = tm.getNetworkCountryIso();
//Getting sim country info
String SIMCountryISO = tm.getSimCountryIso();
//Getting the software on which app is running
String softwareVersion = tm.getDeviceSoftwareVersion();
//Getting the voice mail number on which app is running
String voiceMailNumber = tm.getVoiceMailNumber();
String strPhoneType = "";
switch (type) {
case (TelephonyManager.PHONE_TYPE_CDMA):
strPhoneType = "CDMA";
break;
case (TelephonyManager.PHONE_TYPE_GSM):
strPhoneType = "GSM";
break;
case (TelephonyManager.PHONE_TYPE_NONE):
strPhoneType = "NONE";
break;
}
boolean isRoaming = tm.isNetworkRoaming();
String phoneInfo = "Phone Details: \n";
phoneInfo += "\n IMEI Number: " + IMEINumber;
phoneInfo += "\n SubscriberID: " + subscriberID;
phoneInfo += "\n SIM Serial Number: " + SIMSerialNumber;
phoneInfo += "\n Network Country ISO: " + networkCountryISO;
phoneInfo += "\n SIMCountryISO: " + SIMCountryISO;
phoneInfo += "\n SoftwreVersion: " + softwareVersion;
phoneInfo += "\n VoiceMailNumber: " + voiceMailNumber;
phoneInfo += "\n Phone Type: " + strPhoneType;
phoneInfo += "\n Is Roaming? : " + isRoaming;
textView.setText(phoneInfo);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_PERMISSION_SETTING) {
if (ActivityCompat.checkSelfPermission(MainActivity.this, android.Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
// Do the exact thing as in else condition above
}
}
}
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == EXTERNAL_STORAGE_PERMISSION_CONSTANT) {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// Do the exact thing as in else condition above
} else {
if (ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
//Show Information about why you need the permission
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle("Need Storage Permission");
builder.setMessage("This app needs storage permission");
builder.setPositiveButton("Grant", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, EXTERNAL_STORAGE_PERMISSION_CONSTANT);
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
builder.show();
} else {
Toast.makeText(getBaseContext(),"Unable to get Permission",Toast.LENGTH_LONG).show();
}
}
}
}
TelephonyManager telephonyManager = (TelephonyManager) this.getSystemService(Context.TELEPHONY_SERVICE);
int duration = Toast.LENGTH_SHORT;
DeviceId = telephonyManager.getDeviceId() ;
String testToastInfo = DeviceId + "\n";
Toast.makeText(this,testToastInfo , duration).show();