使用服务从多任务清除应用程序后,域结果为空

时间:2016-10-23 14:06:43

标签: android realm

我有一个服务,使用ion库下载一些文件,下载完成后我想将下载的文件保存到数据库realm database 问题是当用户从多任务中清除应用程序时,应用程序崩溃并在logcat中显示ArrayIndexOutOfBoundsException 我认为问题是当应用程序被清除时模型类被破坏

这是代码

//服务代码

List<Surah> surahList;

 @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        if (intent != null)
            reciterName = intent.getStringExtra("reciterName");

        surahList = realm.where(Surah.class).equalTo("reciterName", reciterName).findAll();
        Surah surah = surahList.get(10);
        String folderName = surah.getReciterName() + " " + surah.getRewaya();
        File path = new File(Environment.getExternalStorageDirectory() + "/Quran Sound/" + folderName + "/" + surah.getSurahNumberMp3());


        NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
        builder
                .setSmallIcon(R.drawable.ic_stat_noti);
         final NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        notificationManager.notify(2, builder.build());
        Ion.with(this)
                .load(surah.getSurahLink())
                .write(path)
                .setCallback(new FutureCallback<File>() {
                    @Override
                    public void onCompleted(Exception e, File result) {
                        Toast.makeText(DownloadService.this, result + "", Toast.LENGTH_SHORT).show();
                        Log.d("3llomi", "ion Download Finished!");
                        notificationManager.cancel(2);
                        stopSelf();
                    }
                });

        return START_STICKY;
    }

// Logcat

FATAL EXCEPTION: main
Process: com.devlomi.quransound, PID: 4424
java.lang.RuntimeException: Unable to start service com.devlomi.quransound.DownloadService@8f466f2 with null: java.lang.ArrayIndexOutOfBoundsException: rowIndex > available rows: 10 > 0
    at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:3027)
    at android.app.ActivityThread.-wrap17(ActivityThread.java)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1442)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:148)
    at android.app.ActivityThread.main(ActivityThread.java:5417)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
 Caused by: java.lang.ArrayIndexOutOfBoundsException: rowIndex > available rows: 10 > 0
    at io.realm.internal.TableView.nativeGetSourceRowIndex(Native Method)
    at io.realm.internal.TableView.getSourceRowIndex(TableView.java:128)
    at io.realm.RealmResults.get(RealmResults.java:205)
    at io.realm.RealmResults.get(RealmResults.java:70)
    at com.devlomi.quransound.DownloadService.onStartCommand(DownloadService.java:104)
    at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:3010)
    at android.app.ActivityThread.-wrap17(ActivityThread.java) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1442) 
    at android.os.Handler.dispatchMessage(Handler.java:102) 
    at android.os.Looper.loop(Looper.java:148) 
    at android.app.ActivityThread.main(ActivityThread.java:5417) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 

1 个答案:

答案 0 :(得分:0)

好的问题是reciterName来自ActivityService的{​​{1}}使用意图在从多任务中清除应用时被销毁 所以我改变了 return START_STICKYreturn START_REDELIVER_INTENT