AndroidManifest.xml中SMS接收权限的基本错误

Ric*_*een 5 java sms android android-manifest android-permissions

我知道之前已经问了十几次,但是我仍然得到了这个xml配置的权限错误.我已经仔细研究了其他的回应.我正在使用API​​级别23.有人可以指出错误吗?错误很明显:

09-12 09:13:40.016 1295-1309 /?W/BroadcastQueue:权限拒绝:接收Intent {act = android.provider.Telephony.SMS_RECEIVED flg = 0x8000010(有额外内容)}到com.example.richard.simplesmstoast/.SmsReceiver需要android.permission.RECEIVE_SMS,因为发件人com.android .phone(uid 1001)

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >

    <activity
        android:name=".MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <receiver
        android:name=".SmsReceiver"
        android:enabled="true"
        android:exported="true">
        <intent-filter android:priority="999" >
            <action android:name="android.provider.Telephony.SMS_RECEIVED" />
        </intent-filter>
    </receiver>
</application>

<uses-permission android:name="android.permission.RECEIVE_SMS"/>
Run Code Online (Sandbox Code Playgroud)

Yaz*_*llo 17

问题在于Android M的新权限模型(api 23):

快速浏览

  • 如果您的应用面向M预览SDK,则会提示用户在运行时授予权限,而不是安装时间.
  • 用户可以随时从"应用程序设置"屏幕撤消权限.
  • 您的应用需要检查它是否具有每次运行时所需的权限.

为SMS案例文档带来一个例子:

例如,假设应用程序在其清单中列出它需要SEND_SMS和RECEIVE_SMS权限,这两个权限都属于android.permission-group.SMS.当应用需要发送消息时,它会请求SEND_SMS权限.系统向用户显示一个对话框,询问应用程序是否可以访问SMS.如果用户同意,系统会向应用程序授予其请求的SEND_SMS权限.稍后,该应用程序请求RECEIVE_SMS.系统会自动授予此权限,因为用户已经批准了同一权限组中的权限.

解决方案:

  • 正确的方式 - 首先请求许可.
  • 懒惰的方式 - 设置targetSdk 22


小智 5

首先,必须在AndroidManifest.xml中声明RECEIVE_SMS权限。

...
<uses-permission android:name="android.permission.RECEIVE_SMS" />
...
<receiver
    android:name=".receiver.IncomingSmsReceiver"
    android:enabled="true"
    android:exported="true">
    <intent-filter>
        <action android:name="android.provider.Telephony.SMS_RECEIVED" />
    </intent-filter>
</receiver>
Run Code Online (Sandbox Code Playgroud)

然后,从API级别23开始,我们需要在运行时请求RECEIVE_SMS权限。注意这一点很重要。 https://developer.android.com/training/permissions/requesting.html

public class MainActivity extends AppCompatActivity {

    private static final int PERMISSIONS_REQUEST_RECEIVE_SMS = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        // Request the permission immediately here for the first time run
        requestPermissions(Manifest.permission.RECEIVE_SMS, PERMISSIONS_REQUEST_RECEIVE_SMS);
    }


    private void requestPermissions(String permission, int requestCode) {
        // Here, thisActivity is the current activity
        if (ContextCompat.checkSelfPermission(this, permission)
                != PackageManager.PERMISSION_GRANTED) {

            // Should we show an explanation?
            if (ActivityCompat.shouldShowRequestPermissionRationale(this, permission)) {

                // Show an explanation 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.
                Toast.makeText(this, "Granting permission is necessary!", Toast.LENGTH_LONG).show();

            } else {

                // No explanation needed, we can request the permission.

                ActivityCompat.requestPermissions(this,
                        new String[]{permission},
                        requestCode);

                // requestCode is an
                // app-defined int constant. The callback method gets the
                // result of the request.
            }
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode,
                                       String permissions[], int[] grantResults) {
        switch (requestCode) {
            case PERMISSIONS_REQUEST_RECEIVE_SMS: {
                // If request is cancelled, the result arrays are empty.
                if (grantResults.length > 0
                        && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

                    // permission was granted, yay! Do the
                    // contacts-related task you need to do.

                    NotificationUtil.getInstance().show(this, NotificationUtil.CONTENT_TYPE.INFO,
                            getResources().getString(R.string.app_name),
                            "Permission granted!");

                } else {

                    // permission denied, boo! Disable the
                    // functionality that depends on this permission.

                    NotificationUtil.getInstance().show(this, NotificationUtil.CONTENT_TYPE.ERROR,
                            getResources().getString(R.string.app_name),
                            "Permission denied! App will not function correctly");
                }
                return;
            }

            // other 'case' lines to check for other
            // permissions this app might request
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

希望这对您有所帮助。


Mil*_*lan 0

您的许可应放置在应用程序标签之外和之前:

<uses-permission android:name="android.permission.RECEIVE_SMS" />
Run Code Online (Sandbox Code Playgroud)