在KitKat中获取短信访问权限

时间:2014-06-09 22:03:32

标签: android sms android-4.4-kitkat

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

首先,我希望像这样的对话让我写一下:

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

1 个答案:

答案 0 :(得分:3)

应用程序成为SMS应用程序的要求是

   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.

所以你需要你的清单,如下所示,所有上述字段都是强制性的。只有这样你的意图意图=新意图(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>