Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/210.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 与广播接收器问题同步活动_Android_Android Intent_Synchronization_Broadcastreceiver_Google Cloud Messaging - Fatal编程技术网

Android 与广播接收器问题同步活动

Android 与广播接收器问题同步活动,android,android-intent,synchronization,broadcastreceiver,google-cloud-messaging,Android,Android Intent,Synchronization,Broadcastreceiver,Google Cloud Messaging,我想将我的活动与谷歌云消息同步。 当GCM消息接收到自己的接收者时,获取消息并创建通知,然后将我的自定义消息广播给活动接收者 另一方面,我的活动有自己的动态注册的BroadcastReceiver,它接收我的cusom消息 现在情况是这样的: 当应用程序打开时,不点击通知,我的活动 接收器接收消息并显示 但当活动在单击通知后关闭时 接收并打开应用程序 我试过这样的方法: 在maifest上注册一个类广播接收器。但我无法将其与我的活动同步。因为我发现外部接收器可以仅通过putextra使用ac

我想将我的活动与谷歌云消息同步。 当GCM消息接收到自己的接收者时,获取消息并创建通知,然后将我的自定义消息广播给活动接收者

另一方面,我的活动有自己的动态注册的BroadcastReceiver,它接收我的cusom消息

现在情况是这样的:

  • 当应用程序打开时,不点击通知,我的活动 接收器接收消息并显示
  • 但当活动在单击通知后关闭时 接收并打开应用程序
我试过这样的方法:

  • 在maifest上注册一个类广播接收器。但我无法将其与我的活动同步。因为我发现外部接收器可以仅通过putextra使用activity sysnc,然后我的activity应该关闭,然后再次打开以获取extras
  • 尝试在creates notification上再次广播我的自定义消息,但它似乎不起作用,因为我需要在notification上广播单击not onCreate notification
  • 最后,我尝试在的另一个部分动态注册这个内部接收器,但无法访问
  • 如果你遇到我的问题,最好的解决方法是什么

    这是我的活动接收器:

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

    我最终用这种方式解决了这个问题:

    • 我保留了活动中的广播接收器,并创建了一个功能,用于检查是否存在额外的内容。OnReceive功能下的代码有效
    • 在创建通知后,将消息放入额外的主活动以获取它们
    因此,我的应用程序通过两种方式获取消息:

  • 当应用程序打开时,将向接收者发送消息
  • 当应用程序关闭时,将额外向应用程序发送消息
  • 但我想知道是否有更好的方法与一个接收器一起工作