1
0
mirror of https://github.com/termux/termux-app synced 2024-06-17 22:57:08 +00:00

Fix for boot and background service are not working on Android 10

This commit is contained in:
AntonSamokat 2024-03-03 23:59:46 +03:00
parent cff6cff609
commit dda26ccae2
4 changed files with 44 additions and 7 deletions

View File

@ -196,7 +196,8 @@
<service
android:name=".app.TermuxService"
android:exported="false" />
android:exported="true"
android:enabled="true" />
<service
android:name=".app.RunCommandService"

View File

@ -13,6 +13,7 @@ import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.IBinder;
import android.provider.Settings;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.Gravity;
@ -198,6 +199,20 @@ public final class TermuxActivity extends AppCompatActivity implements ServiceCo
@Override
public void onCreate(Bundle savedInstanceState) {
Logger.logDebug(LOG_TAG, "onCreate");
// For details see:
// https://stackoverflow.com/a/19856267/1455694
// https://stackoverflow.com/a/63250729/1455694
// https://www.reddit.com/r/tasker/comments/d7whyj/android_10_and_auto_starting_apps/
// https://stackoverflow.com/q/64642362/1455694
Context context = getApplicationContext();
// Q is Android 10
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
if (!Settings.canDrawOverlays(context)) {
startActivity(new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION));
}
}
mIsOnResumeAfterOnCreate = true;
if (savedInstanceState != null)
@ -257,9 +272,17 @@ public final class TermuxActivity extends AppCompatActivity implements ServiceCo
FileReceiverActivity.updateFileReceiverActivityComponentsState(this);
try {
Intent serviceIntent;
// Start the {@link TermuxService} and make it run regardless of who is bound to it
Intent serviceIntent = new Intent(this, TermuxService.class);
startService(serviceIntent);
// O is Oreo, Android 8
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
serviceIntent = new Intent(context, TermuxService.class);
context.startForegroundService(serviceIntent);
}
else {
serviceIntent = new Intent(this, TermuxService.class);
startService(serviceIntent);
}
// Attempt to bind to the service, this will call the {@link #onServiceConnected(ComponentName, IBinder)}
// callback if it succeeds.

View File

@ -159,10 +159,9 @@ public final class TermuxService extends Service implements AppShell.AppShellCli
break;
}
}
// If this service really do get killed, there is no point restarting it automatically - let the user do on next
// start of {@link Term):
return Service.START_NOT_STICKY;
// If Service.START_NOT_STICKY is used this service will be killed when device not connected to charger
// (probably only for Xiaomi Redmi 7A with Android 10)
return Service.START_STICKY;
}
@Override

View File

@ -5,10 +5,12 @@ import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.Uri;
import android.os.Build;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.termux.app.TermuxService;
import com.termux.shared.data.IntentUtils;
import com.termux.shared.logger.Logger;
import com.termux.shared.termux.TermuxUtils;
@ -51,8 +53,20 @@ public class SystemEventReceiver extends BroadcastReceiver {
}
}
private void runTermuxService(@NonNull Context context) {
Intent serviceIntent = new Intent(context, TermuxService.class);
// O is Oreo, Android 8
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
context.startForegroundService(serviceIntent);
}
else {
context.startService(serviceIntent);
}
}
public synchronized void onActionBootCompleted(@NonNull Context context, @NonNull Intent intent) {
TermuxShellManager.onActionBootCompleted(context, intent);
runTermuxService(context);
}
public synchronized void onActionPackageUpdated(@NonNull Context context, @NonNull Intent intent) {