Create ForegroundService

This commit is contained in:
lionel
2021-03-12 10:32:47 +01:00
parent 54dce31f53
commit 493e836f56
4 changed files with 109 additions and 55 deletions

View File

@ -0,0 +1,43 @@
package com.localtransfer;
import android.app.Notification;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import androidx.core.app.NotificationCompat;
public class TransferService extends Service {
@Override
public void onCreate() {
super.onCreate();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
int id = intent.getIntExtra("id", 0);
Intent notificationIntent = new Intent(this, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this,
0, notificationIntent, 0);
Notification notification = new NotificationCompat.Builder(this, "CHANNEL_ID")
.setContentIntent(pendingIntent)
.build();
startForeground(id, notification);
//do heavy work on a background thread
//stopSelf();
return START_NOT_STICKY;
}
@Override
public IBinder onBind(Intent intent) {
// TODO: Return the communication channel to the service.
throw new UnsupportedOperationException("Not yet implemented");
}
}