【问题标题】:How to use intent in Asynctask class?如何在 Asynctask 类中使用意图?
【发布时间】:2014-10-09 11:22:48
【问题描述】:

请分享如何在 doinbackground() 或 onpostexecute() 方法 Asynctask 类中使用 Intent。当我尝试使用这些代码时,它会显示错误。

Intent intent = new Intent(asynctask.this, home.class);
startActivity(intent);
finish();

private Class<Home> clazz;
        public asynctask(Class<Home> clazz){
            this.clazz = clazz;
        }

Asynctask doInBackground() 方法:

  protected Void doInBackground(Void... arg0) {
        // TODO Auto-generated method stub
        Intent intent = new Intent(this, clazz);
        startActivity(intent);
        finish();
        Toast.makeText(cxt, "welcome", Toast.LENGTH_SHORT).show();
        return null;
    }

【问题讨论】:

  • asynctask 你的活动吗?
  • 您必须始终在 onPostExecute() 方法中使用 Intent.. 并且不要在 doInBackground() 中使用 toast,Toast 应该可能是它在您的代码中给出错误并请发布您的 logcat ..
  • 请清楚地告诉我是 android 新手,我不知道 asynctask 类函数。

标签: android android-intent android-asynctask


【解决方案1】:

试试这个方法,希望能帮助你解决问题。

如何异步任务类:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    new MyCustomAsyncTask(this).execute();
 }

MyCustomAsyncTask.java

public class MyCustomAsyncTask extends AsyncTask<Void,Void,Void> {
    private Context context;

    public MyCustomAsyncTask(Context context){
        this.context=context;
    }
    @Override
    protected void onPreExecute() {
        // write show progress Dialog code here
        super.onPreExecute();
    }

    @Override
    protected Void doInBackground(Void... params) {
        // write service code here
        return null;
    }

    @Override
    protected void onPostExecute(Void aVoid) {
        super.onPostExecute(aVoid);
        Toast.makeText(context, "welcome", Toast.LENGTH_SHORT).show();
        Intent intent = new Intent(context, home.class);
        context.startActivity(intent);
        ((Activity)context).finish();
    }
}

【讨论】:

  • new MyCustomAsyncTask(YourActivityContext).execute();中的youractivitycontext是什么意思
  • 当我执行你的代码时,我收到了这个错误The method startActivity(Intent) is undefined for the type asyncdata.sync
【解决方案2】:

将这个Intent部分移到AsynckTaskonPostExecute(...)方法中

【讨论】:

  • 当我将 Intent 移动到 onPostExecute(...) 时表示它显示此错误 构造函数 Intent(Async, Class) 未定义 @Manish
【解决方案3】:

doInBackground(Void... arg0) 应该只做后台任务 您应该将其他代码放在onPostExecute(...) 方法中。这样当后台任务结束时移动到其他活动。

** 不要尝试从doInBackground(....) 触摸 UI,您的应用可能会崩溃。

【讨论】:

    【解决方案4】:

    您无法与doInBackground(....) 中的用户界面交互。您只能在onPostExecute(...) 中与 UI 交互。就像线程一样,你不能在 Thread for UI 中与 UI 交互,我们使用 Handler。

    【讨论】:

      【解决方案5】:

      始终将意图放在onPostExecute 中。这将确保您的 UI 线程是同步的。

      例如,如果您想表明在接收到正确的凭据后,用户应该移至下一个活动,否则应该显示“凭据无效”消息以防他们出错。你的onPostExecute 应该是这样的:

      protected void onPostExecute(final Boolean success) {
           if(success){
               Intent intent = new Intent(<CurrentActivity>.this, <NextActivity>.class);
               startActivity(intent);
           }
           else{
               Toast.makeText(LoginActivity.this, "Invalid Credentials", Toast.LENGTH_SHORT).show();
           }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多