我正在关注this tutorial将我的应用程序设置为默认短信应用程序,但由于某种原因,我的应用程序没有出现在可用选项列表中。我已尝试尽可能多地研究这一点,但所有内容都指向同一个教程,或者已经过时了。我需要 <receiver>还有?有人可以解释我做错了什么吗?

代码:

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

list :

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

请您参考如下方法:

为了使您的应用有资格被选为默认消息应用(就系统而言),其 list 必须列出该博客文章中描述的四个组件中的每一个,无论这些组件的类是否实际存在并起作用。类名可以是您喜欢的任何有效名称,但每个组件的其余部分应该与所示的几乎完全相同:

<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: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> 
</manifest> 

由于系统仅检查应用程序的 list 以确定它是否可以充当默认消息传递应用程序,因此您实际上不需要任何这些类,尽管您可能必须抑制一些警告/错误,或提供 stub 类,以使您的 IDE 满意。

显然,如果您的应用程序要充当用户的默认消息传递客户端,则它应该完全实现所有指定的组件。然而,不完整的实现肯定是有用的;例如,在学习和测试期间,或在仅需要临时部分访问的应用程序中,例如消息备份和恢复应用程序。

如果您确实打算执行任何与短信/彩信相关的任务,您还需要相关权限。尽管系统在确定合格的默认应用候选时显然不会检查这些,但其相应操作需要以下权限:

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

如果您在发生给定操作时缺少相关权限,则会抛出SecurityException,尽管有些可能很容易错过;例如,当系统尝试将传入 SMS 传送到 list 注册的接收器时,如果缺少 RECEIVE_SMS 权限。如果您观察到意外行为,即使没有明显的崩溃,也请务必检查日志。


评论关闭
IT源码网

微信公众号号:IT虾米 (左侧二维码扫一扫)欢迎添加!