I try to make an Android Service.
So I use an Service extended Class.
public class DelayedToast extends IntentService {
private final static boolean Debug = true;
private final static String TAG = "ALT";
public DelayedToast() {
super("DelayedToast");
}
@Override
protected void onHandleIntent(Intent intent) {
final int delay = intent.getIntExtra("delay", -1);
if (Debug) Log.i(TAG, "DelayedToast:onHandleIntent delay: " + delay);
if (delay > 0) {
SystemClock.sleep(delay * 1000);
if (Debug) Log.i(TAG, "Wake up !! ");
// Intent broadcastIntent = new Intent();
Intent broadcastIntent = new Intent(getApplicationContext(), Receiver.class); // Need to be explicit for Broadcast : https://stackoverflow.com/questions/55610977/why-a-static-broadcastreceiver-not-working
broadcastIntent.setAction(getString(R.string.intent_action));
broadcastIntent.putExtra("delay", delay);
sendBroadcast(broadcastIntent);
if (Debug) Log.i(TAG, "sendBroadcast");
showToast("toast Service: " + delay);
}
}
// https://stackoverflow.com/a/34832674
protected void showToast(final String msg) {
Handler handler = new Handler(Looper.getMainLooper());
handler.post(new Runnable() {
@Override
public void run() {
if (Debug) Log.i(TAG, msg);
Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_LONG).show();
}
});
}
}
I have declared this on the Manifest.
<service
android:name=".DelayedToast"
android:enabled="true"
android:exported="true"
android:process=":DelayedToast" />
I launch service with a Button :
public void onClickStart(View v) {
EditText editText = findViewById(R.id.delay);
final String str_delay = editText.getText().toString();
if (Debug) Log.i(TAG, "MainActivity:onClickStart str: " + str_delay);
if (!str_delay.isEmpty()) {
final int delay = Integer.parseInt(str_delay);
if (Debug) Log.i(TAG, "Q1_MainActivity:onClickStart delay: " + delay);
if (delay > 0) {
Intent intent = new Intent(this, DelayedToast.class);
intent.putExtra("delay", delay);
startService(intent);
}
}
}
C:\Users\dark_vidor>adb shell "ps | grep tp08" // Here application isn't lauching
C:\Users\dark_vidor>adb shell "ps | grep tp08" // Application is running
u0_a246 20032 3012 2569888 132044 0 0 S test.tp08
C:\Users\dark_vidor>adb shell "ps | grep tp08" // I'm start "Delayed Toast" (service)
u0_a246 20032 3012 2573316 135404 0 0 S test.tp08
u0_a246 20090 3012 2324660 79876 0 0 S test.tp08:DelayedToast
C:\Users\dark_vidor>adb shell "ps | grep tp08" // I have receiver a Broacast Intet
u0_a246 20032 3012 2580964 137252 0 0 S test.tp08
u0_a246 20090 3012 2326168 96376 0 0 S test.tp08:DelayedToast
u0_a246 20127 3012 2325228 99696 0 0 S test.tp08:Receiver
C:\Users\dark_vidor>adb shell "ps | grep tp08" // Here I have quited the application, service is also killed
C:\Users\dark_vidor>
I'm followed some tutorials and I don't understand what I have forget and why my Service is killed after I quit my application while he should stay until I stop it I'm working with a Samsung A8, my application is in Java, SDK min 24
Have you any idea to fix this problem ?