上篇文章讲到《Android 短信的备份》,本文主要实现Android 短信的还原,即是将一条
布局文件:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity" > <Button android:onClick="restoreSms" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="还原一条短信" /> </RelativeLayout>
添加代码:
package com.wuyudong.smsrestore; import android.net.Uri; import android.os.Bundle; import android.app.Activity; import android.content.ContentResolver; import android.content.ContentValues; import android.view.Menu; import android.view.View; import android.widget.Toast; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public void restoreSms(View view) { Uri uri = Uri.parse("content://sms/"); ContentResolver resolver = getContentResolver(); ContentValues values = new ContentValues(); values.put("address", "110"); values.put("body", "你将被公安局监控,请速去报到"); values.put("date", System.currentTimeMillis()+""); values.put("type", "1"); //接受短信 resolver.insert(uri, values); Toast.makeText(this, "添加成功了", 0).show(); } }
添加权限:
<uses-permission android:name="android.permission.READ_SMS"/> <uses-permission android:name="android.permission.WRITE_SMS"/>
运行项目,搞定