如何将我的应用设置为默认的短信应用? [英] How do I set my app as the default SMS app?

查看:324
本文介绍了如何将我的应用设置为默认的短信应用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在关注此关于将我的应用程序设置为默认SMS应用程序的教程,但由于某些原因,我的应用程序未出现在可用选项列表中.我已经尝试过对此进行尽可能多的研究,但是所有内容都指向同一教程,或者已经过时了.我还需要<receiver>吗?有人可以解释我在做什么错吗?

I am following this tutorial on setting my app as the default SMS app, but for some reason, my app does not appear in the list of available options. I have tried to research this as much as possible, but everything points back to that same tutorial, or is outdated. Do I need a <receiver> as well? Can someone explain what I am doing wrong?

代码:

@Override
protected 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);
                }
            });
        }
    }
}

清单:

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

推荐答案

为使您的应用有资格被选择为默认消息传递应用(就系统而言),您必须列出所有组件在该博客文章中显示的清单中,这些组件的类是否确实存在.

In order for your app to be eligible to be selected as the default messaging app (as far as the system is concerned), you must list all of the components in the manifest as shown in that blog post, whether those components' classes actually exist or not.

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

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

Since the system only inspects an app's manifest to determine if it can act as the default messaging app, not all of those components' classes must exist for your app to show in the default selection list. This is useful for learning and testing, but, obviously, if your app is to act as a user's default messaging client, it should fully implement all of the specified components.

如果您打算执行任何与SMS/MMS相关的任务,则还需要相关权限.尽管系统在确定合格的默认应用程序候选者时显然不会检查这些内容,但是以下权限对于它们的相应操作是必需的:

If you do intend to perform any SMS/MMS-related tasks, you will also need the relevant permissions. Though the system apparently doesn't check for these when determining eligible default app candidates, the following permissions are necessary for their corresponding operations:

<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_SMSREAD_SMSWRITE_SMS权限,则会抛出SecurityException.但是,如果您缺少RECEIVE_*权限,则只会向您的应用发送相关的广播消息,并且在测试这些功能时似乎没有任何反应.

If you're missing the SEND_SMS, READ_SMS, or WRITE_SMS permission when you attempt to perform the given action, a SecurityException will be thrown. However, if you're missing a RECEIVE_* permission, your app will just not be delivered the pertinent broadcasts, and it might seem as though nothing is happening when testing those functionalities.

这篇关于如何将我的应用设置为默认的短信应用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