我意识到,这里有一些问题,但我认为我的代码与他们完全不同(我是初学者),我无法理解他们的答案!
所以我想在每次收到短信时刷新我的ListView,我尝试使用cursor.requery();
和我在谷歌上找到的一些方法,但它仍然无效。
这是我的代码:
public class SMSActivity extends Activity implements OnItemClickListener {
ArrayList<String> smsList = new ArrayList<String>();
// String ADDRESS[];
// int total = 0;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ContentResolver cr = getContentResolver();
Cursor cursor = cr.query(Uri.parse("content://sms/inbox"), null, null,
null, null);
int indexBody = cursor.getColumnIndex("body");
int indexAddr = cursor.getColumnIndex("address");
if (indexBody < 0 || !cursor.moveToFirst())
return;
smsList.clear();
do {
String str = "Sender : " + cursor.getString(indexAddr) + "\n"
+ cursor.getString(indexBody);
smsList.add(str);
// ADDRESS[total] = cursor.getString(indexAddr);
// total++;
} while (cursor.moveToNext());
ListView lvSms = (ListView) findViewById(R.id.SMSList);
lvSms.setAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, smsList));
// cursor.requery();
lvSms.setOnItemClickListener(this);
}
已编辑: 这是我使用BroadcastReceiver扩展它的类:
public class SMSReceiver extends BroadcastReceiver {
private static final int NOTIF_ID = 0;
@Override
public void onReceive(Context arg0, Intent arg1) {
// TODO Auto-generated method stub
Bundle bundle = arg1.getExtras();
SmsMessage[] msgs = null;
String str = "";
if (bundle != null) {
Object[] pdus = (Object[]) bundle.get("pdus");
msgs = new SmsMessage[pdus.length];
for (int i = 0; i < msgs.length; i++) {
msgs[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
str += "You Get New SMS from " + msgs[i].getOriginatingAddress();
str += " :"; str += msgs[i].getMessageBody().toString();
str += "\n";
Toast.makeText(arg0, str, Toast.LENGTH_SHORT).show();
NotificationManager nm = (NotificationManager) arg0.getSystemService(Context.NOTIFICATION_SERVICE);
String tickerText = str;
Notification notif = new Notification(R.drawable.ic_launcher, tickerText, System.currentTimeMillis());
notif.flags = Notification.FLAG_AUTO_CANCEL;
String contentTitle = msgs[i].getOriginatingAddress();
String contentText = msgs[i].getMessageBody().toString();
Intent intent = new Intent(arg0, SMSReply.class);
PendingIntent pi = PendingIntent.getActivity(arg0, 0, intent, 0);
notif.setLatestEventInfo(arg0, contentTitle, contentText, pi);
notif.defaults = Notification.DEFAULT_ALL;
nm.notify(NOTIF_ID, notif);
String tempSMS = msgs[i].getOriginatingAddress();
Intent pass = new Intent();
Bundle bundlePass = new Bundle();
bundlePass.putString("key", tempSMS);
pass.putExtras(bundlePass);
}
}
}
我不明白如何链接这两个Class所以每次BroadcastReceiver工作(新的SMS来)我的ListView将被更新(就像手机中的短信应用程序)
全部谢谢。
答案 0 :(得分:1)
将适配器定义为全局公共变量
ArrayAdapter<String> adapter;
在onCreate()
内分配,
adapter=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, smsList));
在BroadcastReceiver
访问smsList
并添加新短信,
然后以这种方式访问你的适配器,
adapter.notifyDataSetChanged();
此函数将通知适配器smsList值已更改并将更新列表。
答案 1 :(得分:0)
您可以使用抽象方法创建BroadcastReceiver类,如下所示:
public class MyReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context arg0, Intent arg1) {
...
do your stuff here
...
myAbsMethod(newSms);
}
public abstract void myAbsMethod(String sms);
}
然后,在您的活动中,您可以执行以下操作:
public class MyActivity extends Activity {
private final MyReceiver myReceiver;
private ArrayList<String> smsList = new ArrayList<String>();
...
public MyActivity() {
myReceiver = new MyReceiver() {
@Override
public void myAbsMethod(String sms) {
smsList.add(sms);
}
};
}
@Override
public void onCreate(Bundle savedInstanceState) {
...
IntentFilter filterState = new IntentFilter(YOUR_SMS_ACTION);
registerReceiver(myReceiver, filterState)
}
...
@Override
public void onDestroy() {
...
unregisterReceiver(myReceiver);
}
}
所以,基本上,你是在活动课程中注册这个接收者,然后每当你收到这个广播时,你的活动就会听,你可以在这个被覆盖的抽象方法中做任何你想做的事。