Skip to content

Snappy1

  • Home
  • Android
  • What
  • How
  • Is
  • Can
  • Does
  • Do
  • Why
  • Are
  • Who
  • Toggle search form

[FIXED] android – Run time permission from application

Posted on November 11, 2022 By

Solution 1 :

If you need a permission before you could even request the permission, maybe you should conclude that your code is not well architectured.

Simply bypass that code until you can restart it when permissions are granted.

In your question you seem to want to send the device ID when application is created. Just don’t send it unless you have permission to get that.

if (checkPermission(...) == GRANTED) {
    PropertyManager mgr = new PropertyManager(this);
    mActivityLogger = new ActivityLogger(mgr.getSingularProperty(PropertyManager.DEVICE_ID_PROPERTY));
} else {
    mActivityLogger = new ActivityLogger();
    // Or even disable that logger altogether
}

Problem :

I want to ask for permission from the application class. How can I ask permission for accessing the device ID? I cannot ask for permission from activity as it crashes before starting the splash screen activity.
The following code is part of splash screen activity.

 @Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    checkpermi();

The code for checking permission

public  void checkpermi() {    
if(ContextCompat.checkSelfPermission(this,
Manifest.permission.ACCESS_FINE_LOCATION)
            + ContextCompat.checkSelfPermission(
            this,Manifest.permission.READ_PHONE_STATE)
            + ContextCompat.checkSelfPermission(
            this,Manifest.permission.WRITE_EXTERNAL_STORAGE)
            != PackageManager.PERMISSION_GRANTED){

        // Do something, when permissions not granted
        if(ActivityCompat.shouldShowRequestPermissionRationale(
                this,Manifest.permission.ACCESS_FINE_LOCATION)
                || ActivityCompat.shouldShowRequestPermissionRationale(
                this,Manifest.permission.READ_PHONE_STATE)
                || ActivityCompat.shouldShowRequestPermissionRationale(
                this,Manifest.permission.WRITE_EXTERNAL_STORAGE)){
            // If we should give explanation of requested permissions

            // Show an alert dialog here with request explanation
            AlertDialog.Builder builder = new AlertDialog.Builder(SplashScreenActivity.this);
            builder.setMessage("Location, Read Contacts and Write External" +
                    " Storage permissions are required to do the task.");
            builder.setTitle("Please grant those permissions");
            builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    ActivityCompat.requestPermissions(
                            SplashScreenActivity.this,
                            new String[]{
                                    Manifest.permission.ACCESS_FINE_LOCATION,
                                    Manifest.permission.READ_PHONE_STATE,
                                    Manifest.permission.WRITE_EXTERNAL_STORAGE
                            },

                            STORAGE_PERMISSION_CODE
                    );
                }
            });
            builder.setNeutralButton("Cancel",null);
            AlertDialog dialog = builder.create();
            dialog.show();
        }else{
            // Directly request for required permissions, without explanation
            ActivityCompat.requestPermissions(
                    this,
                    new String[]{
                            Manifest.permission.ACCESS_FINE_LOCATION,
                            Manifest.permission.READ_PHONE_STATE,
                            Manifest.permission.WRITE_EXTERNAL_STORAGE
                    },
                    STORAGE_PERMISSION_CODE
            );
        }
    }else {
        // Do something, when permissions are already granted
        Toast.makeText(SplashScreenActivity.this,"Permissions already granted",Toast.LENGTH_SHORT).show();
    }
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {

    //Checking the request code of our request
    if(requestCode == STORAGE_PERMISSION_CODE){

        //If permission is granted
        if(grantResults.length >0 && grantResults[0] == PackageManager.PERMISSION_GRANTED){

            //Displaying a toast
            Toast.makeText(this,"Permission granted now you can read the storage",Toast.LENGTH_LONG).show();
        }else{
            //Displaying another toast if permission is not granted
            Toast.makeText(this,"Oops you just denied the permission",Toast.LENGTH_LONG).show();
        }
    }
}

But instead of opening the splash screen activity the on create method overrides with the on create method of the Collect.java application class and the codes are.

@Override
public void onCreate() {
    singleton = this;

    super.onCreate();

    PropertyManager mgr = new PropertyManager(this);


    mActivityLogger = new ActivityLogger(

            mgr.getSingularProperty(PropertyManager.DEVICE_ID_PROPERTY));
}

If I cannot delete the above code because most of the buttons inside the app won’t work.

