【问题标题】:Proper way to terminate Async Task and inform the user:终止异步任务并通知用户的正确方法:
【发布时间】:2014-12-05 05:58:12
【问题描述】:

我正在将一条记录插入到一​​个数据库中,该数据库在两个字段(名称和 DOB)上有一个唯一的约束。我从 Async 任务的 DoInBackground() 方法插入。我这样做如下:

try{
                        insertInDb(name.trim(),start_date.trim(),imagePath.trim(), String.valueOf(TYPE),String.valueOf(SELECTION), name_friend.trim(), isAdhoc);
                    }catch (Exception e){
                        System.out.println("Caught up here"+e);
                        return "Record Exists";
                    }   

这是我的 postExecute():

public void onPostExecute(String result){
            //TODO
            if(result.equalsIgnoreCase("Record Exists")){
                if(Asycdialog !=null && Asycdialog.isShowing())
                    Asycdialog.dismiss();
                Toast.makeText(getApplicationContext(), "Record Exists please try a different name and year", Toast.LENGTH_LONG).show(); 

            }else if(result.equalsIgnoreCase("Success")){
                if(Asycdialog !=null && Asycdialog.isShowing()){
                    Asycdialog.dismiss();

                    Intent mainIntent;
                    mainIntent = new Intent(AdhocCase.this,MainActivity.class); 
                    AdhocCase.this.startActivity(mainIntent);
                    AdhocCase.this.finish(); 
                }
            }

从 doInBackground 返回时“记录存在”的 toast 消息似乎永远不会执行。但是意图确实发生了变化。

这是我完整的 doInBackground():

protected String doInBackground(Void... params) {
    // TODO Auto-generated method stub

    try{
        System.out.println("Type in async: "+type);
        System.out.println("Selection in async: "+String.valueOf(SELECTION));
        System.out.println("TYPE in async: "+String.valueOf(TYPE));
        dbHelper.open(); 

        Cursor checkCur = dbHelper.checkIfRecordExists(name.trim(), start_date.trim()); 
        System.out.println("Cursor count"+checkCur.getCount()); 
        if(checkCur != null && checkCur.moveToFirst()){
            System.out.println("Exists"); 
            return "Record Exists";
        }else{
            System.out.println("Image path: "+imagePath);
            insertAdhocCase(toTitleCase(name).trim(),start_date.trim(),imagePath, type, String.valueOf(SELECTION), toTitleCase(name_friend).trim());

            try{
                insertInDb(name.trim(),start_date.trim(),imagePath.trim(), String.valueOf(TYPE),String.valueOf(SELECTION), name_friend.trim(), isAdhoc);
            }catch (Exception e){
                System.out.println("Caught up here"+e);
                return "Record Exists";
            }   
        //  LC.writeToSDCard();
        }

    }catch(Exception e){
        //  System.out.println(e); 
    }

    return "Success";
}

}

【问题讨论】:

  • 你的意思是return "Record Exists"; 不工作?
  • 你不能在doInBackground()发消息Toast
  • 我在 onPostExecute 上干杯。
  • @BlazeTama 是的 - 完全正确!
  • 使用 AsyncTask.Status.FINISHED 怎么样?

标签: android android-asynctask


【解决方案1】:

我认为问题出在你的

 Toast.makeText(getApplicationContext(), "Record Exists please try a different name and year", Toast.LENGTH_LONG).show();

您必须从调用 Activity 中显式提供上下文,例如

Toast.makeText(context.getApplicationContext(), "Record Exists please try a different name and year", Toast.LENGTH_LONG).show();

Check this 供参考。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-07-16
    • 2019-01-22
    • 1970-01-01
    • 2014-12-01
    • 1970-01-01
    • 2016-09-28
    • 1970-01-01
    相关资源
    最近更新 更多