DataStream3
DataStream3

Reputation: 43

Problems with checking for WRITE_SECURE_SETTINGS permission in my app

I'm fairly new to Android, and Javascript. But, I'm actively learning by watching tutorials, and asking questions. Now, I'm trying to code an app that changes certain system values. I know this can only be done by granting my app the WRITE_SECURE_SETTINGS permission. Through my research I've realized I cannot declare the permission in the manifest file itself. Obviously, the only respectable answer is to check if the permission has been granted by the user through ADB. I can't find anything related to this topic and I'm hoping you guys can help me come to a conclusion on how to properly do this. So far this is the code that I have.

package com.datastream.settingschanger;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    String requiredPermission = "android.permission.WRITE_SECURE_SETTINGS";
    int checkVal = getContext().checkCallingOrSelfPermission(requiredPermission);
    if (checkVal == PackageManager.PERMISSION_GRANTED) {
    }
    Toast.makeText(getActivity(), "WRITE_SECURE_SETTINGS has been granted to the application. You may now continue!",
            Toast.LENGTH_LONG).show();

    if (checkVal == PackageManager.PERMISSION_DENIED) {
    }
    Toast.makeText(getActivity(), "WRITE_SECURE_SETTINGS has not been granted to the application. Please grant access to continue.",
            Toast.LENGTH_LONG).show();
}
}

But, when ever I run this in Android Studio I get the following errors:

error: cannot find symbol method getContext()
error: cannot find symbol method getActivity()
error: cannot find symbol method getActivity()

Can anyone help me resolves these errors? I have no clue how to fix this. Thank you!

Upvotes: 2

Views: 1071

Answers (1)

AskNilesh
AskNilesh

Reputation: 69689

The getContext() and getActivity() method is used in Fragment to get Context

Remove getContext() and getActivity() And use this and MainActivity.this to get Context in your activity

Use this

int checkVal = checkCallingOrSelfPermission(requiredPermission);

instead of this

int checkVal = getContext().checkCallingOrSelfPermission(requiredPermission);

SAMPLE CODE

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    String requiredPermission = "android.permission.WRITE_SECURE_SETTINGS";
    int checkVal = checkCallingOrSelfPermission(requiredPermission);
    if (checkVal == PackageManager.PERMISSION_GRANTED) {
    }
    Toast.makeText(this, "WRITE_SECURE_SETTINGS has been granted to the application. You may now continue!",
            Toast.LENGTH_LONG).show();

    if (checkVal == PackageManager.PERMISSION_DENIED) {
    }
    Toast.makeText(this, "WRITE_SECURE_SETTINGS has not been granted to the application. Please grant access to continue.",
            Toast.LENGTH_LONG).show();
}
}

Upvotes: 4

Related Questions