首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >短信接收权限AndroidManifest.xml中的基本错误

短信接收权限AndroidManifest.xml中的基本错误
EN

Stack Overflow用户
提问于 2015-09-12 09:15:13
回答 7查看 9.5K关注 0票数 5

我知道这已经被问了十几次了,但是我仍然得到了这个xml配置的权限错误。我已经仔细研究了关于这个问题的其他答复。我正在使用API级别23。谁能指出这个错误吗?错误是显而易见的:

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

代码语言:javascript
运行
复制
<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"/>

EN

回答 7

Stack Overflow用户

发布于 2015-09-14 11:20:40

新权限模型 for Android (api 23)中存在问题:

Quickview

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

关于SMS案例文档,请举一个例子:

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

解决办法:

  • 正确的方式-一开始请求许可。
  • 懒惰方式-设置targetSdk 22
票数 17
EN

Stack Overflow用户

发布于 2016-02-17 16:54:42

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

代码语言:javascript
运行
复制
...
<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

代码语言:javascript
运行
复制
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
        }
    }
}

希望这能帮到你。

票数 5
EN

Stack Overflow用户

发布于 2015-09-18 05:32:08

@Richard Green您的逻辑猫抛出

权限拒绝:接收到com.example.richard.simplesmstoast/.SmsReceiver的意图{ act=android.provider.Telephony.SMS_RECEIVED flg=0x8000010 (有额外的)}需要android.permission.RECEIVE_SMS,因为发送方com.android.phone

权限是限制对代码的一部分或对设备上的数据的访问的限制。施加这一限制是为了保护可能被误用以扭曲或破坏用户体验的关键数据和代码。

我认为它的权限有问题。

请将下面的舱单-Permissions添加到应用程序标签上。

代码语言:javascript
运行
复制
   <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
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32537156

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档