2

I know this is a basic question, and there a lot of similar questions on here, BUT, I've looked through dozens and they all ask their questions in a specific way, and their answer does not fix my problem.

inside my main activity class I have:

public static class GcmBroadcastReceiver extends BroadcastReceiver {

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

I want to transition to a new screen/activity when I get a certain gcm message. This needs to be done from the context of mainActivity. so how do I send a message to main activity to tell it to perform this action. I think I'm supposed to use a handler, but I dont know what the exact syntax is, in this case. I never "create" the broadcastreceiver, so I cant pass in some handler in its constructor. The BCR is set up through an intent filter through my manifest file. this is how the android tutorial on gcm has it set up, so I prefer not to create a broadcast receiver dynamically (unless its the only way).

4

2 回答 2

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

我希望它对你有用。

于 2013-09-21T04:01:52.467 回答
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);  
        }
 }

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

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

于 2013-09-21T03:53:56.317 回答