服务和intentservice之间的通信

时间:2016-06-08 18:44:06

标签: android broadcastreceiver android-service intentservice

我是Android开发的新手,我尝试使用service和intentservice进行一些练习。

这是我的服务类:

public class MyBaseService extends Service {

private double[] returnData;

public MyBaseService() {
}

@Override
public void onCreate() {
    returnData = new double[//dataSise];
}

/** The service is starting, due to a call to startService() */
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    try {
        for (Map.Entry<Integer, Double[]> mapEntry : dataMap.entrySet()) {

            doXYZ(mapEntry.getValue());
            Arrays.sort(returnData);
        }
    } catch (IOException e) {
        e.printStackTrace();
    }

    Intent intents = new Intent();
    intents.setAction(ACTION_SEND_TO_ACTIVITY);
    sendBroadcast(intents);
    return START_STICKY;
}

/** A client is binding to the service with bindService() */
@Override
public IBinder onBind(Intent arg0) {
    return mBinder;
}

public class MyBinder extends Binder {
    public MyBaseService getService() {
        return MyBaseService.this;
    }
}

/** Called when a client is binding to the service with bindService()*/
@Override
public void onRebind(Intent intent) {

}

/** Called when The service is no longer used and is being destroyed */
@Override
public void onDestroy() {
    super.onDestroy();
}


private void doXYZ(double[] data) {
    int gallerySize = galleryFiles.length;

    for (int i=0; i<data.length; ++i) {
        Intent cfIntent = new Intent(this, MyIntentService.class);
        compareFeatureIntent.putExtra(MyIntentService.COMPARING_INDEX, i);
        startService(cfIntent);
    }

}

BroadcastReceiver mReceiver;

// use this as an inner class like here or as a top-level class
public class MyReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        int index = intent.getIntExtra(MyIntentService.COMPARING_INDEX, 0);
        double scores = intent.getDoubleArrayExtra(MyIntentService.COMPARING_SCORE);
        data[index] = scores[0];
    }

    // constructor
    public MyReceiver(){
    }
}

}

这是一个意向服务类:

public class MyIntentService extends IntentService {
protected static final String ACTION_COMPARE_FEATURES = "CompareFeatures";
protected static final String COMPARING_SCORE = "Score";
protected static final String COMPARING_INDEX = "Index";

public MyIntentService() {
    super("MyIntentService");
}

@Override
protected void onHandleIntent(Intent intent) {

    int index = (int)intent.getLongExtra(COMPARING_INDEX, 0);

    // This is long operation
    double[] scores = getScores(index);

    Intent intents = new Intent();
    intents.setAction(ACTION_COMPARE_FEATURES);
    intent.putExtra(COMPARING_SCORE, scores);
    intent.putExtra(COMPARING_INDEX, index);
    sendBroadcast(intents);
}

}

场景是我想在main活动中启动MyBaseService类。在MyBaseService中,我需要进行长时间运行,需要多次迭代该操作。因此,我将这个长操作放在MyIntentService中,并在循环中启动MyIntentService。

MyIntentService将生成一些数据,我想在MyBaseService类中获取该数据以进行一些进一步的操作。

我面临的问题是MyBaseService和MyIntentService之间的通信。因为MyBaseService会多次启动MyIntentSerice,我的初始解决方案是从MyIntentService发送sendBroadcast(),并在MyBaseService中注册接收器。

所以,我的问题是:

  1. 我的MyBaseService MyIntentService设计是否有效?如果没有,我该如何归档我想要的结果?

  2. 如果sendBroadcast()是正确的方向,我应该如何在MyBaseService中注册?

1 个答案:

答案 0 :(得分:1)

您的架构很好。有几种方法可以做到这一点,但这种方法还可以。

您可以在BroadcastReceiver中注册MyBaseSerice.onStartCommand()并在MyBaseService.onDestroy()中取消注册。

您需要确定如何关闭MyBaseServiceActivity可以执行此操作,或者MyBaseService需要跟踪IntentService中等待的回复数量,一旦获得最后一个回复,它就可以自行关闭请致电stopSelf()

相关问题