Create ForegroundService
This commit is contained in:
		
							
								
								
									
										43
									
								
								app/src/main/java/com/localtransfer/TransferService.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										43
									
								
								app/src/main/java/com/localtransfer/TransferService.java
									
									
									
									
									
										Normal 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");
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user