0

Hi friends i have an IntentService and i want to stop it from the IntentService whit stopSelf() but i does not work, i have tried stop it from the main activity with stopService(Intent) but i does not work as well. thanks you friends, by the way i have added in manifest this line:

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

here is my IntentService code:

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){

    }

   }
  }

and here is my MainActivity:

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();
        }
    }
  }
}
  • 2
    Please explain in detail what "does not work" means. What are your specific symptoms? – CommonsWare Sep 15 '19 at 19:26
  • hi @CommonsWare thank you for anwser my question, what i want is STOP THE INTENTSERVICE, and i tried with stopService(Intent) and stopSelf() but the service does not stop with any methods stopSelf() and stopService. look my code that i wrote i want stop it after 10 seconds. thank you. – David Garcia Sep 15 '19 at 19:34
  • 2
    I do not know how you are determining that "the service does not stop". "i want stop it after 10 seconds" -- your code will stop it after 100 seconds. You are looping 10 times and sleeping 10 seconds each time. – CommonsWare Sep 15 '19 at 19:40
  • @CommonsWare ahmmm so thas what i want stop the service or kill it any one , after 50 seconds for example. can i do that? – David Garcia Sep 15 '19 at 19:46

1 Answers1

0

You can use a simple service instead of Intent Service which is easier to stop immediately if you need. Other wise you can use this method. First declare a process for your service in your Manifest.xml like this. This will make your service run on a separate thread.

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

Then when you want to kill the service call this method:

 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;
           }
       }

   }

This will kill of your Intent service immediately.

For more info check out this Answer. https://stackoverflow.com/a/23444116/7392868

Muhammad Ali
  • 683
  • 4
  • 9