关闭应用程序后无法访问Job Service中的Internet

问题描述 投票:0回答:1

因此,我编写了这部分作业服务类,旨在在后台访问Internet,下载少量内容并以15秒的间隔显示通知(稍后将其增加到45分钟,不用担心):] >

import android.app.PendingIntent;
import android.app.job.JobParameters;
import android.app.job.JobService;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.util.Log;
import androidx.core.app.NotificationCompat;
import androidx.core.app.NotificationManagerCompat;
import java.util.ArrayList;
import java.util.HashMap;

public class NotificationService extends JobService {

    DownloadNotification d;

    @Override
    public boolean onStartJob(JobParameters params) {
        d=new DownloadNotification();
        d.execute(params);
        return true;
    }

    @Override
    public boolean onStopJob(JobParameters params) {
        return false;
    }

    private class DownloadNotification extends AsyncTask<JobParameters,Void,JobParameters>
    {
        ArrayList<HashMap<String,String>> resN;
        ArrayList<HashMap<String,String>> resP;

        SharedPreferences sp;

        @Override
        protected void onPreExecute() {
            sp=getSharedPreferences("eTutionPro",MODE_PRIVATE);
            super.onPreExecute();
        }

        @Override
        protected JobParameters doInBackground(JobParameters... params) {
            resN=Connectivity.query("SELECT * FROM `notices` WHERE noticeID>? ORDER BY noticeID DESC LIMIT 15",""+sp.getInt("LastNotificationID",-1));
            return params[0];
        }

        @Override
        protected void onPostExecute(JobParameters pam) {
            Log.d("Thread","From Thread NOTICE RESULT : "+(resN!=null?""+resN.size():"null"));

            SharedPreferences.Editor prefEditor = sp.edit();
            if(resN!=null) {
                boolean b=true;
                for (HashMap<String, String> i : resN) {

                    if(b)
                    {
                        b=false;
                        prefEditor.putInt("LastNotificationID", Integer.parseInt(i.get("noticeID")));
                        prefEditor.commit();
                    }

                    Intent intent = new Intent(getApplicationContext(), ViewNotice.class);
                    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
                    PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, intent, 0);

                    NotificationCompat.Builder builder = new NotificationCompat.Builder(getApplicationContext(), "101")
                            .setSmallIcon(R.drawable.logo)
                            .setContentTitle(i.get("noticeTitle"))
                            .setContentText(i.get("noticeBody"))
                            .setContentIntent(pendingIntent)
                            .setPriority(NotificationCompat.PRIORITY_DEFAULT);

                    NotificationManagerCompat notificationManager = NotificationManagerCompat.from(getApplicationContext());
                    notificationManager.notify(Integer.parseInt(i.get("noticeID")), builder.build());
                }
            }
            jobFinished(pam,false);
        }
    }
}

当该应用程序处于活动状态或处于最近状态时,一切正常。但是,当从最近的应用程序中删除该应用程序时,除了NotificationService类中的Async类(即。 DownloadNotification中的doInBackground无法访问Internet下载数据。自定义函数Connectivity.query(String,String ...)在无法获取数据时返回null(按设计)。我猜这个类没有问题,因为一切正常。不知道该怎么办。

所以我写了这段Job Service Class,旨在在后台访问Internet,下载少量内容并以15秒的间隔显示Notification(稍后将其增加到...

android android-asynctask background jobservice
1个回答
0
投票
[抱歉,这是JWT令牌验证代码的问题,而不是上面的代码,已集成到我的REST API中。通过创建另一个API找到了解决方法。
© www.soinside.com 2019 - 2024. All rights reserved.