[英]Wait For AsyncTask to finish before executing script
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();
}
});
}
}
Sorry if I didn't search good enough. 对不起,如果我搜索不够好。 I have my code with me so I'll post it.
我有我的代码所以我会发布它。 Also, as you can probably see, I have no idea how to include a script in my app and then run it.
另外,正如您可能看到的,我不知道如何在我的应用程序中包含脚本然后运行它。 I tried making a new folder and putting the script in it, but it doesn't work.
我尝试制作一个新文件夹并将脚本放入其中,但它不起作用。 Help on that would too be appreciated...
对此的帮助也将受到赞赏......
Edit: I got the first part to work. 编辑:我得到了第一部分工作。 Now I cannot execute the script.
现在我无法执行脚本。 su is executed, but my script is not.
su被执行,但我的脚本不是。 I also need to know where to put my script in my app, and then run that script.
我还需要知道将我的脚本放在我的应用程序中的哪个位置,然后运行该脚本。 I'm not sure where the script will be when you install the app.
我不确定安装应用程序时脚本的位置。 And I don't know where to put the script.
我不知道在哪里放脚本。 So help would be appreciated.
所以帮助将不胜感激。 Below(I guess above now.lol) is the updated code:
下面(我猜上面now.lol)是更新的代码:
You have the onPostExecute
method that you can use for post-execution instructions. 您有
onPostExecute
方法可用于执行后指令。 His parameter depends on what you specify as an output in the AsyncTask
definition. 他的参数取决于您在
AsyncTask
定义中指定的输出。
In order to use an output, you need to return something in the doInBackground
method. 要使用输出,您需要在
doInBackground
方法中返回一些doInBackground
。 A good practice would be to check if this output is null or not before using it. 一个好的做法是在使用之前检查此输出是否为空。 That should do it =)
应该这样做=)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.