【发布时间】:2018-06-30 15:26:00
【问题描述】:
我的设备上有超级用户访问权限。我非常成功地使用此功能以编程方式下载和更新我的应用程序,但从 android 6.0 开始,此功能停止工作(因为新类型的权限请求)。
我的问题是:由于我在我的根设备上拥有超级用户访问权限,如何编辑我的功能以便我可以在不征求用户许可的情况下下载 sdcard 上的外部文件?
这是我用来更新应用程序的功能:
public class UpdateAppZ extends AsyncTask<String,Void,Void>{
private Context context;
public void setContext(Context contextf){
context = contextf;
}
@Override
protected Void doInBackground(String... arg0) {
try {
URL url = new URL(arg0[0]);
HttpURLConnection c = (HttpURLConnection) url.openConnection();
c.setRequestMethod("GET");
c.setDoOutput(true);
c.connect();
String PATH = "/mnt/sdcard/Download/";
File file = new File(PATH);
file.mkdirs();
File outputFile = new File(file, "update.apk");
if(outputFile.exists()){
outputFile.delete();
}
FileOutputStream fos = new FileOutputStream(outputFile);
InputStream is = c.getInputStream();
byte[] buffer = new byte[1024];
int len1 = 0;
while ((len1 = is.read(buffer)) != -1) {
fos.write(buffer, 0, len1);
}
fos.close();
is.close();
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(new File("/mnt/sdcard/Download/update.apk")), "application/vnd.android.package-archive");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); // without this flag android returned a intent error!
context.startActivity(intent);
} catch (Exception e) {
Log.e("UpdateAPP", "Update error! " + e.getMessage());
}
return null;
}
}
然后调用:
UpdateAppZ atualizaApp = new UpdateAppZ();
atualizaApp.setContext(getApplicationContext());
atualizaApp.execute("http://85.118.98.251/misho/app-debug.apk");
【问题讨论】:
-
你的构建工具、目标和编译的sdk版本是什么?
-
buildToolsVersion '23.0.3' minSdkVersion 19 targetSdkVersion 23 这个函数在 6.0 之前运行良好,但我需要对其进行编辑以便它也可以在 6.0 上运行
-
为什么不将它下载到 File 的应用程序目录?您不需要公开 apk。
-
@cgarrido 你能提供代码 sn-p 吗?下载完成后,我需要从设备运行下载的文件
-
正如建议:不要使用“/mnt/sdcard/Download/”,所有设备都没有相同的挂载点路径。首选Environment.getExternalStorageDirectory()
标签: android android-6.0-marshmallow android-permissions