短信备份与还原

最近在做一款手机安全卫士的App,其中有一个功能:短信备份与还原。本来感觉是一个很简单的功能,不过实现起来,遇到了很多的问题,所以就拿来分享一下。

短信备份的功能很好实现,浏览器随便搜一下就能找到答案,这里就不在多说。主要是短信还原的问题。

对于安卓4.4以前,很简单的一段代码就能实现,

                            ContentResolver resolver = context.getContentResolver();
                            Uri uri = Uri.parse("content://sms");
                            ContentValues values = new ContentValues();
                            values.put("address", "1231445");
                            values.put("body", "heheheheh");
// 插入数据
                            resolver.insert(uri, values);
但是,在4.4以后,这种简单的实现就没用了。

因为在4.4以后,只有系统默认的信息应用程序才有权对短信的数据库进行写入操作。

所以要想实现短信写入的操作,就必须手动设置自己的app成为默认的短信应用程序。

但是,你的app要成为默认短息应用,首先得符合条件,然后才可以申请成为默认的短信应用程序。

条件1:

You must Have an Activity including an intent filter with an ACTION SENDTO ("android.intent.action.SENDTO" ) and with schemas sms, smsto, mms, and mmsto.


翻译:你必须有一个活动,包括一个意图过滤器,过滤器的信息包含:

<action android:name="android.intent.action.SENDTO" />

<data android.scheme="sms"/"smsto"/"mms"/"mmsto" />

条件2:

Creating an empty Service including an intent filter with ACTION RESPOND VIA MESSAGE ("android.intent.action.RESPOND VIA MESSAGE") and with schemas, sms, smsto, mms, and mmsto.


翻译:创建一个空的服务包括一个意图过滤器,过滤器的信息包含:

<action android:name="android.intent.action.SENDTO" />

<data android.scheme="sms"/"smsto"/"mms"/"mmsto" />

需要权限:

android:permission="android.permission.SEND_RESPOND_VIA_MESSAG"


条件3:

Create an empty BroadcastReceiver including an intent filter with WAP PUSH DELIVER ACTION ("android.provider.Telephony.WAP PUSH DELIVER") with the MIME type application/vnd.wap.mms-message.


翻译:创建一个空BroadcastReceiver,意图过滤器信息:


<action android:name="android.intent.action.Telephony.WAP PUSH DELIVER" />

<data android:mimeType="application/vnd.wap.mms-message"/>


需要权限:

android:permission="android.permission.BROADCAST_WAP_PUSH"


条件4:

Create an empty BroadcastReceiver including an intent filter with SMS DELIVER ACTION ("android.provider.Telephony.SMS DELIVER").

翻译

创建一个空BroadcastReceiver包括一个意图过滤器:

<action android:name= "android.intent.action.Telephony.SMS DELIVER" />

需要权限:

android:permission="android.permission.BROADCAST_SMS"


所以结合这几点,实现插入(还原)短信的功能步骤:

在AndroidManiFest.xml清单文件中添加以下信息:


<Application...>

.....

<activity
    android:name=".ComposeSmsActivity"
    android:label="@string/title_activity_compose_sms" >
    <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>
<receiver android:name=".SmsReceivers"
    android:permission="android.permission.BROADCAST_SMS">
    <intent-filter>
        <action android:name="android.provider.Telephony.SMS_DELIVER" />
    </intent-filter>
</receiver>
<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>
<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>


所创建的这几个Service,Activity,Recevier,只需要创建出来就行,不用对它们进行操作。

以上步骤做好后,即可以执行以下代码

//    获取当前默认的短信应用程序的包名

    String defaultSmsPkg=Telephony.Sms.getDefaultSmsPackage(context);
    
//    获取当前系统的包名
    String mySmsPkg= context.getPackageName();
    
//    判断默认的短信应用程序的包名与当前应用是否一致
    if(!defaultSmsPkg.equals(mySmsPkg)){
        
        Intent intent = new Intent(Telephony.Sms.Intents.ACTION_CHANGE_DEFAULT);
        
        intent.putExtra(Telephony.Sms.Intents.EXTRA_PACKAGE_NAME, mySmsPkg);
        
        startActivity(intent);//使用startActivityForResult()更好
    }

到这里就已经成功的把你的app设置为默认的短信应用程序,接下来就可以进行短信的插入操作了!!!

当然还需要权限

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

ContentResolver resolver = context.getContentResolver();
                            Uri uri = Uri.parse("content://sms");
                            ContentValues values = new ContentValues();
                            values.put("address", "12414224");//发送人手机号
                            values.put("body", "heheheheh");//短信内容
// 插入数据
                            resolver.insert(uri, values);

插入成功后不要忘了取消默认短信应用程序的设置:

Intent intent = new Intent( Telephony.Sms.Intents.ACTION_CHANGE_DEFAULT);
intent.putExtra(Telephony.Sms.Intents.EXTRA_PACKAGE_NAME, defaultSmsPkg);
startActivity(intent);





  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值