首页 > 解决方案 > 为什么 stopService(Intent) 和 stopSelf() 方法不会停止我的独特服务?

问题描述

嗨朋友们,我有一个 IntentService,我想从 IntentService whit stopSelf() 中停止它,但我不起作用,我尝试使用 stopService(Intent) 从主要活动中停止它,但我也无法正常工作。谢谢你的朋友,顺便说一句,我在清单中添加了这一行:

<service android:name=".MiIntentService"></service>

这是我的 IntentService 代码:

public class MiIntentService extends IntentService {


public static  final String ACTION_PROGRESO = "com.example.pruebafirebase.PROGRESO";
public static final String ACTION_FIN = "com.example.pruebafirebase.FIN";


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


@Override
protected void onHandleIntent(@Nullable Intent intent) {
        ////aqui va la tarea larga

        int iter = intent.getIntExtra("iteraciones",0);
        for(int i = 0 ; i <= iter; i++){
            tareaLarga();

            //comunicacmos afuera lo de tarea larga

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

        Intent finishIntent = new Intent();
        finishIntent.setAction(ACTION_FIN);
        sendBroadcast(finishIntent);

}

private void tareaLarga(){

    try {
        Thread.sleep(10000);
        this.stopSelf();
    }catch (Exception e){

    }

   }
  }

这是我的主要活动:

public class MainActivity extends AppCompatActivity {


Intent msgIntent;



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);



    msgIntent  = new Intent(MainActivity.this, MiIntentService.class);
    msgIntent.putExtra("iteraciones", 10);
    startService(msgIntent);




    //registrando nuestro broadcast a la aplicacion y filtrando solo para las acciones especificadas

    IntentFilter filtroBroadcast = new IntentFilter();
    filtroBroadcast.addAction(MiIntentService.ACTION_FIN);
    filtroBroadcast.addAction(MiIntentService.ACTION_PROGRESO);

    ProgressReceiver progressReceiver = new ProgressReceiver();
    registerReceiver(progressReceiver,filtroBroadcast);

}


public class ProgressReceiver extends BroadcastReceiver{


    @Override
    public void onReceive(Context context, Intent intent) {
        if(intent.getAction().equals(MiIntentService.ACTION_PROGRESO)){
            Toast.makeText(getApplicationContext(),String.valueOf(intent.getIntExtra("progreso",0)),Toast.LENGTH_LONG).show();

        }

        if(intent.getAction().equals(MiIntentService.ACTION_FIN)){
            Toast.makeText(getApplicationContext(),"Se termino de realizar las 10 tareas que se tenia",Toast.LENGTH_LONG).show();
        }
    }
  }
}

标签: javaandroidandroid-studio

解决方案


您可以使用简单的服务而不是 Intent 服务,如果需要,它更容易立即停止。否则,您可以使用此方法。首先像这样在 Manifest.xml 中为您的服务声明一个流程。这将使您的服务在单独的线程上运行。

<service 
android:name=".MiIntentService"
android:exported="false"
android:process=":myservice">
</service>

然后,当您想终止服务时,请调用此方法:

 public void killService(){


       ActivityManager am = (ActivityManager) getLMvdActivity().getSystemService(ACTIVITY_SERVICE);
       List<ActivityManager.RunningAppProcessInfo> runningAppProcesses = am.getRunningAppProcesses();

       Iterator<ActivityManager.RunningAppProcessInfo> iter = runningAppProcesses.iterator();

       while(iter.hasNext()){
           ActivityManager.RunningAppProcessInfo next = iter.next();

           String pricessName = getLMvdActivity().getPackageName() + ":myservice"; //i.e the process you named for your service

           if(next.processName.equals(pricessName)){
               Process.killProcess(next.pid);
               break;
           }
       }

   }

这将立即终止您的 Intent 服务。

有关更多信息,请查看此答案。https://stackoverflow.com/a/23444116/7392868


推荐阅读