如何从broadcastreceiver向主要活动发送消息

时间:2013-09-21 03:10:28

标签: java android

我知道这是一个基本问题,这里有很多类似的问题,但是,我已经浏览了几十个,他们都以特定的方式提出问题,他们的答案并没有解决我的问题。 / p>

在我的主要活动课程中:

public static class GcmBroadcastReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
            }
}

当我收到某个gcm消息时,我想转换到新的屏幕/活动。这需要从mainActivity的上下文中完成。所以我如何向主活动发送消息告诉它执行此操作。我想我应该使用一个处理程序,但在这种情况下我不知道确切的语法是什么。我从不“创建”广播接收器,因此我无法在其构造函数中传递一些处理程序。 BCR是通过我的清单文件中的intent过滤器设置的。这就是gcm上的android教程如何设置,所以我不想动态创建广播接收器(除非是唯一的方法)。

2 个答案:

答案 0 :(得分:1)

public class GCMIntentService extends GCMBaseIntentService {

    public static final String PROJECT_ID = "345657565857";
    private static final String TAG = "GCMIntentService";
    ModelNotificationMessage modelNotificationMessage;

    public GCMIntentService() {
        super(PROJECT_ID);
        Log.d(TAG, "GCMIntentService init");
    }

    @Override
    protected void onError(Context ctx, String sError) {
        // TODO Auto-generated method stub
        Log.d(TAG, "Error: " + sError);

    }

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

        Log.d(TAG, "Message Received");

        String message = intent.getStringExtra("message");

        Log.d(TAG, "Message Received" + message);



                    sendNotification(message);
            Intent broadcastIntent = new Intent();
            broadcastIntent.setAction("GCM_RECEIVED_ACTION");
            broadcastIntent.putExtra("gcm", message);
            ctx.sendBroadcast(broadcastIntent);

    }

    private void sendNotification(String message) {
        // this
        String ns = Context.NOTIFICATION_SERVICE;
        NotificationManager mNotificationManager = (NotificationManager)                            getSystemService(ns);

        int icon = R.drawable.notification;
        CharSequence tickerText = message; // ticker-text
        long when = System.currentTimeMillis();
        Context context = getApplicationContext();
        CharSequence contentTitle = modelNotificationMessage.getKey();
        CharSequence contentText = message;
        Intent notificationIntent = null;

        int NOTIFICATION_ID = 9999;

                NOTIFICATION_ID = CommonVariable.notification_message;
                notificationIntent = new Intent(this, ViewMessages.class);



        // and this
        PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
                notificationIntent, 0);
        Notification notification = new Notification(icon, tickerText, when);
        // Play default notification sound
        notification.defaults |= Notification.DEFAULT_ALL;
        notification.setLatestEventInfo(context, contentTitle, contentText,
                contentIntent);
        mNotificationManager.notify(NOTIFICATION_ID, notification);
    }

    @Override
    protected void onRegistered(Context ctx, String regId) {
        // TODO Auto-generated method stub
        // send regId to your server
        Log.d(TAG, regId);

    }

    @Override
    protected void onUnregistered(Context ctx, String regId) {
        // TODO Auto-generated method stub
        // send notification to your server to remove that regId

    }

}

然后广播接收器在onresume方法上调用mainactivity。

public void onResume() {

        gcmFilter = new IntentFilter();
        gcmFilter.addAction("GCM_RECEIVED_ACTION");
        viewMessages.registerReceiver(gcmReceiver, gcmFilter);

    }





// A BroadcastReceiver must override the onReceive() event.
    private BroadcastReceiver gcmReceiver = new BroadcastReceiver() {

        private String broadcastMessage;

        @Override
        public void onReceive(Context context, Intent intent) {

            broadcastMessage = intent.getExtras().getString("gcm");

            if (broadcastMessage != null && viewMessages != null) {
                // display our received message
                 onResume();
            }
        }
    };

我希望它对你有用。

答案 1 :(得分:0)

我认为在收到GCM消息时您要切换到某个活动/屏幕。为此,以下是从BroadcastReceiver开始执行活动的代码:

公共静态类GcmBroadcastReceiver扩展了BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
             //start activity
             Intent i = new Intent();
             //syntax: i.setClassName("packageName","Activity to start inside packageName:);
             i.setClassName("com.test", "com.test.MainActivity");
             i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
             context.startActivity(i);  
        }
 }

处理程序是您正在启动的工作线程与主线程之间的通信方式。

由于您刚刚开始活动,因此您不需要处理程序来执行此操作。