我发现了一个麻烦的问题,那就是自动下载新版本的应用程序
1。活动部分的主要代码:
private void downloadFile(String url) {
String dir = FileUtils.getSDCardPath()+ "/";
String fileName = System.currentTimeMillis() + ".apk";
path=dir+fileName;
verifyStoragePermissions(UpdateActivity.this);
if (shouldAskPermissions()) {
askPermissions();
}
OkHttpUtils
.get()
.url(url)
.build().execute(
new FileCallBack(dir,fileName) {
@Override
public void onBefore(Request request, int id) {
super.onBefore(request, id);
}
@Override
public void inProgress(float progress, long total, int id) {
// super.inProgress(progress, total, id);
int progressl=(int) (100 * progress);
L.i("toal "+total);
L.i("prgress :"+progressl);
Message msg=new Message();
msg.what=HANDLER_LODING;
Bundle bundle=new Bundle();
bundle.putLong("total",total);
bundle.putInt("progress",progressl);
msg.setData(bundle);
handler.sendMessage(msg);
}
@Override
public void onError(Call call, Exception e, int id) {
L.i("error" +e.getMessage().toString()+" local:"+e.getLocalizedMessage()+" cause "+e.getCause()+" code"+e.getStackTrace());
UtilTools.sendEmail(getClass().toString()+"失败 e:"+e.getMessage());
handler.sendEmptyMessage(HANDLER_ON);
}
@Override
public void onResponse(File response, int id) {
// OpenFileTipDialog.openFiles(downloadFile.getAbsolutePath(),YearShuilddjhActivity.this);
Log.e("info: ", "onResponse :" + response.getAbsolutePath());
L.i("success");
handler.sendEmptyMessage(HANDLER_OK);
}
}
);
}
AndroidMenifest.xml中的2
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_INTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES" />
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />
// all user permission about download file
03-15 15:32:25.648 11709-11709/com.example.darren.fdc I/darrenli: error/storage/emulated/0/1552635145458.apk: open failed: EACCES (Permission denied) local:/storage/emulated/0/1552635145458.apk: open failed: EACCES (Permission denied) cause android.system.ErrnoException: open failed: EACCES (Permission denied) code[Ljava.lang.StackTraceElement;@2d58e5b8
api 'com.squareup.picasso:picasso:2.5.2'
api 'com.zhy:okhttputils:2.6.2'
//RxVolley
api('com.kymjs.rxvolley:rxvolley:1.1.2') {
exclude group: 'com.squareup.okhttp3', module: 'okhttp'
exclude group: 'com.squareup.okio'
exclude group: 'io.reactivex'
}
//RxVolley下载模块
api('com.kymjs.rxvolley:okhttp:1.1.0') {
exclude group: 'com.squareup.okhttp3'
exclude group: 'com.squareup.okio'
}
//NumberProgressBar
api 'com.daimajia.numberprogressbar:library:1.2@aar'
api 'io.reactivex:rxandroid:1.0.1'
api 'io.reactivex:rxjava:1.0.14'
之后,我进行了一些测试:
第一次尝试:
private static final int REQUEST_EXTERNAL_STORAGE = 1;
private static String[] PERMISSIONS_STORAGE = {
Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.WRITE_EXTERNAL_STORAGE };
public void verifyStoragePermissions(Activity activity) {
// Check if we have write permission
int permission = ActivityCompat.checkSelfPermission(activity,
Manifest.permission.WRITE_EXTERNAL_STORAGE);
L.i("error" +permission);
L.i("error1" + PackageManager.PERMISSION_GRANTED);
if (permission != PackageManager.PERMISSION_GRANTED) {
// We don't have permission so prompt the user
ActivityCompat.requestPermissions(activity, PERMISSIONS_STORAGE,
REQUEST_EXTERNAL_STORAGE); } }
//致电我的下载代码
第二次尝试:
protected boolean shouldAskPermissions() {
return (Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP_MR1);
}
@TargetApi(23)
protected void askPermissions() {
String[] permissions = {
"android.permission.READ_EXTERNAL_STORAGE",
"android.permission.WRITE_EXTERNAL_STORAGE"
};
int requestCode = 200;
requestPermissions(permissions, requestCode);
}
第三次尝试
public static boolean RootCommand(String command){
Process process = null;
DataOutputStream os = null;
try{
process = Runtime.getRuntime().exec("su");
os = new DataOutputStream(process.getOutputStream());
os.writeBytes(command + "\n");
os.writeBytes("exit\n");
os.flush();
process.waitFor();
} catch (Exception e){
return false;
} finally{
if (os != null){
try {
os.close();
}catch (IOException e){
e.printStackTrace();
}
}
if(process!=null) {
process.destroy();
}
}
return true;
}
String apkRoot="chmod 777 "+path;
UpdateActivity.RootCommand(apkRoot);