从asynctask传递上下文?

 mobiledu2502905163 发布于 2023-02-06 03:17

好的,所以我有一个运行asynctask的任务服务类,然后在条件满足时启动通知.在发送方法中有一个空指针异常,其中创建了Intent,上下文具有所有空值.我不认为我正确地从BloodLevelParse asynctask传递上下文?不确定我还能尝试什么.这是班级:

public class TaskService extends IntentService {

    private ArrayList BloodLevelList;

    private String bloodLevel;
    private String bloodType;
    private Integer bloodLevelDays;

    private MyProfileActivity myProfileActivity;

    private BloodLevelParse bloodLevelParse;

    public TaskService() {
        super("TaskService");
    }

    @Override
    protected void onHandleIntent(Intent intent) {

        // Pass through the blood type
        Bundle bundle = intent.getExtras();
        bloodType = bundle.getString("currentBloodType2");

        // Only perform check if there is an internet connection
        if (CheckNetwork.isInternetAvailable(TaskService.this)) {

            // Check the current blood level for the given blood type
            new BloodLevelParse(this).execute(bloodType);

        } else {
            Toast.makeText(
                    TaskService.this,
                    "Unable to perform daily blood level check, no internet connection.",
                    Toast.LENGTH_LONG).show();
        }

    }

    private void sendNotification(Context context) {

        NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
                this).setSmallIcon(R.drawable.ic_launcher)
                .setContentTitle("Blood Level Notification")
                .setContentText(bloodLevel);

        Intent resultIntent = new Intent(this, MyProfileActivity.class);

        // Because clicking the notification opens a new ("special") activity,
        // there's no need to create an artificial back stack.
        PendingIntent resultPendingIntent = PendingIntent.getActivity(this, 0,
                resultIntent, PendingIntent.FLAG_UPDATE_CURRENT);

        mBuilder.setContentIntent(resultPendingIntent);

        // Sets an ID for the notification
        int mNotificationId = 001;
        // Gets an instance of the NotificationManager service
        NotificationManager mNotifyMgr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        // Builds the notification and issues it.
        mNotifyMgr.notify(mNotificationId, mBuilder.build());

    }

    public class BloodLevelParse extends
            AsyncTask> {

        private Context myCtx;

        public BloodLevelParse(Context ctx){
            // Now set context
            this.myCtx = ctx;
        }

        protected ArrayList doInBackground(String... params) {

            String bloodType = params[0];

            try {

                // HTTP PROTOCOL
                Document doc = Jsoup
                        .connect(
                                "http://www.giveblood.ie/Current_Blood_Supply/")
                        .timeout(5000)
                        .userAgent(
                                "Mozilla/5.0 (Windows; U; WindowsNT 5.1; en-US; rv1.8.1.6) Gecko/20070725 Firefox/2.0.0.6")
                        .cookie("auth", "token").get();

                // EXTRACT SPECIFIC ELEMENTS FROM THE HTML
                Elements bloodsupplylines1 = doc.select("td.bloodsupplyline");
                Elements bloodsupplylines2 = bloodsupplylines1
                        .select("img[src~=(?i)\\.(png|jpe?g|gif)]");

                // CREATE LIST AND POPULATE IT WITH ELEMENTS RETRIEVE FROM HTML
                List BloodLevelRawList = new ArrayList();

                for (Element bloodsupplyline : bloodsupplylines2) {

                    BloodLevelRawList.add(new String(bloodsupplyline
                            .attr("alt")));

                }

                BloodLevelList = new ArrayList();

                for (int i = 0; i < BloodLevelRawList.size(); i += 2) {
                    // Only add to BloodLevelList if it is the blood type
                    // selected
                    if (BloodLevelRawList.get(i).equals(bloodType)) {

                        BloodLevelList.add(new BloodLevels(BloodLevelRawList
                                .get(i), BloodLevelRawList.get(i + 1)));
                    }
                }

            }

            catch (IOException ioex) {
                ioex.printStackTrace();
            }

            return BloodLevelList;

        }

        @Override
        protected void onPostExecute(ArrayList BloodLevelList) {

            String bloodLevelReplace = BloodLevelList.get(0).getBloodLevel()
                    .replaceAll("Blood supply", "");

            bloodLevel = ("Current blood levels for "
                    + BloodLevelList.get(0).getBloodType() + " is" + bloodLevelReplace);

            // Get the number of days from the blood level string
            Scanner in = new Scanner(bloodLevel).useDelimiter("[^0-9]+");
            int bloodLevelDays = in.nextInt();

            // Send notification if no. of days blood level supply goes below 5
            if (bloodLevelDays < 5) {

                TaskService taskService = new TaskService();
                taskService.sendNotification(myCtx);
            }

        }

    }

}

