【问题标题】:Pass objects to IntentService from activity将对象从活动传递给 IntentService
【发布时间】:2014-08-10 20:38:19
【问题描述】:

为了在我的应用中节省电池电量,我决定使用“新”融合位置。但是我需要将一些参数传递给接收 GPS 更新的服务。下面的方法可以工作 (putExtras(...)),但我需要制作很多类 Serializable/Parseable,这会很痛苦。

我四处搜索并找到了使用 Binder 的其他方法,但不知道如何让它工作。使用 Binder 是唯一的方法还是有其他方法?

如果有什么不清楚的地方,请告诉。 谢谢。

public class LocationService extends IntentService {
    ...
    public LocationService(StartActivity startActivity, DatabaseSQLite db, HomeFragment homeFragment) {
        super("Fused Location Service");
        ...
    }

   @Override
   public int onStartCommand(Intent intent, int flags, int startId) {
   db = (DatabaseSQLite) intent.getExtras().get("DatabaseSQLite");

   ...
   return START_REDELIVER_INTENT;

    }
}

这就是它在我的活动中的使用方式:

@Override
    public void onConnected(Bundle bundle) {
        mIntentService = new Intent(this, LocationService.class);
        mIntentService.putExtra("DatabaseSQLite", database);
        ...
        mPendingIntent = PendingIntent.getService(this, 1, mIntentService, 0);

}

【问题讨论】:

    标签: java android service gps


    【解决方案1】:

    你应该看看https://github.com/greenrobot/EventBus

    可以在此处找到示例:http://awalkingcity.com/blog/2013/02/26/productive-android-eventbus/

    基本上可以让您执行以下操作:

    @Override
        public void onConnected(Bundle bundle) {
            mIntentService = new Intent(this, LocationService.class);
    
            // could be any object
            EventBus.getDefault().postSticky(database);
            ...
            mPendingIntent = PendingIntent.getService(this, 1, mIntentService, 0);
    
    }
    

    只要你需要对象

    public class LocationService extends IntentService {
        ...
        public LocationService(StartActivity startActivity, DatabaseSQLite db, HomeFragment homeFragment) {
            super("Fused Location Service");
            ...
        }
    
       @Override
       public int onStartCommand(Intent intent, int flags, int startId) {
    
       // could also be in Broadcast Receiver etc..
       db = EventBus.getDefault().getStickyEvent(DatabaseSQLite.class); 
    
       ...
       return START_REDELIVER_INTENT;
    
        }
    }
    

    不仅更简单,这个链接还表明它优于其他方法:http://www.stevenmarkford.com/passing-objects-between-android-activities/

    【讨论】:

    • 看起来很不错,但是无法使用。我需要在我的 LocationService 类以及该类调用 EventBus.getDefault().register(this) 中实现“onEventMainThread(DatabaseSQLite db); 我错过了什么吗?
    • 无需为传递对象(粘性对象)进行注册。它们允许在事件总线上“留下”对象并在稍后的代码中检索它们。您所指的另一部分非常适合“实时”事件,例如,如果您希望在单击片段时在活动中触发事件,或者相反。然后你需要注册来监听这些事件。
    • 所以我也不需要onEvent?问题是我收到此错误:com.example.RoadTrip D/Event:没有为事件类数据库注册的订阅者。DatabaseSQLite
    • 如果您忽略错误会发生什么(我假设它不会使应用程序崩溃)您是否能够在 IntentService 中捕获对象?
    • 由于某种原因,我的应用程序现在不想触发我的 IntentService,可能是 GPS 信号不佳或 LocationClient 存在问题。但是,您可能是对的,它无论如何都应该工作,是的,它没有崩溃。感谢您的帮助:) 一旦我让 LocationClient 触发我的 Intent 将立即接受您的回答:p
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-30
    • 1970-01-01
    • 1970-01-01
    • 2020-05-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多