遇到个问题,求回答

Android 码拜 8年前 (2016-05-12) 1114次浏览
package cn.duanxin;
import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteException;
import android.net.Uri;
import android.util.Log;
import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
/**
* Created by Administrator on 2016/7/27.
*/
public class SmsContent {
private Context context;
private Uri SMS_IMBOX =Uri.parse(“content://sms/”);
private List<SMSbean> list;
public List<SMSbean> getSmsInPhone(Context context) {
list =new ArrayList<SMSbean>();

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”;
final String SMS_URI_OUTBOX = “content://sms/outbox”;
final String SMS_URI_FAILED = “content://sms/failed”;
final String SMS_URI_QUEUED = “content://sms/queued”;

StringBuilder smsBuilder = new StringBuilder();

try {
Uri uri = Uri.parse(SMS_URI_ALL);
String[] projection = new String[] { “_id”, “address”, “person”, “body”, “date”, “type” };
Cursor cur = context.getContentResolver().query(uri, projection, null, null, “date desc”);      // 获取手机内部短信

if (cur.moveToFirst()) {
int index_Address = cur.getColumnIndex(“address”);
int index_Person = cur.getColumnIndex(“person”);
int index_Body = cur.getColumnIndex(“body”);
int index_Date = cur.getColumnIndex(“date”);
int index_Type = cur.getColumnIndex(“type”);

do {
String strAddress = cur.getString(index_Address);
int intPerson = cur.getInt(index_Person);
String strbody = cur.getString(index_Body);
long longDate = cur.getLong(index_Date);
int intType = cur.getInt(index_Type);

SimpleDateFormat dateFormat = new SimpleDateFormat(“yyyy-MM-dd hh:mm:ss”);
Date d = new Date(longDate);
String strDate = dateFormat.format(d);

String strType = “”;
if (intType == 1) {
strType = “接收”;
} else if (intType == 2) {
strType = “发送”;
} else {
strType = “null”;
}
SMSbean sms =new SMSbean(strAddress,strbody);
list.add(sms);

} while (cur.moveToNext());

if (!cur.isClosed()) {
cur.close();
cur = null;
}
} else {
smsBuilder.append(“no result!”);
} // end if

smsBuilder.append(“getSmsInPhone has executed!”);

} catch (SQLiteException ex) {
Log.d(“SQLiteException in getSmsInPhone”, ex.getMessage());
}

return list;
}
}
本人这获取短信方法对吗,不想在activity写。好像空指针了,求高手。怎么改
遇到个问题,求回答

解决方案

5

。首先要先写个构造函数吧context传过去吧,你直接声明一个context 并没有赋值

5

这…同楼上所说

public class SmsContent {
	private Context context;
	public SmsContent(Context context) {
		super();
		this.context = context;
	}

5

smscontent 实例化了吗?

5

同楼上所问遇到个问题,求回答

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明遇到个问题,求回答
喜欢 (0)
[1034331897@qq.com]
分享 (0)