gpt4 book ai didi

java - Activity 与 GCMListenerService Android 通信

转载 作者:行者123 更新时间:2023-11-29 06:56:04 26 4
gpt4 key购买 nike

我正在使用 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();

}
}

我希望在收到消息“开始任务”时执行 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.在 Activity 中添加监听器
3.如果 Activity 正在运行,将执行该方法

**编辑 1:**
我已经像这样抽象了奥托巴士。

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;
}
}

创建上述对象的实例并尝试发布事件

EDIT 2 用于 Jcarlo 的评论
按照以下步骤查找 Activity 的状态。

  1. 在您的 Activity 的 onResume 中调用 MyBus.getInstance().register(this)。
  2. 在您的 Activity 的 onPause 调用 MyBus.getInstance().unregister(this)。
  3. 在发布消息之前在您的 GCM IntentService 中

    if(MyBus.getInstance().isRegistered()){ //应用程序还活着 //发布数据 }别的{ //显示通知

希望对你有帮助

关于java - Activity 与 GCMListenerService Android 通信,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33888524/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com