diff --git a/app/src/main/java/com/termux/app/TermuxActivity.java b/app/src/main/java/com/termux/app/TermuxActivity.java
index b802d824..9a245324 100644
--- a/app/src/main/java/com/termux/app/TermuxActivity.java
+++ b/app/src/main/java/com/termux/app/TermuxActivity.java
@@ -248,14 +248,24 @@ public final class TermuxActivity extends AppCompatActivity implements ServiceCo
registerForContextMenu(mTerminalView);
- // Start the {@link TermuxService} and make it run regardless of who is bound to it
- Intent serviceIntent = new Intent(this, TermuxService.class);
- startService(serviceIntent);
+ try {
+ // Start the {@link TermuxService} and make it run regardless of who is bound to it
+ Intent 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.
- if (!bindService(serviceIntent, this, 0))
- throw new RuntimeException("bindService() failed");
+ // Attempt to bind to the service, this will call the {@link #onServiceConnected(ComponentName, IBinder)}
+ // callback if it succeeds.
+ if (!bindService(serviceIntent, this, 0))
+ throw new RuntimeException("bindService() failed");
+ } catch (Exception e) {
+ Logger.logStackTraceWithMessage(LOG_TAG,"TermuxActivity failed to start TermuxService", e);
+ Logger.showToast(this,
+ getString(e.getMessage() != null && e.getMessage().contains("app is in background") ?
+ R.string.error_termux_service_start_failed_bg : R.string.error_termux_service_start_failed_general),
+ true);
+ mIsInvalidState = true;
+ return;
+ }
// Send the {@link TermuxConstants#BROADCAST_TERMUX_OPENED} broadcast to notify apps that Termux
// app has been opened.
diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml
index d5335bf0..5f23f4bb 100644
--- a/app/src/main/res/values/strings.xml
+++ b/app/src/main/res/values/strings.xml
@@ -118,6 +118,8 @@
%1$s requires `allow-external-apps`
property to be set to `true` in `%2$s` file.
+ Failed to start TermuxService. Check logcat for exception message.
+ Failed to start TermuxService while app is in background due to android bg restrictions.