Android 10 Ошибка при остановке службы уведомлений с помощью кнопки в уведомлении. При нажатии на кнопку «Стоп» уведомления очистка уведомления занимает 5–6 секунд.
, а если щелкнуть 2 раза до закрытия уведомления, он переходит к методу onCreate()
службы.
также я проверяю в уведомлении приложение Google chrome, которое имеет ту же проблему
Работает нормально, ниже Android 10.
код с использованием услуги
public class DownloadService extends Service {
private NotificationCompat.Builder myNotify;
private String id, downloadUrl, file_path, file_name;
private RemoteViews rv;
private OkHttpClient client;
public static final String ACTION_STOP = "com.mydownload.action.STOP";
public static final String ACTION_START = "com.mydownload.action.START";
private String NOTIFICATION_CHANNEL_ID = "download_ch_1";
private static final String CANCEL_TAG = "c_tag";
NotificationManager mNotificationManager;
public int NOTIFICATION_ID = 105;
private Handler mHandler = new Handler(new Handler.Callback() {
@Override
public boolean handleMessage(Message message) {
int progress = Integer.parseInt(message.obj.toString());
switch (message.what) {
case 1:
rv.setTextViewText(R.id.nf_title, getString(R.string.app_name));
rv.setProgressBar(R.id.progress, 100, progress, false);
rv.setTextViewText(R.id.nf_percentage, getResources().getString(R.string.downloading) + " " + "(" + progress + " %)");
mNotificationManager.notify(NOTIFICATION_ID, myNotify.build());
break;
case 2:
stopForeground(false);
stopSelf();
break;
}
return false;
}
});
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
super.onCreate();
mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
myNotify = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID)
.setContentTitle(getString(R.string.app_name))
.setPriority(Notification.PRIORITY_MAX)
.setSmallIcon(R.drawable.ic_onesignal_large_icon_default)
.setTicker(getResources().getString(R.string.downloading))
.setWhen(System.currentTimeMillis())
.setOnlyAlertOnce(true);
rv = new RemoteViews(getPackageName(),
R.layout.my_custom_notification);
rv.setTextViewText(R.id.nf_title, getString(R.string.app_name));
rv.setProgressBar(R.id.progress, 100, 0, false);
rv.setTextViewText(R.id.nf_percentage, getResources().getString(R.string.downloading) + " " + "(0%)");
Intent closeIntent = new Intent(this, DownloadService.class);
closeIntent.setAction(ACTION_STOP);
PendingIntent pcloseIntent = PendingIntent.getService(this, 0,
closeIntent, 0);
rv.setOnClickPendingIntent(R.id.relativeLayout_nf, pcloseIntent);
myNotify.setCustomContentView(rv);
NotificationChannel mChannel;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
CharSequence name = getResources().getString(R.string.app_name);// The user-visible name of the channel.
mChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, name, NotificationManager.IMPORTANCE_HIGH);
mNotificationManager.createNotificationChannel(mChannel);
}
startForeground(NOTIFICATION_ID, myNotify.build());
}
@Override
public void onDestroy() {
super.onDestroy();
stopForeground(false);
stopSelf();
}
@SuppressLint("LogNotTimber")
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
super.onStartCommand(intent, flags, startId);
if (intent.getAction() != null && intent.getAction().equals(ACTION_START)) {
id = intent.getStringExtra("id");
downloadUrl = intent.getStringExtra("downloadUrl");
file_path = intent.getStringExtra("file_path");
file_name = intent.getStringExtra("file_name");
init();
}
if (intent.getAction() != null && intent.getAction().equals(ACTION_STOP)) {
try {
File file = new File(file_path, file_name);
if (file.exists()) {
file.delete();
}
Method.isDownload = true;
if (client != null) {
for (Call call : client.dispatcher().runningCalls()) {
if (call.request().tag().equals(CANCEL_TAG))
call.cancel();
}
}
stopService(intent);
stopForeground(false);
mNotificationManager.cancel(NOTIFICATION_ID);
stopSelf();
} catch (Exception e) {
Log.d("error_fail", e.toString());
}
}
return START_STICKY;
}
@SuppressLint("LogNotTimber")
public void init() {
new Thread(new Runnable() {
@Override
public void run() {
//download code using OkHttpClient
//update download data call case 1
//complete download call case 2
}
}).start();
}
}