嗨, friend 们,我有一个 IntentService,我想从 IntentService 中停止它 stopSelf() 但我不起作用,我尝试用 stopService(Intent) 从主要 Activity 中停止它,但我也不起作用。 谢谢 friend 们,顺便说一句,我在 list 中添加了这一行:
<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){
}
}
}
这是我的主要 Activity :
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();
}
}
}
}
最佳答案
您可以使用简单的服务来代替 Intent Service,如果需要,它更容易立即停止。 否则您可以使用此方法。 首先在 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
关于java - 为什么 stopService(Intent) 和 stopSelf() 方法不会停止我的独特服务?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57947521/