Плохое уведомление об ошибке startForeground в AndroidAndroid

Форум для тех, кто программирует под Android
Ответить Пред. темаСлед. тема
Anonymous
 Плохое уведомление об ошибке startForeground в Android

Сообщение Anonymous »

Ошибка возникает при запуске службы переднего плана с уведомлением.
Я пытаюсь загрузить данные из файла JSON в свою базу данных. Поскольку файл большой, я использую службу переднего плана, чтобы отображать ход загрузки в уведомлении.
LoadIntentService.java

Код: Выделить всё

import android.app.NotificationManager;
import android.app.Service;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.IBinder;
import android.util.Log;

import androidx.annotation.Nullable;
import androidx.core.app.NotificationCompat;

import com.android.word.database.DbHelper;
import com.android.word.database.WordDictionaryContract;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.io.InputStream;

import static com.android.word.App.CHANNEL_ID;

public class LoadIntentService  extends Service {

private static final String LOG_TAG = "LoadIntentService";

private DbHelper dbHelper;
private SQLiteDatabase mDatabase;

private Context mContext;

public LoadIntentService() {
super();
Log.d(LOG_TAG,"LoadIntentService");
}

@Override
public void onCreate() {
super.onCreate();

mContext = getApplicationContext();

dbHelper = new DbHelper(this);
mDatabase = dbHelper.getWritableDatabase();
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {

final int countMax = intent.getIntExtra("size",0);

Log.d(LOG_TAG," onStartCommand");

final NotificationCompat.Builder notification = new NotificationCompat.Builder(this,CHANNEL_ID)
.setContentTitle("Loading Dictionary")
.setSmallIcon(R.drawable.ic_file_download_black_24dp)
.setOnlyAlertOnce(true)
.setOngoing(true)
.setProgress(countMax,0,false);

final NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(1,notification.build());

startForeground(1,notification.build());

new Thread(new Runnable() {
@Override
public void run() {

Log.d(LOG_TAG,"  start thread");

String json;

String word,definition,audiourl,synonym,antonym;

try {
Log.d(LOG_TAG,"parsing json");
InputStream inputStream = mContext.getAssets().open("worddictionary.json");
int size = inputStream.available();
Log.d(LOG_TAG,"parsing json size: "+size);
byte[] buffer = new byte[size];
inputStream.read(buffer);
inputStream.close();
json = new String(buffer, "UTF-8");
// Log.d(LOG_TAG,"parsing json data: "+json);
JSONArray jsonArray = new JSONArray(json);

for (int i=0;i=Build.VERSION_CODES.O){
NotificationChannel serviceChannel = new NotificationChannel(
CHANNEL_ID,"Loading Dictionary",
NotificationManager.IMPORTANCE_HIGH
);

NotificationManager manager = getSystemService(NotificationManager.class);
manager.createNotificationChannel(serviceChannel);
}
}

}
Ошибка

Код: Выделить всё

2020-09-08 22:42:06.863 5076-5076/com.android.word E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.android.word, PID: 5076
android.app.RemoteServiceException:  Bad notification for startForeground
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1738)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6692)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
Я не знаю, что вызывает эту ошибку. Я также предоставил разрешение службы переднего плана в AndroidManifest.xml.

Подробнее здесь: https://stackoverflow.com/questions/637 ... in-android
Реклама
Ответить Пред. темаСлед. тема

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

  • Похожие темы
    Ответы
    Просмотры
    Последнее сообщение
  • Плохое уведомление для startForeground playerNotificationManagerBuilder.setNotificationListener
    Гость » » в форуме Android
    0 Ответы
    40 Просмотры
    Последнее сообщение Гость
  • Ошибка в приложении Flutter: плохое уведомление для startForeground
    Anonymous » » в форуме Android
    0 Ответы
    31 Просмотры
    Последнее сообщение Anonymous
  • Ошибка в приложении Flutter: плохое уведомление для startForeground
    Anonymous » » в форуме Android
    0 Ответы
    22 Просмотры
    Последнее сообщение Anonymous
  • Ошибка в приложении Flutter: Плохое уведомление о Startforeground
    Anonymous » » в форуме Android
    0 Ответы
    15 Просмотры
    Последнее сообщение Anonymous
  • Ошибка в приложении Flutter: Плохое уведомление о Startforeground
    Anonymous » » в форуме Android
    0 Ответы
    1 Просмотры
    Последнее сообщение Anonymous

Вернуться в «Android»