android中获取手机短信,删除短信功能
2014-01-15 15:50
351 查看
java 代码:
package com.cs.smstest;
import java.text.SimpleDateFormat;
import java.util.Date;
import android.app.Activity;
import android.content.ContentResolver;
import android.database.Cursor;
import android.database.sqlite.SQLiteException;
import android.net.Uri;
import android.os.Bundle;
import android.text.method.ScrollingMovementMethod;
import android.util.Log;
import android.widget.TextView;
public class SMSTestActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//获取手机短信:
String strSMS = getSmsInPhone();
TextView tv = (TextView)this.findViewById(R.id.tv_view);
tv.setText(strSMS);
tv.setMovementMethod(new ScrollingMovementMethod());//设置textview可滚动
//删除手机短信
deleteSMS();
}
/**
*
* 方法功能描述:删除短信
* <p>
* <b>参数说明</b>
* <p>
* @since 1.0
* @author zlp
* @time 2014-1-15 下午3:14:16
*/
public void deleteSMS(){
try{
ContentResolver cr=getContentResolver();
Uri urisms=Uri.parse("content://sms/");
Cursor c=cr.query(urisms,new String[]{"_id", "thread_id" }, null, null, null);
if(null!=c && c.moveToFirst()){
do{
long threadid=c.getLong(1);
String ss=c.getString(0)+c.getString(1);
cr.delete(Uri.parse("content://sms/conversations/" +threadid),
null, null);
Log.d("deleteSMS", "threadId:: "+threadid);
}while(c.moveToNext());
}
}catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
/**
* 获取手机短信内容
* @return
*/
public String getSmsInPhone() {
final String SMS_URI_ALL = "content://sms/"; //所有短信
final String SMS_URI_INBOX = "content://sms/inbox"; //收信箱
final String SMS_URI_SEND = "content://sms/sent"; //发信箱
final String SMS_URI_DRAFT = "content://sms/draft"; //草稿箱
StringBuilder smsBuilder = new StringBuilder();
try {
ContentResolver cr = getContentResolver();
String[] projection = new String[] { "_id", "address", "person",
"body", "date", "type" };
Uri uri = Uri.parse(SMS_URI_ALL);
Cursor cur = cr.query(uri, projection, null, null, "date desc");
if (cur.moveToFirst()) {
String name;
String phoneNumber;
String smsbody;
String date;
String type;
int nameColumn = cur.getColumnIndex("person");//姓名
int phoneNumberColumn = cur.getColumnIndex("address");//手机号
int smsbodyColumn = cur.getColumnIndex("body");//短信内容
int dateColumn = cur.getColumnIndex("date");//日期
int typeColumn = cur.getColumnIndex("type");//收发类型 1表示接受 2表示发送
do {
name = cur.getString(nameColumn);
phoneNumber = cur.getString(phoneNumberColumn);
smsbody = cur.getString(smsbodyColumn);
SimpleDateFormat dateFormat = new SimpleDateFormat(
"yyyy-MM-dd hh:mm:ss");
Date d = new Date(Long.parseLong(cur.getString(dateColumn)));
date = dateFormat.format(d);
int typeId = cur.getInt(typeColumn);
if (typeId == 1) {
type = "接收";
} else if (typeId == 2) {
type = "发送";
} else {
type = "";
}
smsBuilder.append("[");
smsBuilder.append(name + ",");
smsBuilder.append(phoneNumber + ",");
smsBuilder.append(smsbody + ",");
smsBuilder.append(date + ",");
smsBuilder.append(type);
smsBuilder.append("] ");
if (smsbody == null)
smsbody = "";
} while (cur.moveToNext());
} else {
smsBuilder.append("没有记录!");
}
smsBuilder.append("获取彩信完成!");
} catch (SQLiteException ex) {
Log.d("SQLiteException in getSmsInPhone", ex.getMessage());
}
return smsBuilder.toString();
}
}
获取短信内容需要界面显示:
布局文件:<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:id="@+id/tv_view"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
android:scrollbars="vertical"
/>
</LinearLayout>
不要忘记添加权限:
手机短信的读写权限
<uses-permission android:name="android.permission.READ_SMS" />
<uses-permission android:name="android.permission.WRITE_SMS"/>
package com.cs.smstest;
import java.text.SimpleDateFormat;
import java.util.Date;
import android.app.Activity;
import android.content.ContentResolver;
import android.database.Cursor;
import android.database.sqlite.SQLiteException;
import android.net.Uri;
import android.os.Bundle;
import android.text.method.ScrollingMovementMethod;
import android.util.Log;
import android.widget.TextView;
public class SMSTestActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//获取手机短信:
String strSMS = getSmsInPhone();
TextView tv = (TextView)this.findViewById(R.id.tv_view);
tv.setText(strSMS);
tv.setMovementMethod(new ScrollingMovementMethod());//设置textview可滚动
//删除手机短信
deleteSMS();
}
/**
*
* 方法功能描述:删除短信
* <p>
* <b>参数说明</b>
* <p>
* @since 1.0
* @author zlp
* @time 2014-1-15 下午3:14:16
*/
public void deleteSMS(){
try{
ContentResolver cr=getContentResolver();
Uri urisms=Uri.parse("content://sms/");
Cursor c=cr.query(urisms,new String[]{"_id", "thread_id" }, null, null, null);
if(null!=c && c.moveToFirst()){
do{
long threadid=c.getLong(1);
String ss=c.getString(0)+c.getString(1);
cr.delete(Uri.parse("content://sms/conversations/" +threadid),
null, null);
Log.d("deleteSMS", "threadId:: "+threadid);
}while(c.moveToNext());
}
}catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
/**
* 获取手机短信内容
* @return
*/
public String getSmsInPhone() {
final String SMS_URI_ALL = "content://sms/"; //所有短信
final String SMS_URI_INBOX = "content://sms/inbox"; //收信箱
final String SMS_URI_SEND = "content://sms/sent"; //发信箱
final String SMS_URI_DRAFT = "content://sms/draft"; //草稿箱
StringBuilder smsBuilder = new StringBuilder();
try {
ContentResolver cr = getContentResolver();
String[] projection = new String[] { "_id", "address", "person",
"body", "date", "type" };
Uri uri = Uri.parse(SMS_URI_ALL);
Cursor cur = cr.query(uri, projection, null, null, "date desc");
if (cur.moveToFirst()) {
String name;
String phoneNumber;
String smsbody;
String date;
String type;
int nameColumn = cur.getColumnIndex("person");//姓名
int phoneNumberColumn = cur.getColumnIndex("address");//手机号
int smsbodyColumn = cur.getColumnIndex("body");//短信内容
int dateColumn = cur.getColumnIndex("date");//日期
int typeColumn = cur.getColumnIndex("type");//收发类型 1表示接受 2表示发送
do {
name = cur.getString(nameColumn);
phoneNumber = cur.getString(phoneNumberColumn);
smsbody = cur.getString(smsbodyColumn);
SimpleDateFormat dateFormat = new SimpleDateFormat(
"yyyy-MM-dd hh:mm:ss");
Date d = new Date(Long.parseLong(cur.getString(dateColumn)));
date = dateFormat.format(d);
int typeId = cur.getInt(typeColumn);
if (typeId == 1) {
type = "接收";
} else if (typeId == 2) {
type = "发送";
} else {
type = "";
}
smsBuilder.append("[");
smsBuilder.append(name + ",");
smsBuilder.append(phoneNumber + ",");
smsBuilder.append(smsbody + ",");
smsBuilder.append(date + ",");
smsBuilder.append(type);
smsBuilder.append("] ");
if (smsbody == null)
smsbody = "";
} while (cur.moveToNext());
} else {
smsBuilder.append("没有记录!");
}
smsBuilder.append("获取彩信完成!");
} catch (SQLiteException ex) {
Log.d("SQLiteException in getSmsInPhone", ex.getMessage());
}
return smsBuilder.toString();
}
}
获取短信内容需要界面显示:
布局文件:<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:id="@+id/tv_view"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
android:scrollbars="vertical"
/>
</LinearLayout>
不要忘记添加权限:
手机短信的读写权限
<uses-permission android:name="android.permission.READ_SMS" />
<uses-permission android:name="android.permission.WRITE_SMS"/>
相关文章推荐
- Android获取手机通讯录调用系统短信功能
- android 添加手机短信,获取手机短信,删除手机短信和修改手机短信
- android 获取手机中的短信信息
- android手机获取系统短信sqlite数据库并查看内容
- android手机短信获取
- Android开发之监听或获取手机短信内容
- Android开发之监听或获取手机短信内容
- Android从本地获取照片以及调用手机拍照功能的方法
- Android开发之获取手机短信验证码
- Android开发之监听或获取手机短信内容
- 获取手机短信会话信息 Android
- Android 获取app应用大小、手机内存信息、删除文件方法、获得文件大小
- 向Android手机中插入一条短信 及联系人获取
- Android开发之监听或获取手机短信内容【转】
- 发送手机短信获取验证码功能
- 获取Android手机中所有的短信
- android手机获取系统短信sqlite数据库并查看内容
- Android开发之监听或获取手机短信内容
- [android] 手机卫士手机实现短信指令获取位置
- Android如何通过手机获取验证码来完成注册功能_Android