阅读了Android文档之后,我决定进行一次娱乐活动并“阅读来自设备的短信”。
但是,在Manifest中设置权限,并在(java)后面的代码中编写代码以请求权限,并在RequestPermission()
上设置断点之后,我可以看到我的应用程序崩溃是因为“权限被拒绝”。
为什么在我声明了文档告诉我要声明的所有内容并在您使操作系统要求用户授予或拒绝权限的情况下实现了该事情,我为什么仍拒绝权限(甚至不会发生-它不会向我显示一个对话框以授予或拒绝权限-我的应用程序甚至在显示之前就崩溃了!)
我怎么知道发生了什么事?我如何向用户请求[特定] READ_SMS
权限?
请注意,几天来我一直在试图解决此问题,这是我最新的代码,但是大约进行了20次(完整)重写。因此,如果缺少某些东西,它可能已经在那儿了,但是因为它不起作用而被删除了,或者为了进行故障排除以查看什么不起作用而将其删除。
表现:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.me.app">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_SMS" />
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-feature
android:name="android.hardware.telephony"
android:required="true">
</uses-feature>
<grant-uri-permission android:path="content://sms/"
android:pathPattern=".*"
android:pathPrefix="\"/>
<activity
android:name=".Main"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Main.java(onCreate(...)):
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Testing Snackbar", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
RequestPermission(this);
}
Main.java(权限方法):
private static final int MY_PERMISSIONS_REQUEST_READ_SMS = 226;
protected void RequestPermission(Activity activity)
{
if(ContextCompat.checkSelfPermission(activity, Manifest.permission.READ_SMS) != PackageManager.PERMISSION_GRANTED)
{
if(ActivityCompat.shouldShowRequestPermissionRationale(activity, Manifest.permission.READ_SMS)) {
return;
}
else
{
ActivityCompat.requestPermissions(activity, new String[] { Manifest.permission.READ_SMS
}, MY_PERMISSIONS_REQUEST_READ_SMS);
}
}
}
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults)
{
switch(requestCode)
{
case MY_PERMISSIONS_REQUEST_READ_SMS:
{
if(grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED)
{
MessageAdapter messageAdapter = new MessageAdapter(GetMessages());
RecyclerView recList = (RecyclerView) findViewById(R.id.messagesList);
recList.setAdapter(messageAdapter);
// CardView List
LinearLayoutManager llm = new LinearLayoutManager(this.getApplicationContext());
llm.setOrientation(LinearLayoutManager.VERTICAL);
recList.setLayoutManager(llm);
}
else
{
return;
}
return;
}
}
}
最佳答案
您应该修复的一个地方是:
if(ActivityCompat.shouldShowRequestPermissionRationale(activity, Manifest.permission.READ_SMS)) {
return;
}
在这里您只是返回,而实际上您应该向
rationalle
显示为什么需要此权限然后再请求它。通常的方法是使用Snackbar或对话框,或直接立即调用ActivityCompat.requestPermissions(
-至少出于测试目的。[编辑]
Snackbar.make(activity, R.string.rationaleStringId, Snackbar.LENGTH_INDEFINITE)
.setAction(android.R.string.ok, new View.OnClickListener() {
@Override
@TargetApi(Build.VERSION_CODES.M)
public void onClick(View v) {
ActivityCompat.requestPermissions(activity, new String[] {Manifest.permission.READ_SMS}, MY_PERMISSIONS_REQUEST_READ_SMS);
}
}).show();