我正在使用GCM开发具有推送通知功能的android应用程序。我创建了一个名为PushNotificationService的类,该类扩展了GCMListenerService。在onMessageReceived(String from, Bundle data)内部,我可以在推送通知中获取消息。

现在,无论何时在推送中收到特定消息时,我都想访问MainActivity类中的方法。

以下是我的代码:-

PushNotificationService.java

public class PushNotificationService extends GcmListenerService {

@Override
    public void onMessageReceived(String from, Bundle data) {
        // TODO Auto-generated method stub
        super.onMessageReceived(from, data);



         String message = data.getString("message");

         if(message.equalsIgnoreCase("Begin Task"))
            {

                    //call method from MainActivity.class

            }

        }
}


MainActivty.java

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

    }

    public void beginTask()

    {

        Log.d("GCM","Message Received from Server");
        finish();

    }
}


我希望每当收到消息“ Begin Task”时都执行beginTask()方法。

我知道一种方法是通过Service-> Interface-> Activity体系结构,但是由于我从未创建过PushNotificationService的对象,因此无法使用它。

请帮忙。

更新:-
我现在正在使用Otto Library,下面是我的代码。

添加了新的MyBus.java

public class MyBus extends Bus {
    private static Bus bus;
    //isRegistered is used to track the current registration status
    private static boolean isRegistered;
    private Handler handler = new Handler(Looper.getMainLooper());

    public MyBus() {
        if (bus == null) {
            //ANY will allow event bus to run even with services
            //and broadcast receivers
            bus = new Bus(ThreadEnforcer.ANY);
        }
    }

    @Override
    public void register(Object obj) {
        //The bus is registered when an activity starts
        bus.register(obj);
        isRegistered = true;
    }

    @Override
    public void unregister(Object obj) {
        //The bus is unregistered when an activity goes to background
        bus.unregister(obj);
        isRegistered = false;
    }

    @Override
    public void post(final Object event) {
        if (Looper.myLooper() == Looper.getMainLooper()) {
            //post the event in main thread or background thread
            bus.post(event);
        } else {
            handler.post(new Runnable() {
                @Override
                public void run() {
                    bus.post(event);
                }
            });
        }
    }

    public boolean isRegistered(){
        return isRegistered;
    }
}


PushNotificationService.java

public class PushNotificationService extends GcmListenerService {

@Override
    public void onMessageReceived(String from, Bundle data) {
        // TODO Auto-generated method stub
        super.onMessageReceived(from, data);

        MyBus myBus = new MyBus();
        myBus.register(myBus);

         String message = data.getString("message");

         if(message.equalsIgnoreCase("Begin Task"))
            {

                    myBus.post(message);

            }

        }
}


MainActivity.java

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

    }

    @Subscribe
    public void beginTask()

    {

        Log.d("GCM","Message Received from Server");


    }
}


问题仍然没有解决。 MainActivity.java中的beginTask()仍未调用。

最佳答案

使用eventBus库促进此过程...

我在此过程中使用Otto
http://square.github.io/otto/

这是另一个eventBus库https://greenrobot.github.io/EventBus/

脚步:
1.从服务创建事件
2.在活动中添加一个侦听器
3.如果活动正在运行,该方法将被执行

**编辑1:**

我已经像这样抽象了otto总线。

package com.mypackage.eventBus;

import android.os.Handler;
import android.os.Looper;

import com.squareup.otto.Bus;
import com.squareup.otto.ThreadEnforcer;

/**
 * Created by gowtham on 10/6/15.
 */
public class MyBus extends Bus {
    private static Bus bus;
    //isRegistered is used to track the current registration status
    private static boolean isRegistered;
    private Handler handler = new Handler(Looper.getMainLooper());

    public MyBus() {
        if (bus == null) {
            //ANY will allow event bus to run even with services
            //and broadcast receivers
            bus = new Bus(ThreadEnforcer.ANY);
        }
    }

    @Override
    public void register(Object obj) {
        //The bus is registered when an activity starts
        bus.register(obj);
        isRegistered = true;
    }

    @Override
    public void unregister(Object obj) {
        //The bus is unregistered when an activity goes to background
        bus.unregister(obj);
        isRegistered = false;
    }

    @Override
    public void post(final Object event) {
        if (Looper.myLooper() == Looper.getMainLooper()) {
            //post the event in main thread or background thread
            bus.post(event);
        } else {
            handler.post(new Runnable() {
                @Override
                public void run() {
                    bus.post(event);
                }
            });
        }
    }

    public boolean isRegistered(){
        return isRegistered;
    }
}


创建上述对象的实例,然后尝试发布事件

编辑2对Jcarlo的评论
请按照以下步骤查找活动的状态。


在您活动的onResume中,调用MyBus.getInstance()。register(this)。
在活动的onPause中,调用MyBus.getInstance()。unregister(this)。
在发布消息之前,在您的GCM IntentService中

if(MyBus.getInstance()。isRegistered()){
     // app还活着
     //发布数据
 }其他{
     //显示通知
 }


希望这可以帮助

07-25 22:47