【问题标题】:How to send message to main activity from broadcastreceiver如何从广播接收器向主要活动发送消息
【发布时间】:2014-02-15 04:55:15
【问题描述】:

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

在我的主要活动课程中:

public static class GcmBroadcastReceiver extends BroadcastReceiver {

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

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

【问题讨论】:

  • 你的意思是告诉每当你得到你的广播接收器,你想切换到一个新的活动/屏幕?我说得对吗。

标签: java android


【解决方案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();
            }
        }
    };

希望对你有用。

【讨论】:

  • 这里的 gcmfilter 是什么?必须回复
【解决方案2】:

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

Handler 是您正在启动的工作线程和主线程之间的一种通信方式。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-11-08
    • 1970-01-01
    • 2011-04-23
    • 2018-02-27
    • 1970-01-01
    • 1970-01-01
    • 2012-06-05
    • 1970-01-01
    相关资源
    最近更新 更多