编辑:根据tyczj的回答,我不需要asynctask,愚蠢的我.这是工作版本:

public class TaskService extends IntentService {

private ArrayList BloodLevelList;

private String bloodLevel;
private String bloodType;
private Integer bloodLevelDays;

public TaskService() {
    super("TaskService");
}

@Override
protected void onHandleIntent(Intent intent) {

    // Pass through the blood type from AlarmReceiver
    Bundle bundle = intent.getExtras();
    bloodType = bundle.getString("currentBloodType2");

    // Only perform check if there is an internet connection
    if (CheckNetwork.isInternetAvailable(TaskService.this)) {

        try {

            // HTTP PROTOCOL
            Document doc = Jsoup
                    .connect(
                            "http://www.giveblood.ie/Current_Blood_Supply/")
                    .timeout(5000)
                    .userAgent(
                            "Mozilla/5.0 (Windows; U; WindowsNT 5.1; en-US; rv1.8.1.6) Gecko/20070725 Firefox/2.0.0.6")
                    .cookie("auth", "token").get();

            // EXTRACT SPECIFIC ELEMENTS FROM THE HTML
            Elements bloodsupplylines1 = doc.select("td.bloodsupplyline");
            Elements bloodsupplylines2 = bloodsupplylines1
                    .select("img[src~=(?i)\\.(png|jpe?g|gif)]");

            // CREATE LIST AND POPULATE IT WITH ELEMENTS RETRIEVE FROM HTML
            List BloodLevelRawList = new ArrayList();

            for (Element bloodsupplyline : bloodsupplylines2) {

                BloodLevelRawList.add(new String(bloodsupplyline
                        .attr("alt")));

            }

            BloodLevelList = new ArrayList();

            for (int i = 0; i < BloodLevelRawList.size(); i += 2) {
                // Only add to BloodLevelList if it is the blood type
                // selected
                if (BloodLevelRawList.get(i).equals(bloodType)) {

                    BloodLevelList.add(new BloodLevels(BloodLevelRawList
                            .get(i), BloodLevelRawList.get(i + 1)));
                }
            }

        }

        catch (IOException ioex) {
            ioex.printStackTrace();
        }

        String bloodLevelReplace = BloodLevelList.get(0).getBloodLevel()
                .replaceAll("Blood supply", "");

        bloodLevel = ("Current blood levels for "
                + BloodLevelList.get(0).getBloodType() + " is" + bloodLevelReplace);

        // Get the number of days from the blood level string
        Scanner in = new Scanner(bloodLevel).useDelimiter("[^0-9]+");
        int bloodLevelDays = in.nextInt();

        // Send notification if no. of days blood level supply goes below 5
        if (bloodLevelDays < 5) {

            this.sendNotification(this);

        }

    } else {
        Toast.makeText(
                TaskService.this,
                "Unable to perform daily blood level check, no internet connection.",
                Toast.LENGTH_LONG).show();
    }

}

private void sendNotification(Context context) {

    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
            this).setSmallIcon(R.drawable.ic_launcher)
            .setContentTitle("Blood Level Notification")
            .setContentText(bloodLevel);

    Intent resultIntent = new Intent(this, MyProfileActivity.class);

    // Because clicking the notification opens a new ("special") activity,
    // there's no need to create an artificial back stack.
    PendingIntent resultPendingIntent = PendingIntent.getActivity(this, 0,
            resultIntent, PendingIntent.FLAG_UPDATE_CURRENT);

    mBuilder.setContentIntent(resultPendingIntent);

    // Sets an ID for the notification
    int mNotificationId = 001;
    // Gets an instance of the NotificationManager service
    NotificationManager mNotifyMgr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    // Builds the notification and issues it.
    mNotifyMgr.notify(mNotificationId, mBuilder.build());

}

}

1 个回答
  • 为什么你在使用AsyncTask一个IntentService?一个IntentService已经在一个单独的线程上运行,所以使用AsyncTask不是nesaccery.

    此外,由于IntentService从derrived Context所有你所要做的就是通话TaskService.this和你有背景

    2023-02-06 03:51 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有