Skip to content

Commit

Permalink
Use startForegroundService() on Android O
Browse files Browse the repository at this point in the history
  • Loading branch information
fornwall committed Nov 1, 2017
1 parent 1d062fa commit 26be3c0
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ protected void onResume() {
toast.show();
}

startService(executeIntent);
TermuxWidgetProvider.startTermuxService(this, executeIntent);
finish();
}

Expand Down
13 changes: 12 additions & 1 deletion app/src/main/java/com/termux/widget/TermuxWidgetProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.view.Gravity;
import android.widget.RemoteViews;
import android.widget.Toast;
Expand Down Expand Up @@ -99,7 +100,7 @@ public void onReceive(Context context, Intent intent) {
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
}
context.startService(executeIntent);
startTermuxService(context, executeIntent);
break;
case REFRESH_WIDGET_ACTION:
int appWidgetId = intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID);
Expand All @@ -112,6 +113,16 @@ public void onReceive(Context context, Intent intent) {
}
}

static void startTermuxService(Context context, Intent executeIntent) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
// https://developer.android.com/about/versions/oreo/background.html
context.startForegroundService(executeIntent);
} else {
context.startService(executeIntent);
}

}

/** Ensure readable and executable file if user forgot to do so. */
static void ensureFileReadableAndExecutable(File file) {
if (!file.canRead()) file.setReadable(true);
Expand Down

0 comments on commit 26be3c0

Please sign in to comment.