Hi Im new in coding and I need some help. Im trying to create a foreground download service with a progress notification. And I made a foreground service after run the app it worked really good, then I added download task; after run the app it worked again but with little freezing, then I added progress notification and it still working but with so much freezing.
I used this for my foregroundService.class file:
package com.seon.store;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Build;
import android.os.IBinder;
import com.downloader.*;
import androidx.annotation.Nullable;
import androidx.core.app.NotificationCompat;
import android.widget.Toast;
public class foregroundService extends Service {
public static Context context;
public int downloadId;
public static final String CHANNEL_ID = "nubChannel";
public static String name = "";
public static String input = "";
@Override
public void onCreate() {
context = this;
PRDownloader.initialize(getApplicationContext());
super.onCreate();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
//Game name to string
input = intent.getStringExtra("inputExtra");
if (intent.getAction().equals(Constants.ACTION.STARTFOREGROUND_ACTION)) {
name = "HUE_GAME.apk";
String url = "https://www.googleapis.com/drive/v3/files/1-H27u87TNVlafd1xJXt-JcJugo5I5e6s?alt=media&key=AIzaSyDPM9n6LpZ-D1uf0b4w1pChmiw6uM4_avU";
String path = FileUtil.getExternalStorageDir().concat("/seon");
if (FileUtil.isExistFile(FileUtil.getExternalStorageDir().concat("/seon/".concat(name.concat(".temp"))))) {
_downloadfile(url, path, name);
}
else {
//Start Download
_downloadfile(url, path, name);
}
} else if (intent.getAction().equals(Constants.ACTION.PREV_ACTION)) {
//Cancel service and download
PRDownloader.cancel(downloadId);
stopForeground(true);
stopSelf();
}
return START_NOT_STICKY;
}
@Override
public void onDestroy() {
super.onDestroy();
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
public void _downloadfile(final String _Url, final String _directory, final String _filename) {
downloadId = PRDownloader.download(_Url, _directory, _filename)
.build()
.setOnStartOrResumeListener(new OnStartOrResumeListener() {
@Override
public void onStartOrResume() {
Toast.makeText(getApplicationContext(),"Download started",Toast.LENGTH_SHORT).show();
}
})
.setOnPauseListener(new OnPauseListener() {
@Override
public void onPause() {
Toast.makeText(getApplicationContext(),"Download paused",Toast.LENGTH_SHORT).show();
}
}).setOnCancelListener(new OnCancelListener() {
@Override
public void onCancel() {
Toast.makeText(getApplicationContext(),"Download cancelled",Toast.LENGTH_SHORT).show();
}}).setOnProgressListener(new OnProgressListener() {
@Override
public void onProgress(Progress progress) {
_notification("Downloading", name, (int)progress.currentBytes, (int)progress.totalBytes);
}
})
.start(new OnDownloadListener() {
@Override
public void onDownloadComplete() {
stopForeground(true);
stopSelf();
}
@Override
public void onError(com.downloader.Error error) {
Toast.makeText(getApplicationContext(),"Unable to download file",Toast.LENGTH_SHORT).show();
}
});
} public void _notification(final String _title, final String _subtitle, final double _progressValue, final double _progressMax) {
Intent previousIntent = new Intent(this, foregroundService.class);
previousIntent.setAction(Constants.ACTION.PREV_ACTION);
PendingIntent ppreviousIntent = PendingIntent.getService(this, 0, previousIntent, 0);
createNotificationChannel();
Intent notificationIntent = new Intent(this, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
Notification notification = new NotificationCompat.Builder(this, CHANNEL_ID)
.setContentTitle("Downloading")
.setContentText(input)
.setSmallIcon(R.drawable.ic_android)
.addAction(android.R.drawable.ic_media_previous, "Cancel", ppreviousIntent)
.setContentIntent(pendingIntent)
.setProgress((int)_progressMax,(int)_progressValue, false)
.build();
startForeground(69693, notification);
}
private void createNotificationChannel() {
// Create the NotificationChannel
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
CharSequence name = CHANNEL_ID;
String description = "description";
int importance = NotificationManager.IMPORTANCE_DEFAULT;
NotificationChannel channel = new NotificationChannel(CHANNEL_ID, name, importance);
channel.setDescription(description);
NotificationManager notificationManager = getSystemService(NotificationManager.class);
notificationManager.createNotificationChannel(channel);
}
}
}
Also progress notification is really slow and I guess it using so much cpu. Can someone little help to fix it?