【问题标题】:Display GCM message in Android在 Android 中显示 GCM 消息
【发布时间】:2013-02-12 16:40:34
【问题描述】:

我为 android 编写应用程序,它将与 GCM 通信。我可以收到消息,但我想在屏幕上显示它并收到错误消息。
有我的代码,我在 Activity act = (Activity) context 行有问题;
我收到错误“此类文件的 JAR 属于容器‘Android 依赖项’,它不允许修改其条目上的源附件”

@Override
protected void onMessage(Context context, Intent indent) {

    String message = indent.getExtras().getString("message").toString();

    Log.i(TAG, "new message= " + message);

    Activity act = (Activity) context;  
    if(act != null)
    {
        TextView pushNotification = (TextView) act.findViewById(R.id.txtPushNotify);    
        pushNotification.setText(message);
    }
}

我做错了什么??这个方法在类中

public class GCMIntentService extends GCMBaseIntentService {...}

这是我的 LogCat

致命异常:IntentService[GCMIntentService-19193409722-1] java.lang.ClassCastException:android.app.Application
在 com.sagar.gcma.GCMIntentService.onMessage(GCMIntentService.java:41)
在 com.google.android.gcm.GCMBaseIntentService.onHandleIntent(GCMBaseIntentService.java:223)
在 android.app.IntentService$ServiceHandler.handleMessage(IntentService.java:59)
在 android.os.Handler.dispatchMessage(Handler.java:99)
在 android.os.Looper.loop(Looper.java:123)
在 android.os.HandlerThread.run(HandlerThread.java:60)

【问题讨论】:

  • 您可能想发布整个 logcat 错误,为人们提供更好的信息
  • 您正在尝试将 Service 的上下文投射到 Activity 中,我认为这是不可能的。
  • 如何在屏幕上显示 GCMIntentService 类的内容?
  • 你为什么写Activity act = (Activity) context;代码?相反,您应该创建一个新的 Intent 并启动该活动。

标签: java android google-cloud-messaging


【解决方案1】:

试试下面的代码。

Intent myIntent = new Intent(context.getApplicationContext(), YourActivity.class);
Bundle bundle = new Bundle();
bundle.putString("message", message);
myIntent.putExtras(bundle);
context.getApplicationContext().startActivity(myIntent);

然后在 activity 中编写消息显示代码。

【讨论】:

    【解决方案2】:

    错误消息“此类文件的 JAT 属于容器'Android 依赖项',不允许修改其条目上的源附件”似乎(不知何故)与我无关,因为它是由生成的错误消息IDE,与您的实际代码无关。

    我会小心代码中的演员:

    Activity act = (Activity) context;  
    

    您确定传递的上下文实际上是(在任何情况下)您的活动吗?

    编辑:

    阅读您的编辑后,我可以确认您收到的上下文是您的应用程序,而不是活动。

    并且您需要以某种方式将该消息(您的服务收到)中继到前台活动(如果处于活动状态)。如果没有前台活动,请使用通知或类似的东西。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-02-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多