Created
April 4, 2022 02:44
-
-
Save nicemak/ded0d5db3dd25c00c4f65ba2d0fb2dbf to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
If you want install application programmatically in Android 10, You need to give permission to install app for your application | |
============================================================================== | |
Steps 1: Give permission in Manifest | |
============================================================================== | |
<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES" /> | |
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> | |
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> | |
============================================================================== | |
Step 2: Write provider in Android Manifest | |
============================================================================== | |
<provider | |
android:name="androidx.core.content.FileProvider" | |
android:authorities="${applicationId}.provider" | |
android:exported="false" | |
android:grantUriPermissions="true"> | |
<meta-data | |
android:name="android.support.FILE_PROVIDER_PATHS" | |
android:resource="@xml/provider_paths" /> | |
</provider> | |
============================================================================== | |
Step 3: Create file in XML folder named as provider_paths.xml and write | |
============================================================================== | |
<?xml version="1.0" encoding="utf-8"?> | |
<paths xmlns:android="http://schemas.android.com/apk/res/android"> | |
<external-path | |
name="files_root" | |
path="Android/data/${applicationId}" /> | |
<external-path | |
name="external_files" | |
path="." /> | |
</paths> | |
============================================================================== | |
Step 4: Give runtime permission for apk installation and storage permissions | |
============================================================================== | |
//installtion permission | |
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { | |
if (!getPackageManager().canRequestPackageInstalls()) { | |
startActivityForResult(new Intent(Settings.ACTION_MANAGE_UNKNOWN_APP_SOURCES).setData(Uri.parse(String.format("package:%s", getPackageName()))), 1234); | |
} else { | |
} | |
} | |
//Storage Permission | |
if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) { | |
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE,Manifest.permission.READ_EXTERNAL_STORAGE}, 1); | |
} | |
if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) { | |
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1); | |
} | |
============================================================================== | |
Step 5: Call install apk function | |
============================================================================== | |
void installAPK(){ | |
String PATH = Environment.getExternalStorageDirectory() + "/" + "apkname.apk"; | |
File file = new File(PATH); | |
if(file.exists()) { | |
Intent intent = new Intent(Intent.ACTION_VIEW); | |
intent.setDataAndType(uriFromFile(getApplicationContext(), new File(PATH)), "application/vnd.android.package-archive"); | |
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); | |
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); | |
try { | |
getApplicationContext().startActivity(intent); | |
} catch (ActivityNotFoundException e) { | |
e.printStackTrace(); | |
Log.e("TAG", "Error in opening the file!"); | |
} | |
}else{ | |
Toast.makeText(getApplicationContext(),"installing",Toast.LENGTH_LONG).show(); | |
} | |
} | |
Uri uriFromFile(Context context, File file) { | |
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { | |
return FileProvider.getUriForFile(context, BuildConfig.APPLICATION_ID + ".provider", file); | |
} else { | |
return Uri.fromFile(file); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment