简体   繁体   English

Android - 如何以编程方式使我的应用程序默认短信应用程序

[英]Android - how to make my app default sms app programatically

I am developing an sms blocking app.我正在开发一个短信拦截应用程序。 Which is working fine up to Jelly-Bean.这对 Jelly-Bean 来说效果很好。 And its not working from Kitkat to Marshmallow.从奇巧到棉花糖,它不起作用。 I searched on google and everyone recommending to take permission from user to make it default sms app of this phone by using .我在谷歌上搜索,每个人都建议征得用户的许可,通过使用 .

   if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
                if(!Telephony.Sms.getDefaultSmsPackage(getApplicationContext()).equals(getApplicationContext().getPackageName())) {
                    Intent intent = new Intent(Telephony.Sms.Intents.ACTION_CHANGE_DEFAULT);
                    intent.putExtra(Telephony.Sms.Intents.EXTRA_PACKAGE_NAME,
                            getApplicationContext().getPackageName());
                    startActivity(intent);
                }
            }

Manifest file :清单文件:

<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.WRITE_CONTACTS" />
<uses-permission android:name="android.permission.READ_CALL_LOG" />
<uses-permission android:name="android.permission.WRITE_CALL_LOG" />
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.SEND_SMS" />
<uses-permission android:name="android.permission.READ_SMS" />
<uses-permission android:name="android.permission.WRITE_SMS" />
<uses-permission android:name="android.permission.CALL_PHONE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.VIBRATE" />

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme.NoActionBar">
    <activity android:name=".RegisterActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:name=".DashboardActivity">
        <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>
    <activity android:name=".LoginActivity" />
    <activity android:name=".ContactListActivity" />
    <activity android:name=".CallLogListActivity" />
    <activity android:name=".SMSContactListActivity" />
    <activity android:name=".SettingsActivity" />

    <receiver
        android:name=".PhoneStateReceiver"
        android:enabled="true"
        android:permission="android.permission.BROADCAST_SMS">
        <intent-filter android:priority="9999">
            <action android:name="android.intent.action.PHONE_STATE" />
            <action android:name="android.intent.action.NEW_OUTGOING_CALL" />
            <action android:name="android.provider.Telephony.SMS_RECEIVED" />
            <action android:name="android.provider.Telephony.SMS_DELIVER" />
            <action android:name="android.intent.action.RESPOND_VIA_MESSAGE" />
        </intent-filter>
    </receiver>

    <!-- Service that delivers messages from the phone "quick response" -->
    <service
        android:name=".HeadlessSmsSendService"
        android:enabled="true"
        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>

    <activity android:name=".BlockedCallLogActivity" />
    <activity android:name=".BlockedSmsLogActivity" />
    <activity android:name=".AboutUsActivity" />
</application>

I used this code sample.我使用了这个代码示例。 It shows a dialog with messaging app of phone.它显示了一个带有手机消息应用程序的对话框。 My app is not showing on this list.我的应用程序未显示在此列表中。

Now i want to show my app on this list also, so user can select my app from this list and it will be default sms app of that phone.现在我也想在此列表中显示我的应用程序,以便用户可以从此列表中选择我的应用程序,它将成为该手机的默认短信应用程序。 How can i do that.我怎么能这样做。 Please reply with example/code-sample.请回复示例/代码示例。 Thanks.谢谢。

I followed it我跟着它

Not according to the code in your question.不是根据您问题中的代码。 Let's review the four requirements from the blog post :让我们回顾一下博客文章中的四个要求:

In a broadcast receiver, include an intent filter for SMS_DELIVER_ACTION ("android.provider.Telephony.SMS_DELIVER").在广播接收器中,为 SMS_DELIVER_ACTION(“android.provider.Telephony.SMS_DELIVER”)包含一个意图过滤器。 The broadcast receiver must also require the BROADCAST_SMS permission.广播接收器还必须需要 BROADCAST_SMS 权限。

You have this, in the form of your PhoneStateReceiver .你有这个,以你的PhoneStateReceiver的形式。

In a broadcast receiver, include an intent filter for WAP_PUSH_DELIVER_ACTION ("android.provider.Telephony.WAP_PUSH_DELIVER") with the MIME type "application/vnd.wap.mms-message".在广播接收器中,为 WAP_PUSH_DELIVER_ACTION(“android.provider.Telephony.WAP_PUSH_DELIVER”)包含一个具有 MIME 类型“application/vnd.wap.mms-message”的意图过滤器。 The broadcast receiver must also require the BROADCAST_WAP_PUSH permission.广播接收器还必须需要 BROADCAST_WAP_PUSH 权限。

You do not have this.你没有这个。

In your activity that delivers new messages, include an intent filter for ACTION_SENDTO ("android.intent.action.SENDTO") with schemas, sms:, smsto:, mms:, and mmsto:.在您传递新消息的 Activity 中,为 ACTION_SENDTO(“android.intent.action.SENDTO”)包含一个带有架构、sms:、smsto:、mms: 和 mmsto: 的意图过滤器。

You have this, in the form of DashboardActivity .你有这个,以DashboardActivity的形式。

In a service, include an intent filter for ACTION_RESPONSE_VIA_MESSAGE ("android.intent.action.RESPOND_VIA_MESSAGE") with schemas, sms:, smsto:, mms:, and mmsto:.在服务中,包含 ACTION_RESPONSE_VIA_MESSAGE ("android.intent.action.RESPOND_VIA_MESSAGE") 的意图过滤器,其中包含架构、sms:、smsto:、mms: 和 mmsto:。 This service must also require the SEND_RESPOND_VIA_MESSAGE permission.此服务还必须需要 SEND_RESPOND_VIA_MESSAGE 权限。

You have this, in the form of HeadlessSmsSendService .你有这个,以HeadlessSmsSendService的形式。

So, add a <receiver> for WAP_PUSH_DELIVER_ACTION , following the instructions, and see if that helps.因此,按照说明为WAP_PUSH_DELIVER_ACTION添加一个<receiver> ,看看是否有帮助。

Use the below code in manifest file.在清单文件中使用以下代码。 It should work perfectly.它应该可以完美运行。

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

    <activity android:name=".main.MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:name=".conversation.ConversationsActivity" />
    <!-- Activity that allows the user to send new SMS/MMS messages -->
    <activity
        android:name=".sendsms.SendSmsActivity"
        android:windowSoftInputMode="adjustPan">
        <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>
    <!-- BroadcastReceiver that listens for incoming SMS messages -->
    <receiver
        android:name=".receivesms.SmsReceiver"
        android:enabled="true"
        android:exported="true"
        android:permission="android.permission.BROADCAST_SMS">
        <intent-filter>
            <action android:name="android.provider.Telephony.SMS_RECEIVED" />
            <action android:name="android.provider.Telephony.SMS_DELIVER" />
        </intent-filter>
    </receiver>
    <!-- BroadcastReceiver that listens for incoming MMS messages -->
    <receiver
        android:name=".receivesms.MmsReceiver"
        android:enabled="true"
        android:exported="true"
        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 that delivers messages from the phone "quick response" -->
    <service
        android:name=".service.SmsSendService"
        android:enabled="true"
        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>
</application>

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

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