READ  [FIXED] android - Livedata inside fragment with view Model issue using kotlin
Powered by Inline Related Posts

And the error logs are.

2020-02-27 16:36:12.159 31359-31359/com.gic.spade.android E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.gic.spade.android, PID: 31359
java.lang.RuntimeException: Unable to create application com.gic.spade.android.application.Collect: java.lang.SecurityException: getDeviceId: Neither user 10366 nor current process has android.permission.READ_PHONE_STATE.
at android.app.ActivityThread.handleBindApplication(ActivityThread.java:6065)
at android.app.ActivityThread.-wrap1(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1764)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6944)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)
Caused by: java.lang.SecurityException: getDeviceId: Neither user 10366 nor current process has android.permission.READ_PHONE_STATE.
at android.os.Parcel.readException(Parcel.java:1959)
at android.os.Parcel.readException(Parcel.java:1905)
at com.android.internal.telephony.ITelephony$Stub$Proxy.getDeviceId(ITelephony.java:5333)
at android.telephony.TelephonyManager.getDeviceId(TelephonyManager.java:1069)
at com.gic.spade.android.logic.PropertyManager.(PropertyManager.java:135)
at com.gic.spade.android.application.Collect.onCreate(Collect.java:260)
at android.app.Instrumentation.callApplicationOnCreate(Instrumentation.java:1125)
at android.app.ActivityThread.handleBindApplication(ActivityThread.java:6062)
at android.app.ActivityThread.-wrap1(Unknown Source:0) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1764) 
at android.os.Handler.dispatchMessage(Handler.java:105) 
at android.os.Looper.loop(Looper.java:164) 
at android.app.ActivityThread.main(ActivityThread.java:6944) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374) 

Comments

Comment posted by Vishal

Please post your error logs

Comment posted by a_local_nobody

@VishalBari there probably aren’t any error logs because (i think) this isn’t possible

Comment posted by Request Permission from Application class

Does this answer your question?

Comment posted by Shraddha Sharma

I have posted the log error with my codes

Comment posted by Vishal

Can you check the intended functionality by giving permission manually? from Mobile settings.

Comment posted by Shraddha Sharma

it crashes before the app asks for permission

Comment posted by Vincent Mimoun-Prat

it will not crash if you don’t run the code which needs that permission before asking for it…

Android Tags:android, android-application-class, android-studio

Post navigation

Previous Post: [FIXED] Is it possible to switch from Start state to End state in android MotionLayout using Java/Kotlin?
Next Post: [FIXED] android – PHP / SQL: cannot save image at server but successful save image link at SQL database

Related Posts

[FIXED] How to send database table to another user in Android Android
[FIXED] android – Google Login: SignIn API Exception 10 only in the Play Store Android
[FIXED] android – Navigation between fragments using BottomNavigationView Android
[FIXED] java – Why my app crashes with NullPointerException Android
[FIXED] Android programmatically change background of Button Android
[FIXED] android – Developer options don’t save changes Android

Archives

  • March 2023
  • February 2023
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022

Categories

  • ¿Cómo
  • ¿Cuál
  • ¿Cuándo
  • ¿Cuántas
  • ¿Cuánto
  • ¿Qué
  • Android
  • Are
  • At
  • C'est
  • Can
  • Comment
  • Did
  • Do
  • Does
  • Est-ce
  • Est-il
  • For
  • Has
  • Hat
  • How
  • In
  • Is
  • Ist
  • Kann
  • Où
  • Pourquoi
  • Quand
  • Quel
  • Quelle
  • Quelles
  • Quels
  • Qui
  • Should
  • Sind
  • Sollte
  • Uncategorized
  • Wann
  • Warum
  • Was
  • Welche
  • Welchen
  • Welcher
  • Welches
  • Were
  • What
  • What's
  • When
  • Where
  • Which
  • Who
  • Who's
  • Why
  • Wie
  • Will
  • Wird
  • Wo
  • Woher
  • you can create a selvedge edge: You can make the edges of garter stitch more smooth by slipping the first stitch of every row.2022-02-04
  • you really only need to know two patterns: garter stitch

Recent Posts

  • What is a good substitute for condensed milk?
  • ¿Como el cuerpo te avisa cuando te va a dar un infarto?
  • What is the luxury brand of Jaguar?
  • Who is Big poppa baseball player?
  • What material are foam runners?

Recent Comments

No comments to show.

Copyright © 2023 Snappy1.

Powered by PressBook Grid Dark theme