使用广播接收器更新列表活动

时间:2014-03-20 19:24:22

标签: android broadcastreceiver listactivity

我最近开始进行Android开发,并希望做一些实验。 我有一个电话号码列表,当我点击其中一个时,它会显示ShowPhoneActivity,其中包含我在主要活动中点击的数字的短信列表

这是ShowPhoneActivity中onCreate方法的代码:

 protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        //PhoneDao is a class that handle Phone details
        dao = new PhoneDAO(this);
        //this receives the data of the main activity
        Bundle showData = getIntent().getExtras();
        phoneNumber = showData.getString("phone_number");
        phoneID = showData.getInt("phone_id");

        //Query for the sms
        Uri uri = Uri.parse("content://sms/inbox");
        Cursor c = getContentResolver().query(uri, null, "address = \"" + phoneNumber + "\"", null, null);
        startManagingCursor(c);

        // Read the sms data and store it in the list
        if (c.moveToFirst()) {
            for (int i = 0; i < c.getCount(); i++) {
                SMSData sms = new SMSData();
                sms.setBody(c.getString(c.getColumnIndexOrThrow("body")).toString());
                sms.setNumber(c.getString(c.getColumnIndexOrThrow("address")).toString());
                smsList.add(sms);
                c.moveToNext();
            }
        }

        // Set smsList in the ListAdapter
        setListAdapter(new ListSMSAdapter(this, smsList));

    }

此代码可能放在其他地方(我不知道在哪里)。所以一切正常,但我知道每当我收到新的短信时,我想更新列表。所以为了做到这一点,我发现我必须创建一个BroadcastReceiver并且我已经完成了这个。

public class IncomingSms extends BroadcastReceiver {

    // Get the object of SmsManager
    final SmsManager sms = SmsManager.getDefault();

    public void onReceive(Context context, Intent intent) {

        final Bundle bundle = intent.getExtras();

        try {
            if (bundle != null) {
                final Object[] pdusObj = (Object[]) bundle.get("pdus");
                for (int i = 0; i < pdusObj.length; i++) {
                    SmsMessage currentMessage = SmsMessage.createFromPdu((byte[]) pdusObj[i]);
                    String phoneNumber = currentMessage.getDisplayOriginatingAddress();
                    String senderNum = phoneNumber;
                    String message = currentMessage.getDisplayMessageBody();

                    /*Log.i("SmsReceiver", "senderNum: " + senderNum + "; message: " + message);

                    int duration = Toast.LENGTH_LONG;
                    Toast toast = Toast.makeText(context, "senderNum: " + senderNum + ", message: " + message, duration);
                    toast.show();*/

                } // end for loop
            } // bundle is null
        } catch (Exception e) {
            Log.e("SmsReceiver", "Exception smsReceiver" + e);
        }
    }
}

toast工作正常,但我想在我的活动中更新List,我发现了以下替代方案:

  • 更新适配器(据我搜索,我没有在此处正确设置适配器) with notifyDatasetChanged ,但我不知道该放哪个方法。
  • 在ShowPhoneActivity中,使用BroadcastReceiver对象创建onPause和onResume方法,在onPause内写入代码以填充列表。我不确定我是否必须 将相同的代码粘贴到onCreate。

正如我的问题的标题所说,我需要帮助,或者有关于此的一些教程。

提前致谢

1 个答案:

答案 0 :(得分:0)

首先,您必须在主要活动中注册广播接收器

IntentFilter intentFilter = new IntentFilter(
                "com.android.mymessage");
        registerReceiver(messageReceiver, intentFilter);

然后实现广播接收器

 private BroadcastReceiver messageReceiver = new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent) {
            if (intent.getExtras().getString("message") != null) {
                if (intent.getExtras().getString("message").equals("abc")) {
                    Intent nextscreen = new Intent(SecondVerification.this, ThirdVerification.class);
                    startActivity(nextscreen);
                }
            }
        }
    };


    @Override
    public void onDestroy() {
        super.onDestroy();
        unregisterReceiver(messageReceiver);
    }

现在将更改的数据发送到发生更改的接收器

Intent verifyIntent=new Intent("com.android.mymessage");
                        verifyIntent.putExtra("message",message);
                        context.sendBroadcast(verifyIntent);