【问题标题】:Update Activity/Trigger an Event when a Broadcast is Received while the activity is open在活动打开时接收到广播时更新活动/触发事件
【发布时间】:2014-09-16 08:56:19
【问题描述】:

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

我的问题是如何在活动打开时收到广播时更新消息列表(触发事件)? (就像他们在“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());
    }
}

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

我的MainActivity如下:

 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);
            }
       }

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

【问题讨论】:

    标签: android android-notifications


    【解决方案1】:

    两种方式,

    1. 要么在您的 Activity 中有一个静态 Handler 声明,并使用相同的 post 一条消息返回到 Activity,如果还活着则更新您需要的任何内容。

    喜欢:

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

    喜欢:

    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
    
            }
    
        }
    

    在我看来,第二个会更容易使用。

    【讨论】:

    • 您能像第一种方法一样描述第二种方法吗?
    • 这里你添加了一个示例,通过它并让我知道,以防你需要更多帮助。
    • 您刚刚对 MainActivity 中的 BroadCastreceiver 类进行了子类化,因此它可以通过 UI 进行调整。它肯定会工作但在我的情况下,我做了一个单独的类来扩展 BroadCastReceiver。那里我如何在 MainActivity 的 UI 中进行更改。谢谢你的回答。
    • 两种方式,或者有两个接收器,或者更多你的一个活动子类化一个。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-06
    • 1970-01-01
    • 1970-01-01
    • 2019-03-24
    • 1970-01-01
    相关资源
    最近更新 更多