我在 SMSReceiverActivity 类中收到错误“描述资源路径位置类型构造函数 SQLiteAdapter(new BroadcastReceiver(){}) 未定义”。
有错误的行是mySQLiteAdapter = new SQLiteAdapter(this);
这是完整的代码:
package com.example.smsTest;
import java.util.ArrayList;
import android.database.Cursor;
import android.widget.SimpleCursorAdapter;
import android.app.ListActivity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class SMSReceiverActivity extends ListActivity {
private SQLiteAdapter mySQLiteAdapter;
private ArrayList<String> bodyarr = new ArrayList<String>();
private ArrayAdapter<String> arrayAdpt;
private BroadcastReceiver mIntentReceiver;
ListView listview;
//ArrayAdapter<String> arrayAdpt;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_smsreceiver);
listview=this.getListView();
arrayAdpt = new ArrayAdapter<String>(SMSReceiverActivity.this, android.R.layout.simple_list_item_1,
bodyarr);
listview.setAdapter(arrayAdpt);
}
@Override
protected void onResume() {
super.onResume();
IntentFilter intentFilter = new IntentFilter("SmsMessage.intent.MAIN");
mIntentReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String msg = intent.getStringExtra("get_msg");
//Process the sms format and extract body & phoneNumber
msg = msg.replace("\n", "");
String body = msg.substring(msg.lastIndexOf(":")+1, msg.length());
String pNumber = msg.substring(0,msg.lastIndexOf(":"));
bodyarr.add(body);
arrayAdpt.notifyDataSetChanged();
/*
* Create/Open a SQLite database
* and fill with dummy content
* and close it
*/
mySQLiteAdapter = new SQLiteAdapter(this);
mySQLiteAdapter.openToWrite();
mySQLiteAdapter.deleteAll();
mySQLiteAdapter.insert(body);
mySQLiteAdapter.close();
}
};
this.registerReceiver(mIntentReceiver, intentFilter);
}
@Override
protected void onPause() {
super.onPause();
this.unregisterReceiver(this.mIntentReceiver);
}
}
这是我的 SQLiteAdapter 的代码:
package com.example.smsTest;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
public class SQLiteAdapter {
public static final String MYDATABASE_NAME = "MY_DATABASE";
public static final String MYDATABASE_TABLE = "MY_TABLE";
public static final int MYDATABASE_VERSION = 1;
public static final String KEY_ID = "_id";
public static final String KEY_CONTENT = "Content";
//create table MY_DATABASE (ID integer primary key, Content text not null);
private static final String SCRIPT_CREATE_DATABASE =
"create table " + MYDATABASE_TABLE + " ("
+ KEY_ID + " integer primary key autoincrement, "
+ KEY_CONTENT + " text not null);";
private SQLiteHelper sqLiteHelper;
private SQLiteDatabase sqLiteDatabase;
private Context context;
public SQLiteAdapter(Context c){
context = c;
}
public SQLiteAdapter openToRead() throws android.database.SQLException {
sqLiteHelper = new SQLiteHelper(context, MYDATABASE_NAME, null, MYDATABASE_VERSION);
sqLiteDatabase = sqLiteHelper.getReadableDatabase();
return this;
}
public SQLiteAdapter openToWrite() throws android.database.SQLException {
sqLiteHelper = new SQLiteHelper(context, MYDATABASE_NAME, null, MYDATABASE_VERSION);
sqLiteDatabase = sqLiteHelper.getWritableDatabase();
return this;
}
public void close(){
sqLiteHelper.close();
}
public long insert(String content){
ContentValues contentValues = new ContentValues();
contentValues.put(KEY_CONTENT, content);
return sqLiteDatabase.insert(MYDATABASE_TABLE, null, contentValues);
}
public int deleteAll(){
return sqLiteDatabase.delete(MYDATABASE_TABLE, null, null);
}
public Cursor queueAll(){
String[] columns = new String[]{KEY_ID, KEY_CONTENT};
Cursor cursor = sqLiteDatabase.query(MYDATABASE_TABLE, columns,
null, null, null, null, null);
return cursor;
}
public class SQLiteHelper extends SQLiteOpenHelper {
public SQLiteHelper(Context context, String name,
CursorFactory factory, int version) {
super(context, name, factory, version);
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL(SCRIPT_CREATE_DATABASE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
}
}
}
更新:
我认为这里需要 SmsReceiver 类:
这里是:
package com.example.smsTest;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsMessage;
import android.widget.Toast;
public class SmsReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Bundle extras = intent.getExtras();
if (extras == null)
return;
Object[] pdus = (Object[]) extras.get("pdus");
for (int i = 0; i < pdus.length; i++) {
SmsMessage SMessage = SmsMessage.createFromPdu((byte[]) pdus[i]);
String sender = SMessage.getOriginatingAddress();
String body = SMessage.getMessageBody().toString();
// A custom Intent that will used as another Broadcast
Intent in = new Intent("SmsMessage.intent.MAIN").
putExtra("get_msg", sender+":"+body);
// To display a Toast whenever there is an SMS.
Toast.makeText(context,body,Toast.LENGTH_LONG).show();
//You can place your check conditions here(on the SMS or the sender)
//and then send another broadcast
context.sendBroadcast(in);
/*Intent act = new Intent(context, NewActivity.class);
act.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
act.putExtra("message", body);
act.putExtra("sender", sender);
context.startActivity(act);*/
// This is used to abort the broadcast and can be used to silently
// process incoming message and prevent it from further being
// broadcasted. Avoid this, as this is not the way to program an app.
this.abortBroadcast();
}
}
}