为了账号安全,请及时绑定邮箱和手机立即绑定

如何将我的应用程序设置为默认的SMS应用程序?

如何将我的应用程序设置为默认的SMS应用程序?

四季花海 2019-09-03 17:18:37
我正在按照本教程将我的应用程序设置为默认的SMS应用程序,但出于某种原因,我的应用程序没有出现在可用选项列表中。我试图尽可能地研究这个,但是一切都指向同一个教程,或者已经过时了。我也需要一个<receiver>吗?谁能解释我做错了什么?代码:@Overrideprotected void onResume(){    super.onResume();    Log.i("MainAcitvity", "On Resume Called");    // Only do these checks/changes on KitKat+, the "mSetDefaultSmsLayout" has its visibility    // set to "gone" in the xml layout so it won't show at all on earlier Android versions.    final String myPackageName = getPackageName();    if (Utility.hasKitKat())    {        if (Utility.isDefaultSmsApp(this))        {            // This app is the default, remove the "make this app the default" layout and            // enable message sending components.            mSetDefaultSmsLayout.setVisibility(View.GONE);        }        else        {            Log.i("MainActivity", "Not Default App");            // Not the default, show the "make this app the default" layout and disable            // message sending components.            mSetDefaultSmsLayout.setVisibility(View.VISIBLE);            Button button = (Button) findViewById(R.id.set_default_sms_button);            button.setOnClickListener(new OnClickListener()            {                @Override                public void onClick(View view)                {                                            Log.i("MainActivity", "Button Pushed");                    //Utility.setDefaultSmsApp(MainActivity.this);                    Intent intent = new Intent(Telephony.Sms.Intents.ACTION_CHANGE_DEFAULT);                    intent.putExtra(Telephony.Sms.Intents.EXTRA_PACKAGE_NAME, myPackageName);                    startActivity(intent);                }            });        }    }
查看完整描述

2 回答

?
米琪卡哇伊

TA贡献1998条经验 获得超6个赞

为了使您的应用程序有资格被选为默认消息传递应用程序(就系统而言),您必须列出清单中显示的所有组件,如该博文中所示,这些组件的类是否实际存在或不。


<manifest>

    ...

    <application>

        <!-- BroadcastReceiver that listens for incoming SMS messages -->

        <receiver android:name=".SmsReceiver"

            android:permission="android.permission.BROADCAST_SMS">

            <intent-filter>

                <action android:name="android.provider.Telephony.SMS_DELIVER" />

            </intent-filter>

        </receiver>


        <!-- BroadcastReceiver that listens for incoming MMS messages -->

        <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>


        <!-- Activity that allows the user to send new SMS/MMS messages -->

        <activity android:name=".ComposeSmsActivity" >

            <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>


        <!-- Service that delivers messages from the phone "quick response" -->

        <service android:name=".HeadlessSmsSendService"

            android:permission="android.permission.SEND_RESPOND_VIA_MESSAGE"

            android:exported="true" >

            <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>

</manifest>

由于系统仅检查应用程序的清单以确定它是否可以充当默认消息传递应用程序,因此并非所有这些组件的类都必须存在才能使您的应用程序显示在默认选择列表中。这对于学习和测试很有用,但是,显然,如果您的应用程序要充当用户的默认消息传递客户端,它应该完全实现所有指定的组件。


如果您打算执行任何与SMS / MMS相关的任务,您还需要相关权限。虽然系统在确定符合条件的默认应用程序候选者时显然不会检查这些,但以下权限对于其相应的操作是必需的:


<uses-permission android:name="android.permission.SEND_SMS" />

<uses-permission android:name="android.permission.RECEIVE_SMS" />

<uses-permission android:name="android.permission.READ_SMS" />

<uses-permission android:name="android.permission.WRITE_SMS" />

<uses-permission android:name="android.permission.RECEIVE_MMS" />

如果您在尝试执行给定操作时错过了SEND_SMS,READ_SMS或WRITE_SMS权限,SecurityException则会抛出一个。但是,如果您缺少RECEIVE_*权限,您的应用程序将无法提供相关的广播,并且在测试这些功能时似乎没有任何事情发生。


查看完整回答
反对 回复 2019-09-03
?
白猪掌柜的

TA贡献1893条经验 获得超10个赞

具有<service>元素的任何组件都是Service子类。

查看完整回答
反对 回复 2019-09-03

添加回答

代码语言

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信