2014-01-13 174 views
1

當我使用此代碼時,我的phonegap應用程序沒有響應/ timout窗口焦點問題/ appwindowtoken等。基本上,應用程序加載,但我有黑屏,一切都凍結。phonegap android應用程序沒有響應

也許我正在處理很多主要活動。我正在嘗試提取文件。

我該如何解決這個問題。

代碼讚賞。

在此先感謝,

傑里米

import java.io.BufferedInputStream; 
    import java.io.BufferedOutputStream; 
    import java.io.File; 
    import java.io.FileInputStream; 
    import java.io.FileNotFoundException; 
    import java.io.FileOutputStream; 
    import java.io.IOException; 
    import java.io.OutputStreamWriter; 
    import java.util.zip.ZipEntry; 
    import java.util.zip.ZipInputStream; 

    import android.os.Bundle; 
    import android.os.Environment; 
    import android.util.Log; 

    import org.apache.cordova.*; 

    public class tester extends DroidGap 
    { 
     @SuppressWarnings("deprecation") 
     @Override 
     public void onCreate(Bundle savedInstanceState) 
     { 
      super.onCreate(savedInstanceState); 
      // Set by <content src="index.html" /> in config.xml 
      super.loadUrl(Config.getStartUrl()); 
      //super.loadUrl("file:///android_asset/www/index.html") 
      File extStore = Environment.getExternalStorageDirectory(); 
      File file = new File(extStore.getAbsolutePath()+"/mysoundboard/com.test.dsoundfxboard/1.dat"); 
      if(! file.exists())   
      { 


      boolean test = noiser.extractZip(extStore.getAbsolutePath()+"/mysoundboard/mydsoundfxboard.zip", extStore.getAbsolutePath()+"/mysoundboard/com.test.dsoundfxboard/"); 
      FileOutputStream fOut = null; 
      OutputStreamWriter osw = null; 
      if(test == true) 
      { 
       try { 
       fOut openFileOutput(extStore.getAbsolutePath()+"/mysoundboard/com.test.dsoundfxboard/1.dat",MODE_WORLD_READABLE); 
      osw = new OutputStreamWriter(fOut); 

       osw.write('1'); 
      } catch (IOException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
      try { 
       osw.flush(); 
      } catch (IOException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
      try { 
       osw.close(); 
       fOut.close(); 
      } catch (IOException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      }  
      } 
      } 
     } 



     private static boolean extractZip(String pathOfZip,String pathToExtract) 
     { 


       int BUFFER_SIZE = 1024; 
       int size; 
       byte[] buffer = new byte[BUFFER_SIZE]; 


       try { 
        File f = new File(pathToExtract); 
        if(!f.isDirectory()) { 
         f.mkdirs(); 
        } 
        ZipInputStream zin = new ZipInputStream(new BufferedInputStream(new FileInputStream(pathOfZip), BUFFER_SIZE)); 
        try { 
         ZipEntry ze = null; 
         while ((ze = zin.getNextEntry()) != null) { 
          String path = pathToExtract +"/"+ ze.getName(); 

          if (ze.isDirectory()) { 
           File unzipFile = new File(path); 
           if(!unzipFile.isDirectory()) { 
            unzipFile.mkdirs(); 
           } 
          } 
          else { 
           FileOutputStream out = new FileOutputStream(path, false); 
           BufferedOutputStream fout = new BufferedOutputStream(out, BUFFER_SIZE); 
           try { 
            while ((size = zin.read(buffer, 0, BUFFER_SIZE)) != -1) { 
             fout.write(buffer, 0, size); 
            } 

            zin.closeEntry(); 
           }catch (Exception e) { 
            Log.e("Exception", "Unzip exception 1:" + e.toString()); 
           } 
           finally { 
            fout.flush(); 
            fout.close(); 
           } 
          } 
         } 
        }catch (Exception e) { 
         Log.e("Exception", "Unzip exception2 :" + e.toString()); 
        } 
        finally { 
         zin.close(); 
        } 
        return true; 
       } 
       catch (Exception e) { 
        Log.e("Exception", "Unzip exception :" + e.toString()); 
       } 
       return false; 

      } 

    } 

回答

0

您的代碼阻塞UI線程,使用AsyncTask提取在一個單獨的線程的壓縮文件。