The article directories

    • Adapter that
    • Enable permission to allow unknown applications to be installed
    • Check whether the application is installed
    • Normal browsers download APK
    • Secure Download (Download APK from App Store)

Adapter that

Automatic update many APP will have, there are also many third-party libraries, but many are these libraries were written by two or three years ago, there was no 8.0, 9.0 the high version of the mobile phone, if rushed to use, found that there is no problem, when the test on the new version will be found on many phones automatically update download does not automatically after the completion of installation, At this time, there are already many versions online. Once forced to update, there will be problems on these new phones, which is difficult to make up, so the third-party library must be used with caution. This problem is because after api26, Android system added a permission. That is the permission to install the app in the allowed location, which is disabled by default. In 8.0 + OS phones, if you do not enable this permission, you will not see the installation screen after downloading the app update. Therefore, it is necessary to guide the user to open the permission to install the application in the allowed location of the phone above 8.0 after the automatic update download is completed.

Enable permission to allow unknown applications to be installed

Configure permissions in the manifest file:

    <uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES"/>
Copy the code
private void openAllowedPermission() { final AlertDialog.Builder alertDialog = new AlertDialog.Builder(HomeActivity1.this); alertDialog.setIcon(R.mipmap.logo); Alertdialog.settitle (" version update "); Alertdialog. setMessage(" Setup exception not avoided, please set application authorization "); alertDialog.setCancelable(false); AlertDialog. SetNegativeButton (" cancel ", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { finish(); }}); AlertDialog. SetPositiveButton (" to set up ", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, Int which) {/ / 8.0 allows position application installation permission to apply for * * Uri packageURI = Uri. Parse (" package: "+ HomeActivity1. Enclosing getPackageName ()); Intent intent = new Intent(Settings.ACTION_MANAGE_UNKNOWN_APP_SOURCES,packageURI); startActivityForResult(intent, INSTALL_APK_REQUESTCODE); * *}}); alertDialog.show(); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); If (requestCode == INSTALL_APK_REQUESTCODE && resultCode ==0){if (requestCode ==0){installApkFile (context, file); Intent Intent = new Intent(settings. action_manage_unknown_sources); startActivityForResult(intent, GET_UNKNOWN_APP_SOURCES); Toastutils.showtoast2 (this," Please enable permission to install unknown applications "); Public static void installApkFile(Context Context, File file) { Intent intent1 = new Intent(Intent.ACTION_VIEW); if ( Build.VERSION.SDK_INT >= Build.VERSION_CODES.N ) { intent1.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); intent1.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); Uri contentUri = FileProvider.getUriForFile(context, context.getPackageName() + ".fileprovider", file); intent1.setDataAndType(contentUri, "application/vnd.android.package-archive"); } else { intent1.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive"); intent1.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); } if ( context.getPackageManager().queryIntentActivities(intent1, 0).size() > 0 ) { context.startActivity(intent1); }Copy the code

Check whether the application is installed

package com.lab.web.entity; import android.content.ComponentName; import android.content.Context; import android.content.Intent; import android.content.pm.ApplicationInfo; import android.content.pm.PackageInfo; import android.content.pm.PackageManager; import android.text.TextUtils; import static android.app.Activity.RESULT_OK; /** * Created by ppg on 2017/12/19. */ public class CheckApkExist { private static String facebookPkgName = "com.tencent.android.qqdownloader "; Public static Boolean checkApkExist(Context Context, String packageName){ if (TextUtils.isEmpty(packageName)) return false; try { ApplicationInfo info = context.getPackageManager() .getApplicationInfo(packageName, PackageManager.GET_UNINSTALLED_PACKAGES); // Timber.d(info.toString()); return true; } catch (PackageManager.NameNotFoundException e) { // Timber.d(e.toString()); return false; } } public static boolean checkFacebookExist(Context context){ return checkApkExist(context, facebookPkgName); } public static boolean isAppInstalled(Context context,String packagename) { PackageInfo packageInfo; try { packageInfo = context.getPackageManager().getPackageInfo(packagename, 0); }catch (PackageManager.NameNotFoundException e) { packageInfo = null; e.printStackTrace(); } if(packageInfo ==null){// system.out.println (" not installed "); return false; }else{// system.out.println (" installed "); return true; }}}Copy the code

Normal browsers download APK

boolean b = CheckApkExist.(this, packeName.toString()); / / com. Android. App. Quanmama_5. 0.3 _503 if (b) {/ / installed open securities mom / / Uri Uri = Uri. Parse (" market: / / details? id=" + "com.android.app.quanmama"); Uri uri = Uri.parse("market://details? id=" + packeName.toString()); Intent goToMarket = new Intent(Intent.ACTION_VIEW, uri); try { Constants.yingyonbbao = true; // goToMarket.setClassName("com.tencent.android.qqdownloader", "com.android.app.quanmama"); myContext.startActivity(goToMarket); } catch (ActivityNotFoundException e) { e.printStackTrace(); } } else { Constants.yingyonbbao = true; // Uri uri = Uri.parse("market://details? id=com.android.app.quanmama"); Parse ("market://details? id=" + packeName.toString()); Intent it = new Intent(intent.action_view, uri); startActivity(it); }Copy the code

Secure Download (Download APK from App Store)

boolean b = CheckApkExist.isAppInstalled(this, "com.tencent.android.qqdownloader"); Parse ("market://details? id=" + "com.android.app.quanmama"); Uri uri = Uri.parse("market://details? id=" + packeName.toString()); Intent goToMarket = new Intent(Intent.ACTION_VIEW, uri); try { // goToMarket.setClassName("com.tencent.android.qqdownloader", "com.tencent.pangu.link.LinkProxyActivity"); myContext.startActivity(goToMarket); Constants.yingyonbbao = true; } catch (ActivityNotFoundException e) { e.printStackTrace(); } Toast.LENGTH_SHORT).show(); Else {// Constants. Yingyonbbao = true; Uri uri = Uri.parse("market://details? id="+packeName.toString()); Intent it = new Intent(intent.action_view, uri); startActivity(it); }Copy the code