繁体   English   中英

如何从广播接收器向主要活动发送消息

[英]How to send message to main activity from broadcastreceiver

我知道这是一个基本问题,但是这里有很多类似的问题,但是,我浏览了数十个问题,他们都以特定的方式提出问题,而他们的回答并不能解决我的问题。

在我的主要活动课中,我有:

public static class GcmBroadcastReceiver extends BroadcastReceiver {

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

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

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

    }

}

然后广播接收机会在您的mainactivity中调用onresume方法。

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

我希望它对您有用。

我认为收到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);  
        }
 }

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

由于您只是开始一项活动,因此不需要处理程序。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM