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