2013-03-03 63 views
0
package com.cydeon.plasmamodz; 

import java.io.BufferedInputStream; 
import java.io.BufferedReader; 
import java.io.FileOutputStream; 
import java.io.IOException; 
import java.io.InputStream; 
import java.io.InputStreamReader; 
import java.io.OutputStream; 
import java.io.Reader; 
import java.net.URL; 
import java.net.URLConnection; 
import java.util.List; 

import com.cydeon.plasmamodz.R; 

import android.app.ActionBar; 
import android.app.Activity; 
import android.app.DownloadManager; 
import android.app.ProgressDialog; 
import android.content.Context; 
import android.content.Intent; 
import android.content.pm.PackageManager; 
import android.content.pm.ResolveInfo; 
import android.net.Uri; 
import android.os.AsyncTask; 
import android.os.Build; 
import android.os.Bundle; 
import android.os.Environment; 
import android.util.Log; 
import android.view.View; 
import android.widget.Button; 
import android.widget.ImageView; 
import android.widget.TextView; 
import android.widget.Toast; 

//Class for Boot Animation Blue Kindle 
public class Boots extends Activity { 

public static String TAG = "Boots"; 
Process process; 

private class DownloadFile extends AsyncTask<String, Integer, String>{ 
    @Override 
    protected String doInBackground(String... sURL) { 
     try{ 
      URL url = new URL(sURL[0]); 
      URLConnection connection = url.openConnection(); 
      connection.connect(); 
      //Shows 0-100% progress bar 
      int fileLength = connection.getContentLength(); 

      //Download the file 
      InputStream input = new BufferedInputStream(url.openStream()); 
      OutputStream output = new FileOutputStream("/sdcard/TWRP-Blaze-2.4.3.0-1.zip"); 

      byte data[] = new byte[1024]; 
      long total = 0; 
      int count; 
      while ((count = input.read(data)) != -1) { 
       total += count; 
       //Publish the Progress 
       publishProgress((int) (total * 100/fileLength)); 
       output.write(data, 0, count); 
       } 

      output.flush(); 
      output.close(); 
      input.close(); 
    } catch (Exception e) { 

    } 
    return null; 
    } 

    @Override 
    protected void onPreExecute() { 
     super.onPreExecute(); 
     mProgressDialog.show(); 
    } 

    @Override 
    protected void onProgressUpdate(Integer... progress){ 
     super.onProgressUpdate(progress); 
     mProgressDialog.setProgress(progress[0]); 



    } 

    @Override 
    protected void onPostExecute(String result) { 
     // TODO Auto-generated method stub 
     super.onPostExecute(result); 
     mProgressDialog.dismiss(); 
     Context context = getApplicationContext(); 
     CharSequence text = "Installing. Please Wait"; 
     int duration = Toast.LENGTH_SHORT; 

     Toast toast = Toast.makeText(context, text, duration); 
     toast.show(); 

     try { 
       Runtime rt = Runtime.getRuntime(); 
       Process proc = rt.exec("su"); 

       proc = rt.exec("sh /sdcard/boots.sh"); 
       InputStream is = proc.getInputStream(); 
       InputStreamReader isr = new InputStreamReader(is); 
       BufferedReader br = new BufferedReader(isr); 
       String line; 

       while ((line = br.readLine()) != null){ 
        System.out.println(line); 
       } 
     }catch (Throwable t){ 
      t.printStackTrace(); 
     } 

    } 
} 

ProgressDialog mProgressDialog; 


@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.boots); 
    ActionBar actionBar = getActionBar(); 
    actionBar.hide(); 
    ImageView img = (ImageView) findViewById(R.id.iv2); 
    img.setImageResource(R.drawable.boot1); 
    Button install = (Button) findViewById(R.id.bAInstall); 
    Button rtrn = (Button) findViewById(R.id.bAReturn); 
    mProgressDialog = new ProgressDialog(Boots.this); 
    mProgressDialog.setMessage("Downloading..."); 
    mProgressDialog.setIndeterminate(false); 
    mProgressDialog.setMax(100); 
    mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); 

    install.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View arg0) { 
      DownloadFile downloadFile = new DownloadFile(); 
      downloadFile.execute("https://dl.dropbox.com/s/t16a0cq0qcon2ux/TWRP-Blaze-2.4.3.0-1.zip"); 



      } 

     } 
    ); 

    rtrn.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View arg0) { 
      finish(); 
     } 
    }); 

    } 



    } 

對不起,如果我沒有足夠的搜索。我有我的代碼,所以我會發布它。另外,正如你可能看到的,我不知道如何在我的應用中包含腳本,然後運行它。我嘗試製作一個新文件夾並將腳本放入其中,但它不起作用。幫助,這將不勝感激...等待AsyncTask完成之前執行腳本

編輯:我得到了第一部分工作。現在我無法執行腳本。蘇被執行,但我的腳本不是。我還需要知道將腳本放在應用程序中的哪個位置,然後運行該腳本。我不確定安裝應用程序時腳本的位置。我不知道該把腳本放在哪裏。所以幫助將不勝感激。下面(我猜上面now.lol)是更新的代碼:

回答

2

您有onPostExecute方法,您可以用於執行後指令。他的參數取決於您在AsyncTask定義中指定的輸出。

爲了使用輸出,您需要返回doInBackground方法中的內容。一個好的做法是在使用它之前檢查這個輸出是否爲空。這應該這樣做=)

+0

好的。如您所見,我將腳本執行放在OnClickListener中。先把它移到onPostExecute然後呢? – user2128299 2013-03-03 06:47:09

+0

是的,如果您將'Runtime'指令移至'onPostExecute',它們將在'doInBackground'完成後立即執行。 – 2013-03-03 06:54:37

+0

謝謝。至於我的劇本,你知道爲什麼它不會執行嗎? – user2128299 2013-03-03 07:03:08

相關問題