我在我的设备上拥有超级用户权限。我曾经非常成功地使用这个功能来编写和更新我的应用程序,但自从安卓6.0以来,由于新类型的权限请求,这个功能已经停止工作了。
我的问题是:既然我在我的rooted设备上拥有超级用户访问权限,我该如何编辑我的功能,以便在不需要向用户请求权限的情况下下载SD卡上的外部文件呢?
以下是我用来更新应用程序的功能:
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");