Create ForegroundService
This commit is contained in:
		@ -13,6 +13,10 @@
 | 
			
		||||
        android:roundIcon="@mipmap/ic_launcher_round"
 | 
			
		||||
        android:supportsRtl="true"
 | 
			
		||||
        android:theme="@style/AppTheme">
 | 
			
		||||
        <service
 | 
			
		||||
            android:name=".TransferService"
 | 
			
		||||
            android:enabled="true"
 | 
			
		||||
            android:exported="true"></service>
 | 
			
		||||
 | 
			
		||||
        <provider
 | 
			
		||||
            android:name="androidx.core.content.FileProvider"
 | 
			
		||||
@ -26,31 +30,30 @@
 | 
			
		||||
 | 
			
		||||
        <activity
 | 
			
		||||
            android:name=".SettingsActivity"
 | 
			
		||||
            android:label="@string/title_activity_settings">
 | 
			
		||||
        </activity>
 | 
			
		||||
 | 
			
		||||
            android:label="@string/title_activity_settings"></activity>
 | 
			
		||||
        <activity
 | 
			
		||||
            android:name=".MainActivity"
 | 
			
		||||
            android:label="@string/app_name"
 | 
			
		||||
            android:theme="@style/AppTheme.NoActionBar">
 | 
			
		||||
 | 
			
		||||
            <intent-filter>
 | 
			
		||||
                <action android:name="android.intent.action.MAIN" />
 | 
			
		||||
 | 
			
		||||
                <category android:name="android.intent.category.LAUNCHER" />
 | 
			
		||||
            </intent-filter>
 | 
			
		||||
 | 
			
		||||
            <intent-filter>
 | 
			
		||||
                <action android:name="android.intent.action.SEND" />
 | 
			
		||||
 | 
			
		||||
                <category android:name="android.intent.category.DEFAULT" />
 | 
			
		||||
 | 
			
		||||
                <data android:mimeType="*/*" />
 | 
			
		||||
            </intent-filter>
 | 
			
		||||
 | 
			
		||||
            <intent-filter>
 | 
			
		||||
                <action android:name="android.intent.action.SEND_MULTIPLE" />
 | 
			
		||||
 | 
			
		||||
                <category android:name="android.intent.category.DEFAULT" />
 | 
			
		||||
 | 
			
		||||
                <data android:mimeType="*/*" />
 | 
			
		||||
            </intent-filter>
 | 
			
		||||
 | 
			
		||||
        </activity>
 | 
			
		||||
    </application>
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@ -1,5 +1,6 @@
 | 
			
		||||
package com.localtransfer;
 | 
			
		||||
 | 
			
		||||
import android.app.Notification;
 | 
			
		||||
import android.app.PendingIntent;
 | 
			
		||||
import android.content.Intent;
 | 
			
		||||
import android.view.LayoutInflater;
 | 
			
		||||
@ -12,6 +13,7 @@ import android.widget.TextView;
 | 
			
		||||
 | 
			
		||||
import androidx.core.app.NotificationCompat;
 | 
			
		||||
import androidx.core.app.NotificationManagerCompat;
 | 
			
		||||
import androidx.core.content.ContextCompat;
 | 
			
		||||
 | 
			
		||||
import java.util.ArrayList;
 | 
			
		||||
import java.util.ConcurrentModificationException;
 | 
			
		||||
