Android - 如何以编程方式使我的应用程序默认短信应用程序 [英] Android - how to make my app default sms app programatically
问题描述
我正在开发一个短信阻止应用。哪个可以正常使用Jelly-Bean。它不是从Kitkat到Marshmallow。我搜索了谷歌,每个人都建议取得用户的许可,使用这个手机的默认短信应用程序。
I am developing an sms blocking app. Which is working fine up to 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);
}
}
清单文件:
<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.
现在我想在此列表中显示我的应用程序,因此用户可以从此列表中选择我的应用程序,它将是该手机的默认短信应用程序。我怎样才能做到这一点。请回复example / code-sample。谢谢。
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:
在广播接收器中,包含SMS_DELIVER_ACTION的意图过滤器(android.provider.Telephony.SMS_DELIVER )。广播接收器还必须要求BROADCAST_SMS权限。
In a broadcast receiver, include an intent filter for SMS_DELIVER_ACTION ("android.provider.Telephony.SMS_DELIVER"). The broadcast receiver must also require the BROADCAST_SMS permission.
你有 PhoneStateReceiver
的形式。
在广播接收器中,包含一个MIME类型为application / vnd的WAP_PUSH_DELIVER_ACTION(android.provider.Telephony.WAP_PUSH_DELIVER)的意图过滤器。 wap.mms消息。广播接收器还必须要求BROADCAST_WAP_PUSH权限。
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". The broadcast receiver must also require the BROADCAST_WAP_PUSH permission.
你没有这个。
在提供新消息的活动中,包括ACTION_SENDTO(android.intent.action.SENDTO)的intent过滤器,其中包含schemas,sms:,smsto:,mms:和mmsto:。
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:.
你有这个,以 DashboardActivity
的形式。
在服务中,包含一个针对ACTION_RESPONSE_VIA_MESSAGE(android.intent.action.RESPOND_VIA_MESSAGE)的intent过滤器,其中包含schemas,sms:,smsto:,mms: ,和mmsto:。此服务还必须具有SEND_RESPOND_VIA_MESSAGE权限。
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:. This service must also require the SEND_RESPOND_VIA_MESSAGE permission.
你有这个,以 HeadlessSmsSendService
的形式。
因此,为 WAP_PUSH_DELIVER_ACTION
添加< receiver>
,说明,看看是否有帮助。
So, add a <receiver>
for WAP_PUSH_DELIVER_ACTION
, following the instructions, and see if that helps.
这篇关于Android - 如何以编程方式使我的应用程序默认短信应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!