简体   繁体   中英

Update activity UI on sms received from broadcast receiver

  1. I have an SMSlist_activity.java that displays the list of an sms-conversation.
  2. I have an sms_receiver.java that is a broadcast receiver for incoming sms.

Now I want to update sms conversation list in SMSlist_activity.java when an sms received via sms_receiver.java , of course this update will just happens if the SMSlist_activity.java is running and visible.

If anyone has an idea, I will be glad to here that.

Finally I found a solution with Broadcast receivers. int sms_receiver.java, when sms received, I broadcast an intent:

            Intent intent2 = new Intent();
            intent2.setAction("co.ir.ts.app.sms.smsumad");
            context.sendBroadcast(intent2);

In app manifest in SMSlist_activity.java definition I add Intent filter so my SMSlist_activity.java can receive broadcast:

    <activity
        android:name=".activity.SMSlist_activity">
        <intent-filter>
            <action android:name="co.ir.ts.app.sms.smsumad" >
            </action>
        </intent-filter>
    </activity>

Now in SMSlist_activity.java main class define a broadcast receiver:

private BroadcastReceiver receiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
       //Updating UI here
    }
};

in onResume method register the receiver created above:

 @Override
protected void onResume() {
    // TODO Auto-generated method stub
     IntentFilter filter = new IntentFilter();
     filter.addAction("co.ir.ts.app.sms.smsumad");
     registerReceiver(receiver, filter);
    super.onResume();
}

and finally in onPause method unregister the receiver:

 protected void onPause()
{
    unregisterReceiver(receiver);
    super.onPause();
    if (isSentPending)
    {
        unregisterReceiver(sent);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM