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