简体   繁体   English

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

[英]Basic error in AndroidManifest.xml for SMS receiving permission

I know that this has been asked a dozen times before, but I still get the permission error with this xml config. 我知道之前已经问了十几次,但是我仍然得到了这个xml配置的权限错误。 I have scoured the other responses on this. 我已经仔细研究了其他的回应。 I am using API level 23. Can someone please point out the mistake ? 我正在使用API​​级别23.有人可以指出错误吗? The error is the obvious: 错误很明显:

09-12 09:13:40.016 1295-1309/? 09-12 09:13:40.016 1295-1309 /? W/BroadcastQueue﹕ Permission Denial: receiving Intent { act=android.provider.Telephony.SMS_RECEIVED flg=0x8000010 (has extras) } to com.example.richard.simplesmstoast/.SmsReceiver requires android.permission.RECEIVE_SMS due to sender com.android.phone (uid 1001) 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"/>

Problem lays in new permissions model for Android M (api 23): 问题在于Android M的新权限模型 (api 23):

Quickview 快速浏览

  • If your app targets the M Preview SDK, it prompts users to grant permissions at runtime, instead of install time. 如果您的应用面向M预览SDK,则会提示用户在运行时授予权限,而不是安装时间。
  • Users can revoke permissions at any time from the app Settings screen. 用户可以随时从“应用程序设置”屏幕撤消权限。
  • Your app needs to check that it has the permissions it needs every time it runs. 您的应用需要检查它是否具有每次运行时所需的权限。

for SMS case documentation bring an example: 为SMS案例文档带来一个例子:

For example, suppose an app lists in its manifest that it needs the SEND_SMS and RECEIVE_SMS permissions, which both belong to android.permission-group.SMS. 例如,假设应用程序在其清单中列出它需要SEND_SMS和RECEIVE_SMS权限,这两个权限都属于android.permission-group.SMS。 When the app needs to send a message, it requests the SEND_SMS permission. 当应用需要发送消息时,它会请求SEND_SMS权限。 The system shows the user a dialog box asking if the app can have access to SMS. 系统向用户显示一个对话框,询问应用程序是否可以访问SMS。 If the user agrees, the system grants the app the SEND_SMS permission it requested. 如果用户同意,系统会向应用程序授予其请求的SEND_SMS权限。 Later, the app requests RECEIVE_SMS. 稍后,该应用程序请求RECEIVE_SMS。 The system automatically grants this permission, since the user had already approved a permission in the same permission group. 系统会自动授予此权限,因为用户已经批准了同一权限组中的权限。

Solutions: 解决方案:

  • right way - request permission at first. 正确的方式 - 首先请求许可。
  • lazy way - set targetSdk 22 懒惰的方式 - 设置targetSdk 22

First, RECEIVE_SMS permission must be declared in AndroidManifest.xml. 首先,必须在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>

Then, from API level 23, we need to request RECEIVE_SMS permission at run time. 然后,从API级别23,我们需要在运行时请求RECEIVE_SMS权限。 This is important to notice. 这一点很重要。 https://developer.android.com/training/permissions/requesting.html 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
        }
    }
}

Hope this help you. 希望这对你有所帮助。

@Richard Green : Your Logcat throws @Richard Green你的Logcat引发了

Permission Denial: receiving Intent { act=android.provider.Telephony.SMS_RECEIVED flg=0x8000010 (has extras) } to com.example.richard.simplesmstoast/.SmsReceiver requires android.permission.RECEIVE_SMS due to sender com.android.phone 权限拒绝:接收Intent {act = android.provider.Telephony.SMS_RECEIVED flg = 0x8000010(有额外内容)}到com.example.richard.simplesmstoast / .SmsReceiver需要android.permission.RECEIVE_SMS,因为发件人com.android.phone

A permission is a restriction limiting access to a part of the code or to data on the device. 权限是限制访问代码的一部分或设备上的数据的限制。 The limitation is imposed to protect critical data and code that could be misused to distort or damage the user experience. 限制是为了保护可能被滥用以破坏或破坏用户体验的关键数据和代码。

I assume that its permissions Problem . 我假设它的权限问题。

Please add below Manifest -Permissions Above Application Tag . 请在下面添加Manifest -Permissions Above Application Tag。

   <uses-permission android:name="INTERNET"/>
   <uses-permission android:name="ACCESS_NETWORK_STATE"/>
   <uses-permission android:name="android.permission.WRITE_SMS" />
   <uses-permission android:name="android.permission.READ_SMS" />
   <uses-permission android:name="android.permission.RECEIVE_SMS" />

I hope it will helps you . 我希望它会对你有所帮助。

您的权限应放在申请标记之外和之前:

<uses-permission android:name="android.permission.RECEIVE_SMS" />
  • If you are targeting Marshmallow then you have to request for runtime permission apart from manifest. 如果您的目标是Marshmallow,那么除了清单之外,您还必须请求运行时权限。

Manifest - 清单 -

    <uses-permission android:name="android.permission.RECEIVE_SMS"/>
<uses-permission android:name="android.permission.READ_SMS" />

Java Activity class - Java Activity类 -

 final int REQ_CODE = 100;
void requestPermission(){
    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.RECEIVE_SMS) != PackageManager.PERMISSION_GRANTED) {
        CTLogs.printLogs( "Permission is not granted, requesting");
        ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.SEND_SMS,Manifest.permission.READ_SMS,Manifest.permission.RECEIVE_SMS}, REQ_CODE);

    } else {
        CTLogs.printLogs("Permission has been granted");
        readSMS();
    }
}

@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
    if (requestCode == REQ_CODE) {
        if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
            CTLogs.printLogs("Permission has been granted");
            readSMS();
        } else {
            CTLogs.printLogs("Permission denied !!!");
        }
    }
}

this worked for me... Check this out Maybe it will Help You 这对我有用...看看这个可能它会帮助你

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@android:style/Theme.Holo.Light.DarkActionBar" >
    <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="packageName" >
        <intent-filter >

            <action android:name="android.provider.Telephony.SMS_RECEIVED" />
        </intent-filter>

    </receiver>
</application>

//Requesting permission
private void requestWritePermission() {
    if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_SMS) == PackageManager.PERMISSION_GRANTED)
        return;

    if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.WRITE_SMS)) {
        //If the user has denied the permission previously your code will come to this block
        //Here you can explain why you need this permission
        //Explain here why you need this permission
    }
    //And finally ask for the permission
    ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_SMS}, WRITE_SMS_PERMISSION_CODE);
}

Error on WRTIE_SMS permission WRTIE_SMS权限出错

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM