Android-API 23-无法显示传入的短信
我试图在屏幕上显示收到的短信。但当我发送新短信时,什么都没有发生 我正在使用Nexus5(API23)模拟器 调试时我得到: 权限拒绝:接收意图{act=android.provider.Telephony.SMS_RECEIVED flg=0x8000010(具有额外功能)}需要android.Permission.RECEIVE_SMS,因为发件人com.android.phone 在MainActivity.Java中,我处理权限Android-API 23-无法显示传入的短信,android,android-6.0-marshmallow,android-permissions,Android,Android 6.0 Marshmallow,Android Permissions,我试图在屏幕上显示收到的短信。但当我发送新短信时,什么都没有发生 我正在使用Nexus5(API23)模拟器 调试时我得到: 权限拒绝:接收意图{act=android.provider.Telephony.SMS_RECEIVED flg=0x8000010(具有额外功能)}需要android.Permission.RECEIVE_SMS,因为发件人com.android.phone 在MainActivity.Java中,我处理权限 @TargetApi(Build.VERSION_CODE
@TargetApi(Build.VERSION_CODES.M)
public void getPermissionToReceiveSMS() {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.RECEIVE_SMS)
!= PackageManager.PERMISSION_GRANTED) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (shouldShowRequestPermissionRationale(Manifest.permission.RECEIVE_SMS)) {
// Show our own UI to explain to the user why we need to read the contacts
// before actually requesting the permission and showing the default UI
}
}
requestPermissions(new String[]{Manifest.permission.RECEIVE_SMS},REQUEST_RECEIVE_SMS);
}
}
我做错了什么?
你能帮帮我吗?
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.globa8track.gtw">
<uses-permission android:name="android.permission.READ_SMS" />
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.SEND_SMS" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<receiver android:name=".gtwWidget">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<meta-data
android:name="android.appwidget.provider"
android:resource="@xml/gtw_widget_info" />
</receiver>
<receiver android:name=".smsReceiver">
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action." />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
我缺少AndroidManifest.xml上的主内部活动标记
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
删除祝酒词
并使用其他内容,如日志
语句或断点。@Commonware tks用于回复,但为什么?Toast在Api23中不起作用?Toast
可以。这是一个糟糕的调试工具,我试图从BroadcastReceiver
@commonware的onReceive()
这样的地方避免使用它,但是日志不仅仅是为了调试?我应该使用哪个日志?Log.i?我认为重点是使用调试工具,首先确保您甚至可以使用Toast语句。这样,你就不必说“我认为这是因为吐司不起作用。”你应该知道什么东西不起作用,然后从那里着手解决这个问题。如果一开始你甚至不知道什么东西坏了,你就不能调试它。
package com.globa8track.gtw;
import android.Manifest;
import android.annotation.TargetApi;
import android.content.pm.PackageManager;
import android.os.Build;
import android.support.annotation.NonNull;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
/**
* Id to identify a read sms permission request
*
*/
private static final int REQUEST_READ_SMS = 1;
private static final int REQUEST_READ_PHONE_STATE = 2;
private static final int REQUEST_RECEIVE_SMS = 3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getPermissionToToast();
getPermissionToReadSMSinbox();
getPermissionToReceiveSMS();
}
// Called when the user is performing an action which requires the app to read the
// sms inbox
@TargetApi(Build.VERSION_CODES.M)
public void getPermissionToReadSMSinbox() {
// 1) Use the support library version ContextCompat.checkSelfPermission(...) to avoid
// checking the build version since Context.checkSelfPermission(...) is only available
// in Marshmallow
// 2) Always check for permission (even if permission has already been granted)
// since the user can revoke permissions at any time through Settings
if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_SMS)
!= PackageManager.PERMISSION_GRANTED) {
// The permission is NOT already granted.
// Check if the user has been asked about this permission already and denied
// it. If so, we want to give more explanation about why the permission is needed.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (shouldShowRequestPermissionRationale(Manifest.permission.READ_SMS)) {
// Show our own UI to explain to the user why we need to read the contacts
// before actually requesting the permission and showing the default UI
}
}
// Fire off an async request to actually get the permission
// This will show the standard permission request dialog UI
requestPermissions(new String[]{Manifest.permission.READ_SMS},REQUEST_READ_SMS);
}
}
@TargetApi(Build.VERSION_CODES.M)
public void getPermissionToToast() {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_PHONE_STATE)
!= PackageManager.PERMISSION_GRANTED) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (shouldShowRequestPermissionRationale(Manifest.permission.READ_PHONE_STATE)) {
// Show our own UI to explain to the user why we need to read the contacts
// before actually requesting the permission and showing the default UI
}
}
requestPermissions(new String[]{Manifest.permission.READ_PHONE_STATE},REQUEST_READ_PHONE_STATE);
}
}
@TargetApi(Build.VERSION_CODES.M)
public void getPermissionToReceiveSMS() {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.RECEIVE_SMS)
!= PackageManager.PERMISSION_GRANTED) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (shouldShowRequestPermissionRationale(Manifest.permission.RECEIVE_SMS)) {
// Show our own UI to explain to the user why we need to read the contacts
// before actually requesting the permission and showing the default UI
}
}
requestPermissions(new String[]{Manifest.permission.RECEIVE_SMS},REQUEST_RECEIVE_SMS);
}
}
// Callback with the request from calling requestPermissions(...)
@Override
public void onRequestPermissionsResult(int requestCode,
@NonNull String permissions[],
@NonNull int[] grantResults) {
// Make sure it's our original READ_CONTACTS request
if (requestCode == REQUEST_READ_SMS) {
if (grantResults.length == 1 &&
grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Toast.makeText(this, "READ_SMS permission granted", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "READ_SMS permission denied", Toast.LENGTH_SHORT).show();
}
} else {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
if(requestCode == REQUEST_READ_PHONE_STATE){
if (grantResults.length == 1 &&
grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Toast.makeText(this, "READ_PHONE_STATE permission granted", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "READ_PHONE_STATE permission denied", Toast.LENGTH_SHORT).show();
}
}else{
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
if(requestCode == REQUEST_RECEIVE_SMS){
if (grantResults.length == 1 &&
grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Toast.makeText(this, "RECEIVE_SMS permission granted", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "RECEIVE_SMS permission denied", Toast.LENGTH_SHORT).show();
}
}else{
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
}
}
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>