android 4.4 设置默认短信 和来电短信拒接

ACTION_RESPONSE_VIA_MESSAGE 这个广播很重要。


怎么开发default SMS app


    现存的短信类App不会默认升级为default SMS app,需要完成Android新的规范协议。Android 4.4中,系统收到短信时,只有一个应用能收到SMS_DELIVER_ACTION,这个应用就是default SMS app,WAP_PUSH_DELIVER_ACTION也是类似。如果现存的短信类App不做改造,运行在Android 4.4也不会Crash,但是写入短信数据库数据时会失败。




    为了使你的应用出现在系统设置的Default SMS app中,你需要在manifest 文件声明一下几种能力。




1、接收SMS_DELIVER_ACTION("android.provider.Telephony.SMS_DELIVER")的broadcast receiver,这个broadcast receiver需要有BROADCAST_SMS权限。

这些是为了让你的应用能接收到SMS messages。




2、接收WAP_PUSH_DELIVER_ACTION("android.provider.Telephony.WAP_PUSH_DELIVER") 的broadcast receiver,这个需要BROADCAST_WAP_PUSH权限。

这些是为了让你的应用能接收到MMS  messages。




3、实现发送短信功能,需要有个Activity完成ACTION_SENDTO("android.intent.action.SENDTO")intent filter,并使用schemas, sms:, smsto:, mms:, 以及 mmsto:。

这可以使其他应用调用你的发短信能力。




4、实现一个提供intent filter for ACTION_RESPONSE_VIA_MESSAGE("android.intent.action.RESPOND_VIA_MESSAGE") with schemas, sms:, smsto:, mms:, and mmsto服务。这个服务需要 SEND_RESPOND_VIA_MESSAGE权限。

这允许用户使用您的应用程序提供即时短信回应电话呼入。





下面是一个manifest文件的例子:


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



    Android 4.4可以使用SMS_RECEIVED_ACTION广播来观察收到了短信,这样可以知道特定的短信收到了,但是我们不能对接收到短信做处理。








设置自己的app为default SMS app



    Android4.4中提供了新的方法 Telephony.Sms.getDefaultSmsPackage(),可以获取到当前Default SMS app的包名。用户打开你的应用时可以通过判断知道自己的应用是否为Default SMS app。如果不是,可以通过startActivity() 方法启动类似如下的Dialog。具体实现可参考下面的代码。





public class ComposeSmsActivity extends Activity {

    @Override
    protected void onResume() {
        super.onResume();

        final String myPackageName = getPackageName();
        if (!Telephony.Sms.getDefaultSmsPackage(this).equals(myPackageName)) {
            // App is not default.
            // Show the "not currently set as the default SMS app" interface
            View viewGroup = findViewById(R.id.not_default_app);
            viewGroup.setVisibility(View.VISIBLE);

            // Set up a button that allows the user to change the default SMS app
            Button button = (Button) findViewById(R.id.change_default_app);
            button.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    Intent intent =
                            new Intent(Telephony.Sms.Intents.ACTION_CHANGE_DEFAULT);
                    intent.putExtra(Telephony.Sms.Intents.EXTRA_PACKAGE_NAME, 
                            myPackageName);
                    startActivity(intent);
                }
            });
        } else {
            // App is the default.
            // Hide the "not currently set as the default SMS app" interface
            View viewGroup = findViewById(R.id.not_default_app);
            viewGroup.setVisibility(View.GONE);
        }
    }
}






对短信备份恢复类App的一点建议



    短信备份恢复类应用没有Default SMS app的能力,不能写入短信数据库数据,就起不到恢复短信的作用了。Android开发者博客给出的建议是临时的设置自己的应用为Default SMS app,临时获取一次写入短信数据库数据能力,等短信恢复完成再改回原来的应用为Default SMS app。




1、获取默认App的包名并保存。


String defaultSmsApp = Telephony.Sms.getDefaultSmsPackage(context);
2、让用户修改你的app为Default SMS app


Intent intent = new Intent(context, Sms.Intents.ACTION_CHANGE_DEFAULT);
intent.putExtra(Sms.Intents.EXTRA_PACKAGE_NAME, context.getPackageName());
startActivity(intent);
3、恢复完短信,再让用户修改回Default SMS app,使用第一步保存的包名。


Intent intent = new Intent(context, Sms.Intents.ACTION_CHANGE_DEFAULT);
intent.putExtra(Sms.Intents.EXTRA_PACKAGE_NAME, defaultSmsApp);
startActivity(intent);



    上面是一些Android4.4短信变化的介绍,大部分是翻译自Android开发者博客,由于作者英语水平有限,可能与原作者的理解有些出入,敬请读者谅解。
  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值