【问题标题】:Background service in AndroidAndroid中的后台服务
【发布时间】:2014-06-07 14:29:32
【问题描述】:

我正在开发一个拥有自己的数据库并将通过 GCM 与后端同步的应用程序,我正在考虑使用后台服务,但我不确定这是否是正确的思考方式,所以,如果您能告诉我以下内容是否正确,我将不胜感激,如果不是,请说明我需要分步做什么或应该如何考虑?不需要代码。

当应用没有运行/活动活动时,假设 GCM 有负载并且不需要联系后端,

1. Backend had new data and sent it with GCM 
2. Background service received it and updated the DB

应用当前运行时

1. Backend had new data and sent it with GCM 
2. Background service received it and updated the DB and notifydatasetchanged
3. Data on activity will be changed as the source has changed(e.g listview update it's items)

【问题讨论】:

    标签: android intentservice


    【解决方案1】:

    总的来说,您的想法是可以的。但是你不需要一直运行后台Service。只需创建WakefulBroadcastReceiver 并将其添加到您的Manifest

    <receiver
            android:name=".receivers.GCMReceiver"
            android:permission="com.google.android.c2dm.permission.SEND">
    
        <intent-filter>
            <action android:name="com.google.android.c2dm.intent.RECEIVE" />
            <category android:name="com.your.package" />
        </intent-filter>
    </receiver>
    

    接收器可能如下所示:

    public class GCMReceiver extends WakefulBroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            ComponentName comp = new ComponentName(context.getPackageName(), GCMService.class.getName());
            startWakefulService(context, (intent.setComponent(comp)));
        }
    }
    

    此接收器启动您的Service(GCMService)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-12-26
      • 1970-01-01
      • 1970-01-01
      • 2020-06-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多