簡體   English   中英

將活動與broadCast Receiver問題同步

[英]Sync Activity with broadCast Receiver issue

我想將我的活動與Google雲消息傳遞同步。 當GCM消息收到時,它自己的接收者獲取消息並創建通知,然后將我的自定義消息廣播到活動接收者。

另一方面,我的活動具有自己動態注冊的BroadcastReceiver,用於接收我的提示消息。

現在這是一種情況:

  • 當應用打開時,沒有單擊通知,我的活動接收者會收到消息並顯示。
  • 但是當點擊通知后關閉活動時,注意接收並僅打開應用程序。

我嘗試了以下方式:

  1. 注冊一個類BroadCast接收器。 但我無法將其與我的活動同步。 因為我發現外部Receiver可以僅使用putextra進行sysnc活動,然后我的活動應關閉,然后再次打開才能獲得額外收益!
  2. 嘗試在創建通知時再次廣播我的自定義消息,但似乎無法正常工作,因為我需要對NotificationClick進行廣泛的投射,而不是onCreate通知。
  3. finnaly,我嘗試在的另一部分上動態注冊此內部接收器,但無法訪問。

因此,如果您遇到我的問題,最好的解決方法是什么?

這是我的活動接收者:

private final BroadcastReceiver mHandleMessageReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            Log.i("LOG", "unreciver");
            String newMessage = intent.getExtras().getString(EXTRA_MESSAGE);
            // Waking up mobile if it is sleeping
            WakeLocker.acquire(getApplicationContext());

            /**
             * Take appropriate action on this message
             * depending upon your app requirement
             * For now i am just displaying it on the screen
             * */

            //Showing received message
            //lblMessage.append(newMessage + "\n");
            Log.i("LOG", "unreciver messsage:"+newMessage);
            //Toast.makeText(getApplicationContext(), "New Message: " + newMessage, Toast.LENGTH_LONG).show();
            loadReciverDialog(newMessage);
            // Releasing wake lock
            WakeLocker.release();
        }
    };

這是從GCM接收消息並創建通知的服務的一部分:

     @Override
        protected void onMessage(Context context, Intent intent) {

            Log.i(TAG, "Received message");
            String message = intent.getExtras().getString("price");

            Log.i("LOG", "GCM service Message "+message);

            displayMessage(context, message);
            // notifies user
            generateNotification(context, message);
        }

 private static void generateNotification(Context context, String message) {

        Log.i("LOG", "genetaret notify");
        int icon = R.drawable.ic_launcher;
        long when = System.currentTimeMillis();
        NotificationManager notificationManager = (NotificationManager)
                context.getSystemService(Context.NOTIFICATION_SERVICE);
        Notification notification = new Notification(icon, message, when);

        String title = context.getString(R.string.app_name);

        Intent notificationIntent = new Intent(context, MainActivity.class);

        // set intent so it does not start a new activity
        notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |
                Intent.FLAG_ACTIVITY_SINGLE_TOP);
        PendingIntent intent =
                PendingIntent.getActivity(context, 0, notificationIntent, 0);
        notification.setLatestEventInfo(context, title, message, intent);
        notification.flags |= Notification.FLAG_AUTO_CANCEL;

        // Play default notification sound
        notification.defaults |= Notification.DEFAULT_SOUND;

        //notification.sound = Uri.parse("android.resource://" + context.getPackageName() + "your_sound_file_name.mp3");


        // Vibrate if vibrate is enabled
        notification.defaults |= Notification.DEFAULT_VIBRATE;
        notificationManager.notify(0, notification);      

    }

}

這部分顯示消息:

  static void displayMessage(Context context, String message) {
        Intent intent = new Intent(DISPLAY_MESSAGE_ACTION);
        intent.putExtra(EXTRA_MESSAGE, message);
        intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
        Log.i("LOG", "commonutils msg="+message);
        context.sendBroadcast(intent);

    }

我終於以這種方式解決了這個問題:

  • 我保留了活動中的廣播接收器,並創建了一個用於檢查是否存在Extras的函數,我在O​​nReceive函數下的代碼是否有效。
  • 創建通知后,向主活動發送額外消息以獲取它們

所以我的應用程序通過兩種方式獲取消息:

  1. 當應用程序打開時,消息將與接收者一起給出
  2. 當應用關閉時,消息會額外發送給應用

但我想知道是否有更好的方法與一個Receiver一起工作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM