android - 在 GCMBaseIntentService 中显示 toast

标签 android

我正在尝试实现推送通知客户端。我想在 onMessage(Context arg0, Intent arg1) 函数中显示 toast 。

这是我的代码:

    public class GCMIntentService extends GCMBaseIntentService {
static Context c;
@SuppressWarnings("hiding")
private static final String TAG = "GCMIntentService";

public GCMIntentService() {
    super("717816998404");
    c = this.getBaseContext();
}

/**
 * Issues a notification to inform the user that server has sent a message.
 */

@Override
protected void onError(Context arg0, String arg1) {
    // TODO Auto-generated method stub

}

@Override
protected void onMessage(Context arg0, Intent arg1) {
    Log.d("GCM", "RECIEVED A MESSAGE");
    Log.d("GCM", "RECIEVED A MESSAGE");
    Log.d("GCM", "RECIEVED A MESSAGE");
    Log.d("GCM", "RECIEVED A MESSAGE");
    Log.d("GCM", "RECIEVED A MESSAGE");
    Log.d("GCM", "RECIEVED A MESSAGE");
    Log.d("GCM", "RECIEVED A MESSAGE");
    String str =arg1.getExtras().getString("PVAL");

    Log.i("PVAL",str);
    // I want to display a toast here

}

@Override
protected void onRegistered(Context arg0, String arg1) {
    // TODO Auto-generated method stub
    Log.i("TEST", arg1);
    Log.e("TEST", arg1);

}

@Override
protected void onUnregistered(Context arg0, String arg1) {
    // TODO Auto-generated method stub

}

我想知道如何显示接收到的消息我试图显示 toast 来检查消息。

最佳答案

试试这个:

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

            public void run() {
                // TODO Auto-generated method stub
            Toast.makeText(arg0, arg1.getStringExtra("message"), Toast.LENGTH_LONG).show();
    }         
        });

关于android - 在 GCMBaseIntentService 中显示 toast,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11364336/

相关文章:

java - 看来 ImageView.setImageBitmap 不起作用

java - 如何在 ImageView 中使用的图像中定义可点击区域

android - 使用 ResultReceiver 如果 Activity 被销毁并且 Intent 服务仍在运行会发生什么

android - android studio-依次播放音频文件

ANDROID-分配内存失败: 1455

java - 如何在DownloadManager中设置下载目录?

java - 按长属性对对象列表进行排序

java - 理解字段访问表达式

android - Eclipse DDMS 不显示 SD 卡的内容

android - listView android 在一个列表中可点击,在其他列表中不可点击