English 中文(简体)
A. 落实AsyncTask深层的问题
原标题:issue implementing an AsyncTask thread

我有以下问题...... I m implementing a QR代码fish in my android application..., which is carried out this int:

 Intent intent = new Intent(
                           "com.google.zxing.client.android.SCAN");
                   intent.setPackage("com.google.zxing.client.android");
                   intent.putExtra("SCAN_MODE", "QR_CODE_MODE");
                   startActivityForResult(intent, 0); 

换言之,我开始使用<条码>《巴斯堪纳编码>。

Want I want to do is launch this application and even if the user starts scanning a barcode or not the application will close automatically after two minutes.

当然,如果用户关闭,意味着时间。

我尝试执行<条码>。 类似于:

<>载在上

initTask=new InitTask();
initTask.execute();

<>斯特凡>

private class InitTask extends AsyncTask<Void,Void,Void>{

           protected Void doInBackground(Void...unused){

           new Runnable() {
                @Override
                public void run() {
                 try {
                   Thread.sleep(20000);
                 } 
                 catch (InterruptedException e) {
                   e.printStackTrace();
                 }
                 Intent intent = new Intent(
                           "com.google.zxing.client.android.SCAN");
                   intent.setPackage("com.google.zxing.client.android");
                   intent.putExtra("SCAN_MODE", "QR_CODE_MODE");
                   startActivityForResult(intent, 0); 
                }
               }.run();
                this.cancel(true);


           return null;

       }

   }

<>斯特凡>

   public void onActivityResult(int requestCode, int resultCode, Intent intent) {
     //..........
    }

The problem is that the app gets opened but it doesn t close automatically after 2 minutes. Does someone know how could I achieve that?

最佳回答

<>Update>

private class InitTask extends AsyncTask<Void,Void,Void>{
@Override
protected void onPreExecute()
{
   Intent intent = new Intent("com.google.zxing.client.android.SCAN");
   intent.setPackage("com.google.zxing.client.android");
   intent.putExtra("SCAN_MODE", "QR_CODE_MODE");
   startActivityForResult(intent, 1515);
}
protected Void doInBackground(Void...unused){

try
{
       Thread.sleep(20000);
}
catch(Exception e)
{
    System.out.println(e);
}
return null;

}
@Override
protected void onPostExecute(Void result) 
{

   finishActivity(1515);

}


}

Activity Result

public void onActivityResult(int requestCode, int resultCode, Intent intent) {
            if (requestCode == 1515) {
                if (resultCode == RESULT_OK) {
                    String contents = intent.getStringExtra("SCAN_RESULT");
                    String format = intent.getStringExtra("SCAN_RESULT_FORMAT");
                    System.out.println("it is ok");
                    // Handle successful scan
                } else if (resultCode == RESULT_CANCELED) {
                    // Handle cancel
                    System.out.println("it is cancel");
                }
            }
        }
问题回答

我认为这里有许多问题。

取消(......)并不强行阻止在执行期间执行权利的任务。 如果它已经避风港,它就无法运行。

即便如此,也不会做你想要的事情。 您可操作的发射是一种完全不同的活动和立即返回。 它并不阻碍或等待。 时间已晚了2分钟。

即便如此,中断这一呼吁也不会对其他活动产生影响。 您可以强行结束另一场活动(别处,无需平台允许)。

你的发言时间不是2分钟,而是20秒。





相关问题
Android - ListView fling gesture triggers context menu

I m relatively new to Android development. I m developing an app with a ListView. I ve followed the info in #1338475 and have my app recognizing the fling gesture, but after the gesture is complete, ...

AsyncTask and error handling on Android

I m converting my code from using Handler to AsyncTask. The latter is great at what it does - asynchronous updates and handling of results in the main UI thread. What s unclear to me is how to handle ...

Android intent filter for a particular file extension?

I want to be able to download a file with a particular extension from the net, and have it passed to my application to deal with it, but I haven t been able to figure out the intent filter. The ...

Android & Web: What is the equivalent style for the web?

I am quite impressed by the workflow I follow when developing Android applications: Define a layout in an xml file and then write all the code in a code-behind style. Is there an equivalent style for ...

TiledLayer equivalent in Android [duplicate]

To draw landscapes, backgrounds with patterns etc, we used TiledLayer in J2ME. Is there an android counterpart for that. Does android provide an option to set such tiled patterns in the layout XML?

Using Repo with Msysgit

When following the Android Open Source Project instructions on installing repo for use with Git, after running the repo init command, I run into this error: /c/Users/Andrew Rabon/bin/repo: line ...

Android "single top" launch mode and onNewIntent method

I read in the Android documentation that by setting my Activity s launchMode property to singleTop OR by adding the FLAG_ACTIVITY_SINGLE_TOP flag to my Intent, that calling startActivity(intent) would ...

From Web Development to Android Development

I have pretty good skills in PHP , Mysql and Javascript for a junior developer. If I wanted to try my hand as Android Development do you think I might find it tough ? Also what new languages would I ...

热门标签