gpt4 book ai didi

android - 以编程方式更新 apk(下载并开始安装)Android API v26

转载 作者:行者123 更新时间:2023-11-29 02:35:12 28 4
gpt4 key购买 nike

我阅读了关于这个问题的所有解决方案。我也知道它可以被认为是重复的,但事实并非如此。我看到错误:再试一次 toast 消息,我看到更新错误日志消息。

我认为在 android v26 中改变了一些关于 intent.setDataAndType 的东西,或者我不知道为什么这不起作用。

我也得到了类似这段代码的权限

ActivityCompat.requestPermissions(Update.this,
new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
REQUEST_WRITE_STORAGE);

问题无法解决。我只想下载并安装 apk 文件。

AndroidManifest.xml(我添加了写权限)

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

更新.java

package com.my.testapp;

public class Update extends AppCompatActivity {

ProgressDialog bar;
private static String TAG = "Update";

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

new DownloadNewVersion().execute();

}

class DownloadNewVersion extends AsyncTask<String,Integer,Boolean> {

@Override
protected void onPreExecute() {
super.onPreExecute();

bar = new ProgressDialog(Update.this);
bar.setCancelable(false);

bar.setMessage("Downloading...");

bar.setIndeterminate(true);
bar.setCanceledOnTouchOutside(false);
bar.show();

}

protected void onProgressUpdate(Integer... progress) {
super.onProgressUpdate(progress);

bar.setIndeterminate(false);
bar.setMax(100);
bar.setProgress(progress[0]);
String msg = "";
if(progress[0]>99){

msg="Finishing... ";

}else {

msg="Downloading... "+progress[0]+"%";
}
bar.setMessage(msg);

}
@Override
protected void onPostExecute(Boolean result) {
// TODO Auto-generated method stub
super.onPostExecute(result);

bar.dismiss();

if(result){

Toast.makeText(getApplicationContext(),"Update Done",
Toast.LENGTH_SHORT).show();

}else{

Toast.makeText(getApplicationContext(),"Error: Try Again",
Toast.LENGTH_SHORT).show();

}

}

@Override
protected Boolean doInBackground(String... arg0) {
Boolean flag = false;

try {


URL url = new URL("http://mydownloadurl.com/_download/update.apk");

HttpURLConnection c = (HttpURLConnection) url.openConnection();
c.setRequestMethod("GET");
c.setDoOutput(true);
c.connect();


String PATH = Environment.getExternalStorageDirectory()+"/Download/";
File file = new File(PATH);
file.mkdirs();

File outputFile = new File(file,"app-debug.apk");

if(outputFile.exists()){
outputFile.delete();
}

FileOutputStream fos = new FileOutputStream(outputFile);
InputStream is = c.getInputStream();

int total_size = 277962;//size of apk

byte[] buffer = new byte[1024];
int len1 = 0;
int per = 0;
int downloaded=0;
while ((len1 = is.read(buffer)) != -1) {
fos.write(buffer, 0, len1);
downloaded +=len1;
per = (int) (downloaded * 100 / total_size);
publishProgress(per);
}
fos.close();
is.close();

OpenNewVersion(PATH);

flag = true;
} catch (Exception e) {
Log.e(TAG, "Update Error: " + e.getMessage());
flag = false;
}
return flag;

}

}

void OpenNewVersion(String location) {

Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(new File(location + "app-debug.apk")),
"application/vnd.android.package-archive");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);

}

最佳答案

Commonsware 非常感谢,我浪费了 2-3 个小时来做​​不同的事情。

其实我之前加了这段代码,但是问题并没有解决。我再次尝试,它成功了,它可能是一个懒惰的解决方案,但它正在工作。

This url have more info about the problem ,可以应用其他解决方案,但这对我来说已经足够了。

        if(Build.VERSION.SDK_INT>=24){
try{
Method m = StrictMode.class.getMethod("disableDeathOnFileUriExposure");
m.invoke(null);
}catch(Exception e){
e.printStackTrace();
}
}

已经完全修复,再次非常感谢。应用程序也需要此存储权限。

ActivityCompat.requestPermissions(Update.this,new String[]{android.Manifest.permission.WRITE_EXTERNAL_STORAGE

关于android - 以编程方式更新 apk(下载并开始安装)Android API v26,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47501972/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com