繁体   English   中英

在Android中显示GCM消息

[英]Display GCM message in Android

我为android编写应用程序,它将与GCM通信。 我可以收到消息,但是我想在屏幕上显示它并出现错误。
有我的代码,我在Activity act =(Activity)上下文中遇到问题;
我收到错误消息“此类文件的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)

尝试以下代码。

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

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

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

我会谨慎使用代码中的强制转换:

Activity act = (Activity) context;  

您确定所传递的上下文实际上是(无论如何)您的活动吗?

编辑:

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

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

暂无
暂无

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

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