android - 在 KitKat 中获取短信访问权限

标签 android sms android-4.4-kitkat

我编写了一个写入短信存储的短信应用程序。这一直有效,直到 Andorid 4.4 KitKat。然后我读到总是有 1 个主要的短信应用程序。 现在我想在我做我的事情之前询问用户是否想使用我的应用程序作为主应用程序,并在我完成写入存储后要求更改回来。

所以首先我想要一个这样的对话框来让我写:

enter image description here

然后我想将其改回用户之前的状态。

我现在的代码不起作用:

String defaultSmsApp = null;

    if (android.os.Build.VERSION.SDK_INT >= 19) {
        defaultSmsApp = Telephony.Sms.getDefaultSmsPackage(myContext);

        final String myPackageName = getPackageName();
        Log.e("SMS Faker", "myPackageName=" + myPackageName);
        Log.e("SMS Faker", "defaultSmsApp=" + defaultSmsApp);
        if (!defaultSmsApp.equals(myPackageName)) {
            Intent intent = new Intent(Sms.Intents.ACTION_CHANGE_DEFAULT);
            intent.putExtra(Sms.Intents.EXTRA_PACKAGE_NAME, myContext.getPackageName());
            startActivity(intent);
        }
    }

最佳答案

应用程序成为短信应用程序的要求是

   Implement SMS_DELIVER_ACTION broadcast receiver with BROADCAST_SMS permission.

   Implement WAP_PUSH_DELIVER_ACTION broadcast receiver with BROADCAST_WAP_PUSH permission.

   Implement RESPOND_VIA_MESSAGE intent and should support smsto Uri scheme and require SEND_RESPOND_VIA_MESSAGE permission.

   Implement ACTION_SENDTO intent support smsto Uri scheme.

因此,您的 list 需要如下所示,并且上述所有字段均为必填字段。只有这样你的Intent Intent = new Intent(Sms.Intents.ACTION_CHANGE_DEFAULT);才会起作用

  <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>
        <intent-filter>
            <action android:name="android.intent.action.SEND" />
            <action android:name="android.intent.action.SENDTO" />

            <category android:name="android.intent.category.DEFAULT" />
            <category android:name="android.intent.category.BROWSABLE" />

            <data android:scheme="sms" />
            <data android:scheme="smsto" />
            <data android:scheme="mms" />
            <data android:scheme="mmsto" />
        </intent-filter>
    </activity>

    <receiver
        android:name="SmsReciever"
        android:permission="android.permission.BROADCAST_SMS" >
        <intent-filter>
            <action android:name="android.provider.Telephony.SMS_DELIVER" />
        </intent-filter>
    </receiver>
    <receiver
        android:name="MmsReceiver"
        android:permission="android.permission.BROADCAST_WAP_PUSH" >
        <intent-filter>
            <action android:name="android.provider.Telephony.WAP_PUSH_DELIVER" />

            <data android:mimeType="application/vnd.wap.mms-message" />
        </intent-filter>
    </receiver>

    <service
        android:name="RespondService"
        android:exported="true"
        android:permission="android.permission.SEND_RESPOND_VIA_MESSAGE" >
        <intent-filter>
            <action android:name="android.intent.action.RESPOND_VIA_MESSAGE" />

            <category android:name="android.intent.category.DEFAULT" />

            <data android:scheme="sms" />
            <data android:scheme="smsto" />
            <data android:scheme="mms" />
            <data android:scheme="mmsto" />
        </intent-filter>
    </service>

关于android - 在 KitKat 中获取短信访问权限,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24129802/

相关文章:

java - 如何使用推送注册表读取 j2me midlet 中收到的消息?

iphone - 如何以编程方式在 iPhone 中获取特定格式短信的正文?

android - 网页 View 缩放android 4.4

Android KitKat 模拟器 native 库 JNI 错误(应用程序错误)

android - 洛蒂动画不起作用

android - 为什么来自 Google map 的 Geocoder 返回日文字符?

android - 在 mapView 上添加图层或背景

android - 程序类型已经存在 : android. support.v4.app.FrameMetricsAggregator$FrameMetricsApi24Impl$1

android - Android 4.4 KitKat 上的 shouldInterceptRequest

android - 应用程序终止或设备仅在 MI 设备上重启后,短信 BroadcastReceiver 不接收短信