我想在一个单独的过程中启动服务,但是什么也没发生(当使用相同的过程时,它起作用了,烤面包了。)
Android清单:
<service
android:name=".DelayedToastIntentService"
android:enabled="true"
android:exported="false"
android:process=":delayed_toast">
</service>
MainActivity:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
intentService = new Intent(MainActivity.this, DelayedToastIntentService.class);
r1 = new Receiver("r1");
IntentFilter filter = new IntentFilter();
filter.addAction("toast"); LocalBroadcastManager.getInstance(this).registerReceiver(r1, filter);
editText = (EditText) findViewById(R.id.editText);
}
private class Receiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(getApplicationContext(), "Délai écoulé : " + String.valueOf(intent.getIntExtra("period",0)),
Toast.LENGTH_LONG).show();
}
}
public void onStartDelayedToast(View v){ intentService.putExtra("period",Integer.valueOf(String.valueOf(editText.getText())));
intentService.setAction("toast");
startService(intentService);
}
DelayedToastIntentService:
public class DelayedToastIntentService extends IntentService {
@Override
protected void onHandleIntent(@Nullable Intent intent) {
final int period =intent.getIntExtra("period",10);
SystemClock.sleep(period*1000); LocalBroadcastManager.getInstance(getApplicationContext()).sendBroadcast(intent);
}
}