我正在使用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()仍未被调用。
答案 0 :(得分:3)
使用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;
}
}
创建上述对象的实例并尝试发布事件
对于Jcarlo的评论,编辑2 请按照以下步骤查找活动的状态。
在发布消息之前在GCM IntentService中
如果(MyBus.getInstance()。isRegistered()){ // app还活着 //发布数据 }其他{ //显示通知 }
希望这有帮助
答案 1 :(得分:3)
您可以使用LocalBroadcastManager
。在GCMListener的LocalBroadcastManager
上创建mBroadcaster = LocalBroadcastManager.getInstance(this);
对象onCreate
并使用
@Override
public void onCreate() {
super.onCreate();
mBroadcaster = LocalBroadcastManager.getInstance(this);
}
@Override
public void onMessageReceived(String from, Bundle data) {
super.onMessageReceived(from, data);
String message = data.getString("message");
if(message.equalsIgnoreCase("Begin Task")) {
Intent i = new Intent();
i.setAction("yourPackageName");
i.putExtra("DATA", yourData);
mBroadcaster.send(i);
}
}
然后,您可以使用MainActivity
在<{1}}中接收消息。
BroadcastReceiver
您还需要在活动的BroadCastReceiver mBroadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
beginTask();
}
};
和onStart
中注册和取消注册接收方
onStop