如何从IntentService中的Service获取数据?

时间:2015-07-27 12:50:50

标签: java android google-cloud-messaging android-service

在我的应用程序中,我有一个服务(MainService),它在后台运行并处理一些数据,如图像,消息等。

我需要在我的GCM通知中显示此服务的其他数据(例如发件人的头像)。

我已经实现了这样的GCM:

GcmBroadcastReceiver.java:

public class GcmBroadcastReceiver extends WakefulBroadcastReceiver{
    @Override
    public void onReceive(Context context, Intent intent){
        ComponentName comp = new ComponentName(context.getPackageName(), GcmNotificationService.class.getName());
        startWakefulService(context, (intent.setComponent(comp)));
        setResultCode(Activity.RESULT_OK);
    }
}

GcmNotificationService.java:

public class GcmNotificationService extends IntentService{
    @Override
    protected void onHandleIntent(Intent intent){
        //Need to retreive data from MainService
        //Tried to bind to MainService here
        ...
        //Send notification
        GcmBroadcastReceiver.completeWakefulIntent(intent);
    }
}

问题是我不能简单地绑定到 onHandleIntent 中的MainService,因为它的线程早于我收到 onServiceConnected 时关闭。

我还尝试在MainService中扩展 IntentService ,但这不是一个好主意。

我应该怎么做才能实现这项功能?

0 个答案:

没有答案
相关问题