从一个活动传递对象IntentService [英] Pass objects to IntentService from activity

查看:300
本文介绍了从一个活动传递对象IntentService的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

要节省电池在我的应用我决定用新的融合的位置。不过,我需要一些参数传递到接收GPS更新服务。它的下面做的方式将工作( putExtras(...)),但我需要做大量的类序列化/可解析,这将是一个痛苦。

我寻觅了一圈,发现使用粘结剂一些其他的方式,但无法弄清楚如何得到它的工作。使用活页夹的唯一方法或有另一种?

如果有不清楚的地方,请告知。
谢谢你。

 公共类LocationService扩展IntentService {
    ...
    公共LocationService(StartActivity startActivity,DatabaseSQLite分贝,HomeFragment homeFragment){
        超(融合位置服务);
        ...
    }   @覆盖
   公众诠释onStartCommand(意向意图,诠释标志诠释startId){
   DB =(DatabaseSQLite)intent.getExtras()获得(DatabaseSQLite);   ...
   返回START_REDELIVER_INTENT;    }
}

这是它是如何在我的活动中使用:

  @覆盖
    公共无效onConnected(束束){
        mIntentService =新意图(这一点,LocationService.class);
        mIntentService.putExtra(DatabaseSQLite数据库);
        ...
        mPendingIntent = PendingIntent.getService(在此,1,mIntentService,0);}


解决方案

您应该检查 https://开头github上。 COM / greenrobot / EventBus

例子可以在这里找到: http://awalkingcity.com /博客/ 2013/2月26日/生产-Android的eventbus /

基本上可以让你做的东西,如:

  @覆盖
    公共无效onConnected(束束){
        mIntentService =新意图(这一点,LocationService.class);        //可以是任何对象
        。EventBus.getDefault()postSticky(数据库);
        ...
        mPendingIntent = PendingIntent.getService(在此,1,mIntentService,0);}

每当你需要的对象

 公共类LocationService扩展IntentService {
    ...
    公共LocationService(StartActivity startActivity,DatabaseSQLite分贝,HomeFragment homeFragment){
        超(融合位置服务);
        ...
    }   @覆盖
   公众诠释onStartCommand(意向意图,诠释标志诠释startId){   //也可以在广播接收器等。
   DB = EventBus.getDefault()getStickyEvent(DatabaseSQLite.class)。   ...
   返回START_REDELIVER_INTENT;    }
}

它不仅是简单的,这个环节也表明,它优于其他的方法:的 http://www.stevenmarkford.com/passing-objects-between-android-activities/

To save battery in my app I decided to use the "new" Fused locations. However I need to pass some parameters into the service which receives GPS updates. The way It's done below would work (putExtras(...)), but I would need to make a lot of classes Serializable/Parseable, which would be a pain.

I have searched around and found some other way using Binder, but can't figure out how to get it to work. Is using Binder the only way or is there another?

If anything is unclear, please tell. Thank you.

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;

    }
}

And this is how It's used in my activity:

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

}

解决方案

You should check out https://github.com/greenrobot/EventBus

Examples can be found here: http://awalkingcity.com/blog/2013/02/26/productive-android-eventbus/

Basically lets you do stuff like:

@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);

}

And whenever you need the object

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;

    }
}

Not only is it simpler, this link also shows that it outperforms other methods: http://www.stevenmarkford.com/passing-objects-between-android-activities/

这篇关于从一个活动传递对象IntentService的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