Skip to content

Commit

Permalink
兼容Android 8.0 安装未知来源的限制
Browse files Browse the repository at this point in the history
  • Loading branch information
itlwy committed Jan 11, 2020
1 parent b9d0123 commit 2271a39
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 1 deletion.
43 changes: 42 additions & 1 deletion app/src/main/java/com/lwy/appsmartupdate/MainActivity.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
package com.lwy.appsmartupdate;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.provider.Settings;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;

Expand All @@ -10,8 +17,10 @@
public class MainActivity extends AppCompatActivity {


private static final int INSTALL_PACKAGES_REQUESTCODE = 101;
private static final int GET_UNKNOWN_APP_SOURCES = 102;
private String manifestJsonUrl = "https://raw.githubusercontent.com/itlwy/AppSmartUpdate/master/resources/app/UpdateManifest.json";
// private String manifestJsonUrl = "http://192.168.2.107:8000/app/UpdateManifest.json";
// private String manifestJsonUrl = "http://192.168.2.107:8000/app/UpdateManifest.json";
private IUpdateCallback mCallback;
private TextView mVersionTV;

Expand All @@ -21,6 +30,7 @@ protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.activity_main);
mVersionTV = (TextView) findViewById(R.id.version_tv);
mVersionTV.setText(BuildConfig.VERSION_CODE + "");
checkAndroidOUnknowSource();
checkUpdate();
}

Expand Down Expand Up @@ -76,4 +86,35 @@ protected void onDestroy() {
super.onDestroy();

}


@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
switch (requestCode) {
case INSTALL_PACKAGES_REQUESTCODE:
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
} else {
Intent intent = new Intent(Settings.ACTION_MANAGE_UNKNOWN_APP_SOURCES);
startActivityForResult(intent, GET_UNKNOWN_APP_SOURCES);
}
break;

}
}

/**
* 8.0需要处理未知应用来源权限问题
*/
private void checkAndroidOUnknowSource() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
boolean b = this.getPackageManager().canRequestPackageInstalls();
if (!b) {
//请求安装未知应用来源的权限
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.REQUEST_INSTALL_PACKAGES}, INSTALL_PACKAGES_REQUESTCODE);
}
}

}

}
1 change: 1 addition & 0 deletions smartupdate/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES"/>

<application>
<service android:name=".UpdateService"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,5 @@ public static boolean installApk(Context context, String filePath) {
return false;
}


}

0 comments on commit 2271a39

Please sign in to comment.