在活动打开时接收广播时更新活动/触发事件

时间:2014-09-16 08:56:19

标签: android android-notifications

我制作了一个广播接收器,通知从服务器收到消息的时间。 当我点击通知时,它打开了预期的活动,并且还更新了消息列表。

我的问题是在活动开放时收到广播时如何更新消息列表(触发事件)? (就像他们在WhatsApp'中做的那样,他们在聊天时不发送通知,而是更新消息列表)。

我的广播接收器如下:

public class NotifReceiver extends BroadcastReceiver {
    private Context context;
    private String notifTitle,notifStatus;
    public static int NOTIF_ID = 54321;

    @Override
    public void onReceive(Context context, Intent intent) {
        this.context = context;

         if (intent.getAction().equals("android.intent.action.BOOT_COMPLETED")) {
            context.startService(new Intent(context, NotifService.class));
            Log.i("Aditya", "Background Service of SamajApp Started");
         }
        if(intent.getAction().equals("Aditya.Notification")){
            showNotif();
        }
    }

    public void showNotif(){
        NotificationManager notifManager = NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);

        Intent i = new Intent(context, MainActivity.class);
        i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        PendingIntent pIntent = PendingIntent.getActivity(context, 0, i, 0);

        Uri soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

        NotificationCompat.Builder notif = new NotificationCompat.Builder(context);
        notif.setContentTitle("SamajApp");
        notif.setContentText(notifTitle);
        notif.setSmallIcon(R.drawable.logo);
        notif.setSound(soundUri);
        notif.setContentIntent(pIntent);
        notifManager.notify(NOTIF_ID, notif.build());
    }
}

我已经在清单中注册了它并且它运行良好。

我的主要活动如下:

 public class MainActivity extends Activity {
       private ListView mListView;

       @Override
        protected void onCreate(Bundle savedInstanceState) {
           super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main); 
             mListView = (ListView) findViewById(R.id.listViewHome);

             updateList();

        }

        //MsgListAdapter is my custom Adapter to populate Listview,
        private void updateList(){
        registerForContextMenu(mListView);
        Vector<HashMap<String, String>> list = getMsgListfromDB();  //gets message list from database
        Vector<HashMap<String, byte[]>> blobList = getBlobListfromDB();  //gets blob list from db.
        ad = new MsgListAdapter(getApplicationContext(), list,blobList,R.layout.listrow,
                new String[] { "MsgCat", "MsgDt_Simp","by","FlagUnread"},
                new int[] {R.id.textViewCat, R.id.textViewDateTime,R.id.textViewBy},
                new int[] {R.id.imageViewMsgImg,R.id.imageViewCatImg},R.id.imageViewUnreaddr);
        mListView.setAdapter(ad);
            }
       }

对此有任何帮助将不胜感激。 谢谢。

1 个答案:

答案 0 :(得分:1)

两种方式,

  1. 在您的Activity中有一个静态处理程序声明,并使用相同的帖子将消息重新发送到活动,如果活动更新您需要的任何内容。
  2. 像:

    public Handler MyHandler= new Handler() {
        public void handleMessage(android.os.Message msg) {
            // Update your UI
        }
    
    };
    
    1. 将您的广播接收器声明为您的Activity类中的静态子类,因此,一旦您收到后续广播,您就可以轻松更新UI。
    2. 像:

      public class MainActivity extends Activity {
      
      
          private MyReceiver mReceiver;
          @Override
          protected void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.activity_main);
      
          }
      
      
      
          @Override
          protected void onResume() {
              // TODO Auto-generated method stub
              super.onResume();
              IntentFilter mFilter = new IntentFilter();
              // add action to this filters
      
              // Initialize ur receiver
              mReceiver = new MyReceiver();
              // register when activity is resumed
              registerReceiver(mReceiver, mFilter);
          }
      
          @Override
          protected void onPause() {
              // TODO Auto-generated method stub
              super.onPause();
              // unregister when activity is paused
              if(mReceiver != null){
                  unregisterReceiver(mReceiver);
              }
          }
      
      
      
          class MyReceiver extends BroadcastReceiver{
      
              @Override
              public void onReceive(Context context, Intent intent) {
                  // This is ur receiver do whatever you want here wIth UI
      
              }
      
          }
      

      在我看来,第二个会更简单,更好用。