【问题标题】:How call an Intent switch from a c2dm message reciever如何从 c2dm 消息接收器调用 Intent 开关
【发布时间】:2012-01-07 10:51:59
【问题描述】:

我有一个简单的 c2dm 消息接收器类,只要设备接收到 c2dm 消息就会调用它。在一种情况下,我想让消息接收器类执行意图切换以加载不同的活动。发生这种情况时,Android 会抛出异常

01-07 02:28:52.480: E/AndroidRuntime(440): Caused by: android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity  context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?

我想我可以调查异常中建议的这个标志,但我想知道我是否采取了错误的方法并且有更好的方法来做到这一点?

c2dm 消息接收类:

public class C2DMMessageReceiver extends BroadcastReceiver {
    @Override
            public void onReceive(Context context, Intent intent) {
                String action = intent.getAction();
                Log.w("C2DM", "Message Receiver called");
                if ("com.google.android.c2dm.intent.RECEIVE".equals(action)) {
                    Log.w("C2DM", "Received message");
                    final String payload = intent.getStringExtra("payload");
                    Log.d("C2DM", "dmControl: payload = " + payload);

                    // Message handling
                    if(payload.equals("RdyRoom::join")) {
                        Intent rIntent = new Intent(context.getApplicationContext(), ReadyRoomActivity.class);
                        context.startActivity(rIntent);
                    }
                }
            }
}

感谢您的任何想法

【问题讨论】:

  • 我建议您阅读有关该标志的信息。它在 Intent 文档中。但是,您能否提供有关您要完成的工作的更多信息?用户是否在 Activity 中并正在等待此响应?之前发生了什么,之后你想发生什么?

标签: android


【解决方案1】:

将此添加到您的意图中

 Intent.FLAG_ACTIVITY_NEW_TASK

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-21
    • 1970-01-01
    • 1970-01-01
    • 2016-03-27
    相关资源
    最近更新 更多