android_71_获取系统短信

 

 

 

 

 

 

 

 

 

 

效果:

 

清单:

 

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.sg31.smsreader"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="21" />

    // 注意权限 
    <uses-permission android:name="android.permission.READ_SMS"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    
    
    
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <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" />
            </intent-filter>
        </activity>
    </application>

</manifest>

 

 

 

 

 

布局:

 

<LinearLayout 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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.sg31.smsreader.MainActivity"
    android:orientation="vertical" 
    >
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="获取系统短信" 
        android:onClick="smsReadBtnClicked"
        />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="备份系统短信" 
        android:onClick="smsBackupBtnClicked"
        />

</LinearLayout>


模型:

 

 

package com.sg31.smsreader;

public class SMSModel {

	private String body;
	private String type;
	private String address;
	private long date;

	public String getBody() {
		return body;
	}

	public void setBody(String body) {
		this.body = body;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public String getAddress() {
		return address;
	}

	public void setAddress(String address) {
		this.address = address;
	}

	public long getDate() {
		return date;
	}

	public void setDate(long date) {
		this.date = date;
	}

	public SMSModel(String body, String type, String address, long date) {
		super();
		this.body = body;
		this.type = type;
		this.address = address;
		this.date = date;
	}

}

 

 

 

 

 

 

控制器代码:

 

package com.sg31.smsreader;

import java.io.File;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.List;

import org.xmlpull.v1.XmlSerializer;

import android.support.v7.app.ActionBarActivity;
import android.content.ContentResolver;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.util.Xml;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;

public class MainActivity extends ActionBarActivity {

	List<SMSModel> smsList;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		smsList = new ArrayList<SMSModel>();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	public void smsReadBtnClicked(View v) {
		// 访问内容提供者获取短信
		ContentResolver cr = getContentResolver();
		// 短信内容提供者的主机名
		Cursor cursor = cr.query(Uri.parse("content://sms"), new String[] {
				"address", "date", "body", "type" }, null, null, null);
		while (cursor.moveToNext()) {
			String address = cursor.getString(0);
			long date = cursor.getLong(1);
			String body = cursor.getString(2);
			String type = cursor.getString(3);
			SMSModel sms = new SMSModel(body, type, address, date);
			smsList.add(sms);
		}
	}

	public void smsBackupBtnClicked(View v) {
		XmlSerializer xs = Xml.newSerializer();
		File file = new File("sdcard/sms.xml");
		FileOutputStream fos;
		try {
			fos = new FileOutputStream(file);
			xs.setOutput(fos, "utf-8");

			xs.startDocument("utf-8", true);
			xs.startTag(null, "message");

			for (SMSModel sms : smsList) {
				xs.startTag(null, "sms");

				xs.startTag(null, "body");
				xs.text(sms.getBody());
				xs.endTag(null, "body");

				xs.startTag(null, "date");
				xs.text(sms.getDate() + "");
				xs.endTag(null, "date");

				xs.startTag(null, "type");
				xs.text(sms.getType());
				xs.endTag(null, "type");

				xs.startTag(null, "address");
				xs.text(sms.getAddress());
				xs.endTag(null, "address");

				xs.endTag(null, "sms");
			}

			xs.endTag(null, "message");
			xs.endDocument();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值