问候爱好者的编程!
我在调用外部类时遇到问题,我用过:
AutoUpdate shh = new AutoUpdate();
shh;
但是,我无法通过 MainActivity 调用该课程。
目前我正在调用该类,好像它是另一个活动(但事实并非如此,该类所做的是验证是否有新版本并显示对话框,如果现在或之后实现。),我使用过:
Intent uppp = new Intent(this, AutoUpdate.class);
startActivity(uppp,null);
代码完美无缺。但我不希望这创建第二个活动,重要的是在相同的 MainActivity 中显示对话框。
这是代码:
public class AutoUpdate extends AppCompatActivity {
public static String url1 = "localhost/version.json";
String VersionUpdate;
String Cambios;
String link;
String nombre;
String ejecutar;
private ProgressDialog pDialog;
public static final int progress_bar_type = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_navigator);
new VersionCheck().execute();
permission_check();
}
public void permission_check() {
if(ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED){
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},100);
return;
}
}
//initialize();
}
public class VersionCheck extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected Void doInBackground(Void... arg0) {
HttpHandler sh = new HttpHandler();
String jsonStr = sh.makeServiceCall(url1);
if (jsonStr != null){
try {
JSONObject jsonObj = new JSONObject(jsonStr);
JSONArray obtener = jsonObj.getJSONArray("Obtener");
for (int i = 0; i < obtener.length(); i++)
{
JSONObject v = obtener.getJSONObject(i);
link = v.getString("link");
VersionUpdate = v.getString("version");
nombre = v.getString("nombre");
Cambios = "";
JSONArray cambiosArr = v.getJSONArray("cambios");
for (int j = 0; j < cambiosArr.length(); j++) {
Cambios += cambiosArr.getString(j) + "\n";
}
}
}catch (final JSONException e) {
// Log.e(TAG, "Json parsing error: " + e.getMessage());
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplicationContext(),
"El formato de JSON es invalido: " + e.getMessage(),
Toast.LENGTH_LONG)
.show();
}
});
}
} else {
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplicationContext(),
"El servidor de comprobar la version esta caido, por favor chequear la version en: Ajustes > Comprobar",
Toast.LENGTH_LONG)
.show();
}
});
}
return null;
}
@Override
protected void onPostExecute (Void result){
if (VersionUpdate != null) {
super.onPostExecute(result);
String VersionName = BuildConfig.VERSION_NAME;
if (VersionUpdate.equals(VersionName)) {
/*Toast.makeText(getApplicationContext(),
"Version actual: " + VersionName,
Toast.LENGTH_LONG)
.show();*/
finish();
} else {
AlertDialog.Builder builder = new AlertDialog.Builder(AutoUpdate.this);
builder.setTitle("Actualización");
builder.setIcon(R.mipmap.ic_launcher);
builder.setCancelable(false);
builder.setMessage("Actual: "+ VersionName + "\n"+ "Disponible: " + VersionUpdate + "\n" + "\n" + "Incluye: " +"\n" +"\n" + Cambios + "\n")
.setPositiveButton("¡Actualizame!", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//Ejecutamos el class para descargar la version
new DownloadFileFromURL().execute(link);
}
}
)
.setNegativeButton("Despues", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
finish();
}
});
AlertDialog alert = builder.create();
alert.show();
}
}
}
}
@Override
protected Dialog onCreateDialog(int id){
switch (id){
case progress_bar_type:
pDialog = new ProgressDialog(this);
pDialog.setMessage("Descargando actualización"+"\n"+"Espere...");
pDialog.setIndeterminate(false);
pDialog.setMax(100);
pDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
pDialog.setCancelable(false);
pDialog.show();
return pDialog;
default:
return null;
}
}
class DownloadFileFromURL extends AsyncTask<String, String, String>{
@Override
protected void onPreExecute(){
super.onPreExecute();
showDialog(progress_bar_type);
}
@Override
protected String doInBackground(String... f_url){
int count;
try{
URL url = new URL(f_url[0]);
URLConnection connection = url.openConnection();
connection.connect();
int lenghtOfFile = connection.getContentLength();
InputStream input = new BufferedInputStream(url.openStream(), 8192);
String storageDir = Environment.getExternalStorageDirectory().getAbsolutePath();
String fileName = "/download/"+nombre+VersionUpdate+".apk";
File arch = new File(storageDir+fileName);
OutputStream output = new FileOutputStream(arch);
ejecutar = storageDir+fileName;
byte data[] = new byte[1024];
long total = 0;
while((count = input.read(data)) != -1){
total += count;
publishProgress(""+(int)((total*100)/lenghtOfFile));
output.write(data, 0, count);
}
output.flush();
output.close();
input.close();
}catch (Exception e){
//Log.e("Error: ", e.getMessage());
}return null;
}
protected void onProgressUpdate(String... progress){
pDialog.setProgress(Integer.parseInt(progress[0]));
}
@Override
protected void onPostExecute(String file_url){
dismissDialog(progress_bar_type);
//aca ejecutamos al finalizar la descarga
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(new File(ejecutar)),
"application/vnd.android.package-archive");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); // without this flag android returned a intent error!
startActivity(intent);
finish(); //finalizamos cerrando la app
}
}
}
我将阅读它们,看看有什么可能的解决方案。
对于那些可能没有理解我的人,或者我没有给自己解释好。
编辑:
这是MainActivity:
中答案 0 :(得分:0)
从gulp.src('src/assets/scss/**/*.scss')
使用MainActivity
方法<{1}}致电AutoUpdate.java
startActivityForResult()
机制的文档
Intent i = new Intent(this, AutoUpdate.class);
startActivityForResult(i, 1);
在完成后启动您想要结果的活动。退出此活动后,系统会使用给定的void startActivityForResult (Intent intent,
int requestCode,
Bundle options)
调用onActivityResult()
method。使用否定requestCode
与调用requestCode
有关更多示例,请同时查看此answer