通过GCM onMessageReceived上的处理程序更新UI

时间:2015-11-30 20:34:46

标签: user-interface google-cloud-messaging handler android-handler

一个小问题。我如何从其他类调用处理程序。

从MainActivity我调用“myHandler.post(myRunnable);”,但我不知道如何调用...

感谢我们的回答!

主:

package gcm.play.android.samples.com.gcmquickstart;

import ...

public class MainActivity extends AppCompatActivity {

public static final Handler myHandler = new Handler();

final Runnable myRunnable = new Runnable() {
    public void run() {
        mMeldungTextView = (TextView) findViewById(R.id.MeldungTextView);
        mMeldungTextView.setText("click!");
        mp.start();
    }
};

的onReceive:

public class MyGcmListenerService extends GcmListenerService {

private static final String TAG = "MyGcmListenerService";
//public TextView mMeldungTextView;


// [START receive_message]
@Override
public void onMessageReceived(String from, Bundle data) {
    String message = data.getString("message");
    Log.d(TAG, "From: " + from);
    Log.d(TAG, "Message: " + message);

    if (from.startsWith("/topics/")) {
        // message received from some topic.
    } else {
        // normal downstream message.
    }

    MainActivity.msg = message;

    sendNotification(message);
    // I want to call handler in MainActivity at this point...
---> myHandler.post(myRunnable);

1 个答案:

答案 0 :(得分:1)

使用字符串额外信息从onMessageReceived(字符串来自,Bundle数据)向您的活动发送一个意图,这将是一条消息。

在您的活动中处理意图并向处理程序发送消息。