Android: establish a service with foreground notification

This commit is contained in:
Ender's Games 2018-07-20 15:17:38 -04:00
parent f451c5e26f
commit 5e9ed25c01
2 changed files with 53 additions and 0 deletions

View File

@ -121,6 +121,12 @@
android:configChanges="orientation|navigation|screenSize|screenLayout|uiMode|keyboard|keyboardHidden"
android:screenOrientation="sensorLandscape"
android:theme="@android:style/Theme.NoTitleBar.Fullscreen" />
<service
android:name=".emu.EmuService"
android:configChanges="orientation"
android:enabled="true"
android:exported="true" />
</application>
</manifest>

View File

@ -0,0 +1,47 @@
package com.reicast.emulator.emu;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.support.v4.app.NotificationCompat;
import com.reicast.emulator.R;
public class EmuService extends Service {
private final static int FOREGROUND_ID = 999;
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
if (intent != null && intent.getAction() != null
&& intent.getAction().equals("com.reicast.emulator.KYS")) {
stopSelf();
} else {
Intent intentService = new Intent(this, EmuService.class);
intentService.setAction("com.reicast.emulator.KYS");
PendingIntent pendingIntent = PendingIntent.getService(this,
(int) System.currentTimeMillis(), intentService,
PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setTicker("Emulator ticker message")
.setContentTitle("Emulator notification title")
.setContentText("Emulator notification content")
.setContentIntent(pendingIntent);
startForeground(FOREGROUND_ID, builder.build());
return START_STICKY_COMPATIBILITY;
}
return START_NOT_STICKY;
}
@Override
public void onDestroy() {
stopForeground(true);
}
}