안드로이드 7.0 누가 이상버전에서 패키지 설치화면 띄우기
안드로이드 7.0 누가 이상버전에서 패키지 설치화면 띄우기
res/xml폴더에 filepaths.xml을 만들고 아래코드 입력 <?xml version="1.0" encoding="utf-8"?> <paths> <external-path name="storage/emulated" path="."/> </paths> |
AndroidManifest.xml <provider android:name="android.support.v4.content.FileProvider" android:authorities="${applicationId}.fileprovider" android:exported="false" android:grantUriPermissions="true"> <meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/filepaths" /> </provider> |
apk파일 install하는 코드 private void install(String apkName){ String localUrl = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) + apkName; String extension = MimeTypeMap.getFileExtensionFromUrl(localUrl); String mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension); File file = new File(localUrl); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N){ Uri apkUri = FileProvider.getUriForFile(app.getApplicationContext(), BuildConfig.APPLICATION_ID + ".fileprovider", file); Intent intent = new Intent(Intent.ACTION_INSTALL_PACKAGE); intent.setData(apkUri); intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); try { startActivity(intent); } catch (ActivityNotFoundException e) { toastS(getResources().getString(R.string.app_name)+" 설치파일을 찾을수 없습니다"); e.printStackTrace(); } }else{ Uri apkUri = Uri.fromFile(file); Intent intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(apkUri, mimeType); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); try { startActivity(intent); } catch (ActivityNotFoundException e) { toastS(getResources().getString(R.string.app_name)+" 설치파일을 찾을수 없습니다"); e.printStackTrace(); } } } |