Browse Source

现场调试改动100

master
fyf 1 year ago
parent
commit
a10bfee6c6
3 changed files with 57 additions and 9 deletions
  1. +11
    -0
      app/src/main/AndroidManifest.xml
  2. +37
    -9
      app/src/main/java/com/example/bpa/helper/UpdateManager.java
  3. +9
    -0
      app/src/main/res/xml/fileproviderpath.xml

+ 11
- 0
app/src/main/AndroidManifest.xml View File

@@ -107,6 +107,17 @@
android:exported="false"
android:windowSoftInputMode="adjustPan|stateHidden" /> <!-- Mqtt Service -->
<service android:name="org.eclipse.paho.android.service.MqttService" />
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="${applicationId}.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/fileproviderpath">
</meta-data>
</provider>

</application>

</manifest>

+ 37
- 9
app/src/main/java/com/example/bpa/helper/UpdateManager.java View File

@@ -95,7 +95,7 @@ public class UpdateManager {
break;
//下载完成,进行安装
case DOWN_OVER:
theClass.installApk();
theClass.installApkNew();
}
}
}
@@ -301,14 +301,42 @@ public class UpdateManager {
* @param url
*/
private void installApk(){
File apkfile = new File(savePath+saveFileName);
if (!apkfile.exists()) {
return;
try {
File apkfile = new File(savePath + saveFileName);
if (!apkfile.exists()) {
return;
}
Intent i = new Intent(Intent.ACTION_VIEW);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//com.example.silentinstallation.fileprovider
Uri uri = Uri.parse("file://" + apkfile.toString());
i.setDataAndType(uri, "application/vnd.android.package-archive");
mContext.startActivity(i);
} catch(Exception e){
T.show(mContext,"错误信息,正在安装..."+e.getMessage());
}
}

private void installApkNew(){
try {

File apkfile = new File(savePath + saveFileName);
Intent intent = new Intent();
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setAction(Intent.ACTION_VIEW);
Uri uri = null;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
//fileprovider为上述manifest中provider所配置相同
uri = FileProvider.getUriForFile(mContext, mContext.getPackageName() + ".fileprovider", apkfile);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
} else {
uri = Uri.fromFile(apkfile);
}
intent.setDataAndType(uri, "application/vnd.android.package-archive");
mContext.startActivity(intent);
} catch(Exception e){
T.show(mContext,"错误信息,正在安装..."+e.getMessage());
}
Intent i = new Intent(Intent.ACTION_VIEW);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Uri uri = Uri.parse("file://" + apkfile.toString());
i.setDataAndType(uri, "application/vnd.android.package-archive");
mContext.startActivity(i);
}
}

+ 9
- 0
app/src/main/res/xml/fileproviderpath.xml View File

@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<!--因为要安装的apk是存储在sd卡的,这里的映射就相当于:
content:虚拟路径 映射到了-> Environment.getExternalStorageDirectory()
-->
<external-path
name="external_path"
path="." />
</paths>

Loading…
Cancel
Save