Android GCMIntentService onMessage上下文问题

时间:2012-08-28 16:42:33

标签: android android-intent google-cloud-messaging

我正在尝试将GCM应用到Android中的应用。服务器和客户端设置似乎是正确的,因为当我从服务器端“推”一个字符串时调用onMessage方法。我可以从意图中读取额外内容,但是,使用通知或Toast消息不起作用。即使应用程序正在运行,手机也没有显示任何内容,所以我想我使用回调中使用的上下文对象有些不对劲。 这是manifest.xml的revelvant部分,其中PACKAGE是基础包。

<receiver
   android:name="PACKAGE.gcm.GCMReceiver"
   android:enabled="true"
   android:permission="com.google.android.c2dm.permission.SEND" >
      <intent-filter>
             <action android:name="com.google.android.c2dm.intent.RECEIVE" />
             <action android:name="com.google.android.c2dm.intent.REGISTRATION" />
            <category android:name="PACKAGE" />
     </intent-filter>
</receiver>

<service
   android:name="PACKAGE.gcm.GCMIntentService"
   android:enabled="true" />
</application>

<permission
    android:name="PACKAGE.permission.C2D_MESSAGE"
    android:protectionLevel="signature" />

<uses-permission android:name="PACKAGE.C2D_MESSAGE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.CALL_PHONE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<!-- App receives GCM messages. -->
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<!-- GCM requires a Google account. -->
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<!-- Keeps the processor from sleeping when a message is received. -->
<uses-permission android:name="android.permission.WAKE_LOCK" />
</manifest>

现在onMessage方法:

@Override
    protected void onMessage(Context context, Intent intent)
    {
        if (DEBUG)
        {
            System.out.println("[GCMIntentService] Message Received! " + intent.getStringExtra("message"));
        }

        Toast.makeText(context, intent.getStringExtra("message"), Toast.LENGTH_SHORT).show();

        String ns = Context.NOTIFICATION_SERVICE;
        NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);
        // int icon = R.drawable.notification_icon;
        CharSequence tickerText = intent.getStringExtra("message");
        long when = System.currentTimeMillis();

        Notification notification = new Notification(0, tickerText, when);

        CharSequence contentTitle = "Some Notification";
        CharSequence contentText = tickerText + " some more text";

        notification.setLatestEventInfo(context, contentTitle, contentText, null);

        final int HELLO_ID = 1;

        mNotificationManager.notify(HELLO_ID, notification);

    }

Toast和Notification不起作用。我在第一个Activity的onCreate中调用服务和注册例程,它被用作Splash并在几秒钟后关闭。可能与它有关吗?

1 个答案:

答案 0 :(得分:6)

这不是Context问题。

GCM接收器在一个意图服务中运行,该服务在一个单独的线程上运行。

要显示Toast,只需从UIThread内部调用即可。您可以这样做:

Handler h = new Handler(Looper.getMainLooper());
h.post(new Runnable(){

    Toast.makeText(context, intent.getStringExtra("message"), Toast.LENGTH_SHORT).show();

});

这将在您的UI线程上发布,然后您将看到吐司!

(你需要让你的本地变量的final由匿名类执行)

干杯!