@ -25,17 +27,24 @@ public class Progress {
 | 
			
		||||
    public static final int DOWNLOAD = 1001;
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    private static NotificationCompat.Builder bl = new NotificationCompat.Builder(Transfer.activity, null);
 | 
			
		||||
    private static Notification.Builder bl = new Notification.Builder(Transfer.activity);
 | 
			
		||||
    private static NotificationManagerCompat notifiManager = NotificationManagerCompat.from(Transfer.activity);
 | 
			
		||||
    private static final LayoutInflater inflater = (LayoutInflater) Transfer.activity.getSystemService(Transfer.activity.LAYOUT_INFLATER_SERVICE);
 | 
			
		||||
 | 
			
		||||
    private static Intent notificationIntent = new Intent(Transfer.activity, MainActivity.class);
 | 
			
		||||
    private static PendingIntent pendingIntent = PendingIntent.getActivity(Transfer.activity,0, notificationIntent, 0);
 | 
			
		||||
 | 
			
		||||
    private static List<Progress> instances = new ArrayList<>();
 | 
			
		||||
 | 
			
		||||
    private Integer id;
 | 
			
		||||
    private String name;
 | 
			
		||||
 | 
			
		||||
    private long size;
 | 
			
		||||
    private String HumanSize;
 | 
			
		||||
 | 
			
		||||
    public long loaded;
 | 
			
		||||
    private String HumanLoaded;
 | 
			
		||||
 | 
			
		||||
    public long percent;
 | 
			
		||||
 | 
			
		||||
    public static View root;
 | 
			
		||||
@ -47,7 +56,7 @@ public class Progress {
 | 
			
		||||
    private String info;
 | 
			
		||||
 | 
			
		||||
    public Button button;
 | 
			
		||||
    private boolean run = false;
 | 
			
		||||
    private boolean run;
 | 
			
		||||
 | 
			
		||||
    public Progress(String name, long size, int type) {
 | 
			
		||||
 | 
			
		||||
@ -57,6 +66,7 @@ public class Progress {
 | 
			
		||||
 | 
			
		||||
        this.name = name;
 | 
			
		||||
        this.size = size;
 | 
			
		||||
        this.HumanSize = Transfer.humanReadableByteCountBin(size);
 | 
			
		||||
 | 
			
		||||
        id = View.generateViewId();
 | 
			
		||||
 | 
			
		||||
@ -73,6 +83,10 @@ public class Progress {
 | 
			
		||||
                throw new IllegalStateException("Unexpected value: " + type);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        Intent serviceIntent = new Intent(Transfer.activity, TransferService.class);
 | 
			
		||||
        serviceIntent.putExtra("id", id);
 | 
			
		||||
        ContextCompat.startForegroundService(Transfer.activity, serviceIntent);
 | 
			
		||||
 | 
			
		||||
        timer.schedule(new TimerTask() {
 | 
			
		||||
            @Override
 | 
			
		||||
            public void run() {
 | 
			
		||||
@ -91,15 +105,18 @@ public class Progress {
 | 
			
		||||
 | 
			
		||||
        run = false;
 | 
			
		||||
 | 
			
		||||
        Intent serviceIntent = new Intent(Transfer.activity, TransferService.class);
 | 
			
		||||
        Transfer.activity.stopService(serviceIntent);
 | 
			
		||||
 | 
			
		||||
        if(! app_started) {
 | 
			
		||||
            bl.setSmallIcon(R.drawable.ic_upload_and_download_from_the_cloud)
 | 
			
		||||
                    .setContentTitle(name)
 | 
			
		||||
                    .setContentText(info)
 | 
			
		||||
                    .setProgress(0, 0, false)
 | 
			
		||||
                    .setAutoCancel(true)
 | 
			
		||||
                    .setOngoing(false);
 | 
			
		||||
                    .setContentIntent(pendingIntent);
 | 
			
		||||
            notifiManager.notify(id, bl.build());
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public void delete() {
 | 
			
		||||
@ -127,34 +144,32 @@ public class Progress {
 | 
			
		||||
        return false;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    public void showProgress(){
 | 
			
		||||
    private void showProgress(){
 | 
			
		||||
 | 
			
		||||
        try {
 | 
			
		||||
 | 
			
		||||
            String HumanLoaded = Transfer.humanReadableByteCountBin(loaded);
 | 
			
		||||
            String HumanSize = Transfer.humanReadableByteCountBin(size);
 | 
			
		||||
 | 
			
		||||
            if(app_started) {
 | 
			
		||||
                notifiManager.cancel(id);
 | 
			
		||||
            }
 | 
			
		||||
            else {
 | 
			
		||||
 | 
			
		||||
                Intent intent = new Intent(Transfer.activity, MainActivity.class);
 | 
			
		||||
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
 | 
			
		||||
                PendingIntent pendingIntent = PendingIntent.getActivity(Transfer.activity, 0, intent, 0);
 | 
			
		||||
            HumanLoaded = Transfer.humanReadableByteCountBin(loaded);
 | 
			
		||||
 | 
			
		||||
            bl.setSmallIcon(R.drawable.ic_upload_and_download_from_the_cloud)
 | 
			
		||||
                    .setContentTitle(name)
 | 
			
		||||
                    .setContentText(String.format("%d%% %s/%s", percent, HumanLoaded, HumanSize))
 | 
			
		||||
                    .setProgress(100, (int) percent, false)
 | 
			
		||||
                        .setContentIntent(pendingIntent)
 | 
			
		||||
                        .setAutoCancel(true)
 | 
			
		||||
                        .setOngoing(true);
 | 
			
		||||
                    .setContentIntent(pendingIntent);
 | 
			
		||||
            notifiManager.notify(id, bl.build());
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            if(app_started && fragment_on) {
 | 
			
		||||
                showProgressFragment();
 | 
			
		||||
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
        } catch (ConcurrentModificationException e) {
 | 
			
		||||
            e.printStackTrace();
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public void showProgressFragment(){
 | 
			
		||||
 | 
			
		||||
        LinearLayout groot = root.findViewById(R.id.groot);
 | 
			
		||||
 | 
			
		||||
        final View progress;
 | 
			
		||||
@ -178,11 +193,4 @@ public class Progress {
 | 
			
		||||
        });
 | 
			
		||||
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
        } catch (ConcurrentModificationException e) {
 | 
			
		||||
            e.printStackTrace();
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										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");
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@ -57,7 +57,7 @@ public class ProgressFragment extends Fragment {
 | 
			
		||||
        List instances = Progress.getInstances();
 | 
			
		||||
        for (Object obj: instances) {
 | 
			
		||||
            Progress p = (Progress) obj;
 | 
			
		||||
            p.showProgress();
 | 
			
		||||
            p.showProgressFragment();
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
		Reference in New Issue
	
	Block a user