Hello ,Guys Today we will learn about how to Request to Run Time permission in android studio.as we know that WRITE_EXTERNAL_STORAGE permission is deprecated in android 10 or above .
So today we will learn how to get run time permission in Android 11, first of all after creating a project in Android Studio .
First of all we will give some permission in manifest like WRITE_EXTERNAL STORAGE,READ_EXTERNAL_STORAGE and
MANAGE_EXTERNAL_STORAGE whose code is given below.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.example.vidtakedownloader">
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_v"
android:label="@string/app_name"
android:largeHeap="true"
android:requestLegacyExternalStorage="true"
android:roundIcon="@mipmap/ic_v_round"
android:supportsRtl="true"
android:theme="@style/Theme.VidTakeDownloader"
android:usesCleartextTraffic="true"
tools:ignore="GoogleAppIndexingWarning"
tools:targetApi="q">
<activity
android:name=".MainActivity"
android:theme="@style/Theme.VidTakeDownloader.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Here we are seeing that we have used
android:requestLegacyExternalStorage="true" in the manifest, we have used this because to get run time permission in Android 10 we have to requestLegacyExternalStorage="true" otherwise it is Android 10 Will not take storage permission in device.
After this we will go to MainActivity class where we will coding it. In MainActivity class we will take run time permission in all devices below android 11 also.
So let's see how we will take this permission, the code is being given below which you should understand very carefully.
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.provider.Settings;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import com.example.vidtakedownloader.R;
import static android.Manifest.permission.WRITE_EXTERNAL_STORAGE;
public class MainActivity extends AppCompatActivity {
static final int code=333;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash_screen);
if (checkPermission())
{
startSpash();
}
else
{
RequestPermission();
}
}
public boolean checkPermission()
{
if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.R)
{
return Environment.isExternalStorageManager();
}
else
{
int write= ContextCompat.checkSelfPermission(getApplicationContext(),WRITE_EXTERNAL_STORAGE);
return write== PackageManager.PERMISSION_GRANTED;
}
}
public void RequestPermission()
{
if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.R)
{
try {
Intent intent=new Intent(Settings.ACTION_MANAGE_ALL_FILES_ACCESS_PERMISSION);
intent.addCategory("android.intent.category.DEFAULT");
intent.setData(Uri.parse(String.format("package:%s",new Object []{getApplicationContext().getPackageName()})));
startActivityForResult(intent,2000);
}
catch(Exception e)
{
Intent obj=new Intent();
obj.setAction(Settings.ACTION_MANAGE_ALL_FILES_ACCESS_PERMISSION);
startActivityForResult(obj,2000);
}
}
else
{
ActivityCompat.requestPermissions(Splash.this,new String[]{WRITE_EXTERNAL_STORAGE},code);
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode==2000)
{
if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.R)
{
if (Environment.isExternalStorageManager())
{
startSpash();
}
else
{
}
}
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
switch (requestCode)
{
case c:
if (grantResults.length>0)
{
boolean storage=grantResults[0] ==PackageManager.PERMISSION_GRANTED;
if (storage)
{
startSpash();
}
else
{
RequestPermission();
}
}
break;
}
}
private void startSpash() {
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
startActivity(new Intent(MainActivity.this, MainActivity.class));
finish();
}
}, 1000);
}
}
This is also useful for you
0 Comments
Please do not enter any spam link in the comment box
Emoji