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

时间:2015-09-12 09:15:14

标签: java android sms 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"/>

6 个答案:

答案 0 :(得分: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>

然后,从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
        }
    }
}

希望这对你有所帮助。

答案 1 :(得分:2)

@Richard Green 您的Logcat抛出

  

权限拒绝:接收意图{   act = android.provider.Telephony.SMS_RECEIVED flg = 0x8000010(有额外内容)   to com.example.richard.simplesmstoast / .SmsReceiver需要   android.permission.RECEIVE_SMS由于发件人com.android.phone

权限是限制访问部分代码或设备上数据的限制。限制是为了保护可能被滥用以破坏或损害用户体验的关键数据和代码。

我认为它的权限问题。

请在下面添加  Manifest -Permissions 应用 标记上方。

   <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" />

我希望它会对你有所帮助。

答案 2 :(得分:0)

  • 如果您的目标是Marshmallow,那么除了清单之外,您还必须请求运行时权限。

清单 -

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

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 !!!");
        }
    }
}

答案 3 :(得分:-1)

这对我有用...... 看看这个可能它会帮助你

<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>

答案 4 :(得分:-1)

在应用程序标记内添加权限,如..

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

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

    <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>

答案 5 :(得分:-1)

{{1}}

WRTIE_SMS权限错误