如何知道服务何时结束

时间:2015-06-09 09:13:21

标签: android android-service android-broadcast

我想知道服务何时结束,所以我使用BroadcastReceiver。 我的服务名称为CheckNuevosAvisosIntentServices,我在main.java(onCreate方法)中将其启动为:

        Intent msgIntent = new Intent(Main.this, CheckNuevosAvisosIntentService.class);
        msgIntent.putExtra("iteraciones", 1);
        startService(msgIntent);

我的应用程序标签内的清单。

<!-- Services -->
<service android:name="com.kirolm.instalacionesdep.services.CheckNuevosAvisosIntentService" />

在另一个片段(HomeFragment)中,我使用isMyServiceRunning方法和BroadcastReceiver类:

private boolean isMyServiceRunning() {
    ActivityManager manager = (ActivityManager) getActivity().getSystemService(Context.ACTIVITY_SERVICE);
    for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
        if (CheckNuevosAvisosIntentService.class.getName().equals(service.service.getClassName())) {
            return true;
        }
    }
    return false;
}

我的广播代码是这样的:

public class ProgressReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        if(intent.getAction().equals(CheckNuevosAvisosIntentService.ACTION_PROGRESO)) {

            Log.e("Testing", "The service is running...");


        }
        else if(intent.getAction().equals(CheckNuevosAvisosIntentService.ACTION_FIN) && isAdded()) {
            Log.e("Testing", "The service has been ended");
        }
    }
}

并在creathe方法(HomeFragment)中实现了这个:

            if(!isMyServiceRunning()){
                Log.e("Testing", "HomeFragment: The service is running");

            }else{
                Log.e("Testing", "HomeFragment: The servie stop");
            }

CheckNuevosAvisosIntentServices代码:

public class CheckNuevosAvisosIntentService extends IntentService{

    public static final String ACTION_PROGRESO = "com.kirolm.instalacionesdep.services.action.PROGRESO";
    public static final String ACTION_FIN =  "com.kirolm.instalacionesdep.services.action.FIN";


    public CheckNuevosAvisosIntentService() {
        super("CheckNuevosAvisosIntentService");
        // TODO Auto-generated constructor stub

    }

    @Override
    protected void onHandleIntent(Intent intent) {
        // TODO Auto-generated method stub

        int iter = intent.getIntExtra("iteraciones", 0);

        for(int i=1; i<=iter; i++) {

            Intent bcIntent = new Intent();
            bcIntent.setAction(ACTION_PROGRESO);
            bcIntent.putExtra("progreso", i*10);
            sendBroadcast(bcIntent);
        }
        buscaNuevasNoticasRss();
        Intent bcIntent = new Intent();
        bcIntent.setAction(ACTION_FIN);
        sendBroadcast(bcIntent);

    }



    private void buscaNuevasNoticasRss() {
        // TODO Auto-generated method stub
        //This method checks.  When this method finish I want finish my services.
    }
}

buscaNuevasNoticiasRss完成时,我想完成我的服务。

我在服务运行时收到但服务停止时我没有收到。

编辑:HomeFragment(onCreat方法)

@Override
public void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);

                ProgressReceiver = new BroadcastReceiver() {

                    @Override
                    public void onReceive(Context context, Intent intent) {
                        // TODO Auto-generated method stub
                        Log.e("Testing", "HomeFragment. BoradcastReceiver.  Dentro de onReceive");

                        if(intent.getAction().equals(CheckNuevosAvisosIntentService.ACTION_PROGRESO)) {

                            Log.e("Testing", "HomeFragment. BoradcastReceiver. The service is running...");


                        }
                        else if(intent.getAction().equals(CheckNuevosAvisosIntentService.ACTION_FIN) && isAdded()) {
                            Log.e("Testing", "HomeFragment. BoradcastReceiver. The service has been ended");


                        }

                    }
                };

}

1 个答案:

答案 0 :(得分:0)

覆盖onDestroy()的{​​{1}}方法,并向接收方发送广播,说服务已被破坏。

IntentService

并创建public class CheckNuevosAvisosIntentService extends IntentService{ //Your code ....... ....... @Override public void onDestroy() { super.onDestroy(); sendBroadcast(new Intent(CheckNuevosAvisosIntentService.ACTION_FIN)); } } 作为BroadcastReceiver的内部类。

HomeFragment