After accept permission how to open a activity? - java

I trying to do a simple functionality. After the user accept permission for camera i need to open a new activity, but how do this?
This is a code. I check the permission and the request it. On my else if i open a new activity.
#RequiresApi(api = Build.VERSION_CODES.M)
#OnClick(R.id.scanButton)
void scanZxing(View view) {
if(getActivity().checkSelfPermission(Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED){
Toast.makeText(context, "We need permissions to acces your CAMERA!", Toast.LENGTH_SHORT).show();
((MainActivity) context).checkCameraPermission();
} else if (view.getId() == R.id.scanButton) {
Intent intent = new Intent(getActivity(), ScanBarcodeActvity.class);
startActivityForResult(intent, 0);
}
}
#TargetApi(23)
public void checkCameraPermission(){
if(Build.VERSION.SDK_INT < Build.VERSION_CODES.M){
return;
}
if (this.checkSelfPermission(Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
requestPermissions(new String[] {Manifest.permission.CAMERA}, REQUEST_CODE_CAMERA);
}
}
#Override
public void onRequestPermissionsResult(int requestCode, #NonNull String[] permissions, #NonNull int[]
grantResults) {
switch (requestCode) {
case REQUEST_CODE_CAMERA:
if(grantResults[0] == PackageManager.PERMISSION_GRANTED){
Toast.makeText(this, "Thanks for your permission", LENGTH_SHORT).show();
} else {
Toast.makeText(this, "We need your permission to open camera",
LENGTH_SHORT).show();
}
default:
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
break;
}
}

You can check the permission request result in your activities onRequestPermissionsResult methodm and if user granted the permission then open the activity
#Override
public void onRequestPermissionsResult(int requestCode, #NonNull String[] permissions, #NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == REQUEST_CODE_CAMERA &&
grantResults[0] == PackageManager.PERMISSION_GRANTED) {
//permisssion was granted
} else {
//permisssion was not granted
}
}

I am supposing you want to open ScanBarcodeActvity.
you can do it in following way
public void openScanBarCodeAcitvity() {
Intent intent = new Intent(getActivity(), ScanBarcodeActvity.class);
startActivityForResult(intent, 0);
}
Call above method at following place in addition to scanZxing() method
#Override
public void onRequestPermissionsResult(int requestCode, #NonNull String[] permissions, #NonNull int[]
grantResults) {
switch (requestCode) {
case REQUEST_CODE_CAMERA:
if(grantResults[0] == PackageManager.PERMISSION_GRANTED){
Toast.makeText(this, "Thanks for your permission", LENGTH_SHORT).show();
//permission granted, open activity
openScanBarCodeAcitvity();
} else {
Toast.makeText(this, "We need your permission to open camera",
LENGTH_SHORT).show();
}
default:
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
break;
}
}

Related

call by phone number on button click (java)

I have a call method on button click
button_call=(Button) findViewById(R.id.button_call);
button_call.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent=new Intent(Intent.ACTION_DIAL);
intent.setData(Uri.parse("tel:+79142214017"));
startActivity(intent);
}
});
And the data transfer method in Firebase
private void validateClientInfo() {
if (TextUtils.isEmpty(client_edit.getText().toString())) {
Toast.makeText(this, "Введите имя", Toast.LENGTH_SHORT).show();
}
if (TextUtils.isEmpty(client_number.getText().toString())){
Toast.makeText(this, "Введите номер", Toast.LENGTH_SHORT).show();
}
else {
HashMap<String, Object> userMap = new HashMap<>();
userMap.put("uid", mAuth.getCurrentUser().getUid());
userMap.put("numberphone_client",client_number.getText().toString());
clientRef.child(mAuth.getCurrentUser().getUid()).updateChildren(userMap);
startActivity(new Intent(ClientName.this,HomeActivity.class));
}
}
transcripts
client_number=(EditText) findViewById(R.id.client_number);
mAuth=FirebaseAuth.getInstance();
how to make it so that when the call button is pressed, the number is received and called?
I want that when the button _call button is pressed, the data transmitted by the transfer method is received and a call is made on them.
Request permission android.permission.CALL_PHONE before calling.
Add into AndroidManifest.xml:
<uses-permission android:name="android.permission.CALL_PHONE" />
Request permission before calling:
boolean isPermissionGranted = (ContextCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE) == PackageManager.PERMISSION_GRANTED);
if (isPermissionGranted) {
Intent intent = new Intent(Intent.ACTION_CALL);
intent.setData(Uri.parse("tel:" + mNumber));
startActivity(intent);
} else {
Toast.makeText(this, "Отсутствует разрешение на звонок с устройства", Toast.LENGTH_SHORT).show();
ActivityCompat.requestPermissions(this, new String[]{ Manifest.permission.CALL_PHONE }, 0);
}
Add request result:
#Override
public void onRequestPermissionsResult(int requestCode, #NonNull #NotNull String[] permissions, #NonNull #NotNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == 0) {
for (int result : grantResults) {
if (result != PackageManager.PERMISSION_GRANTED) {
//RETURN, PERMISSION NOT GRANTED
Toast.makeText(this, "Вы не выдали разрешение, приложение может работать неккоректно!", Toast.LENGTH_SHORT).show();
return;
}
}
//PERMISSIONS GRANTED
Toast.makeText(this, "Спасибо за выданное разрешение!", Toast.LENGTH_SHORT).show();
}
}
If your are looking to call the selected number then just use the following code.
Intent intentCallForward = new Intent(Intent.ACTION_DIAL); // ACTION_CALL
Uri uri2 = Uri.fromParts("tel", "79142214017, "#");
intentCallForward.setData(uri2);
startActivityForResult(intentCallForward, 101);

permission in java android grandResult

I am really looking for the use of this condition (grandResult.lenght() > 0) in android permission
#Override
public void onRequestPermissionsResult(int requestCode, #NonNull String[] permissions, #NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
switch (requestCode) {
case WRITE_EXT_RQS_CODE:
if (grandResult.lenght() > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
createFile();
Toast.makeText(this, "permission is granted , length : " + grantResults.length, Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "permission is denied length : " + grantResults.length, Toast.LENGTH_SHORT).show();
}
}
}
I am not understanding why grandResult.length > 0 is written in this condition,
android said that when canceling the request will return to the grandResult array 0,
but when I click on deny it return 1.
the question is why we put grandResult.length() > 0 condition.

How to add files access permission on device android

My problem is when I am not getting files permission in the device.
Below image is showing: photos and media access are showing but files access not showing.
In the above permission access files permission not showing like below: I need below permissions including files access.
I use permision in the manifest:
<uses-permission
android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission
android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission
android:name="android.permission.CAMERA" />
From the MainActivity:
private static final int CAMERA_PERMISSION_CODE = 100;
private static final int STORAGE_PERMISSION_CODE = 101;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
storage = findViewById(R.id.storage);
camera = findViewById(R.id.camera);
// Set Buttons on Click Listeners
storage.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v)
{
checkPermission(
Manifest.permission.WRITE_EXTERNAL_STORAGE,
STORAGE_PERMISSION_CODE);
}
});
camera.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v)
{
checkPermission(Manifest.permission.CAMERA,
CAMERA_PERMISSION_CODE);
}
});
}
// Function to check and request permission.
public void checkPermission(String permission, int requestCode)
{
if (ContextCompat.checkSelfPermission(MainActivity.this, permission)
== PackageManager.PERMISSION_DENIED) {
// Requesting the permission
ActivityCompat.requestPermissions(MainActivity.this,
new String[] { permission },
requestCode);
}
else {
Toast.makeText(MainActivity.this,
"Permission already granted",
Toast.LENGTH_SHORT)
.show();
}
}
#Override
public void onRequestPermissionsResult(int requestCode,
#NonNull String[] permissions,
#NonNull int[] grantResults)
{
super
.onRequestPermissionsResult(requestCode,
permissions,
grantResults);
if (requestCode == CAMERA_PERMISSION_CODE) {
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Toast.makeText(MainActivity.this,
"Camera Permission Granted",
Toast.LENGTH_SHORT)
.show();
}
else {
Toast.makeText(MainActivity.this,
"Camera Permission Denied",
Toast.LENGTH_SHORT)
.show();
}
}
else if (requestCode == STORAGE_PERMISSION_CODE) {
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Toast.makeText(MainActivity.this,
"Storage Permission Granted",
Toast.LENGTH_SHORT)
.show();
}
else {
Toast.makeText(MainActivity.this,
"Storage Permission Denied",
Toast.LENGTH_SHORT)
.show();
}
}
}
}
#Override
public void onRequestPermissionsResult(int requestCode,
#NonNull String[] permissions,
#NonNull int[] grantResults)
{
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == STORAGE_PERMISSION_CODE) {
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Toast.makeText(MainActivity.this,
"Storage Permission Granted",
Toast.LENGTH_SHORT)
.show();
}
else {
Toast.makeText(MainActivity.this,
"Storage Permission Denied",
Toast.LENGTH_SHORT)
.show();
}
}
}
Changing the targetSdkVersion from 30 to 29 in my build.gradle file changed the dialog for me from photos/media to photos/media/files. I believe it has something to do with how API 30 is more strict with scoped storage.
Adding the below line in the Application portion of Manifest
android:requestLegacyExternalStorage="true"
solves the problem.

StartActivity once user click Allow on Permission Screen

My one activity need Location Permission and I wrote below code to get permission. But in this case user need to click twice to open activity if app does not have location permission initially. Can I make some change so that once user click allow on Permission screen, only then intent fires.
int PERMISSION_ALL = 1;
String[] PERMISSIONS = new String[0];
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
PERMISSIONS = new String[]{
Manifest.permission.ACCESS_FINE_LOCATION};
}
if (!hasPermissions(getApplicationContext(), PERMISSIONS)) {
ActivityCompat.requestPermissions(StartupActivity.this, PERMISSIONS, PERMISSION_ALL);
}
if (hasPermissions(getApplicationContext(), PERMISSIONS)) {
new Thread(new Runnable() {
#Override
public void run() {
Intent intent = new Intent(StartActivity.this, Details.class);
startActivity(intent);
}
}).start();
}
override onRequestPermissionsResult and if the permission is granted then start your Activity from there
#Override
public void onRequestPermissionsResult(int requestCode, #NonNull String[] permissions, #NonNull int[] grantResults) {
if(requestCode == PERMISSION_ALL){
if(grantResults.length>0 && grantResults[0]==PackageManager.PERMISSION_GRANTED ){
new Thread(new Runnable() {
#Override
public void run() {
Intent intent = new Intent(StartActivity.this, Details.class);
startActivity(intent);
}
}).start();
}else{
Toast.makeText(MainActivity.this, "Access Denied ! Cannot proceed further ", Toast.LENGTH_SHORT).show();
}
}
}
Note : Apparently , the code seems like in StartupActivity (from StartupActivity.this ) so you don't required to create a thread , simply start your Details Activity with simple Intent
Code will be
class StartupActivity extends ..{
onCreate...(){}
// here you will have onRequestPermissionsResult
#Override
public void onRequestPermissionsResult( ....){...}
}
Yes, you need to override onRequestPermissionsResult
First on onCreate
int PERMISSION_ALL = 1;
String[] PERMISSIONS = new String[0];
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
PERMISSIONS = new String[]{
Manifest.permission.ACCESS_FINE_LOCATION};
}
if (!hasPermissions(getApplicationContext(), PERMISSIONS)) {
ActivityCompat.requestPermissions(StartupActivity.this, PERMISSIONS, PERMISSION_ALL);
}
then Override
#Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
if (requestCode == PERMISSION_ALL) {
Intent intent = new Intent(StartActivity.this,Details.class);
startActivity(intent);
}
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
#Override
onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults)
{
// Handle your permission grant/reject scenarios here,
for( int x = 0; x < grantResults.length; x++ )
{
if( grantResults[x] == PackageManager.PERMISSION_GRANTED )
{
// Since you had one, start your new activity here
}
else
{
// Do something with the permissions that got BLOCKED??
}
}
}
This will be internally called by android, once user grants/rejects a permission

Displaying a TextView depending on whether Android permission is disabled?

I'm trying to display a TextView which appears visible only when a particular Android permission, i.e. WRITE_EXTERNAL_STORAGE, is not enabled. The idea is to explain to the user why they need to enable it in case they've disabled it the first time, and then have them click the TextView to launch the permission request again. The following is not working for me. How can I accomplish this? I have this in my onCreate() method:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && (checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED)) {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
} else {
if (NetworkHelper.isOnline(this)) {
new AppendParseData().execute();
}
}
TextView checkFilePermission = (TextView) findViewById(R.id.checkFilePermission);
if (!checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
checkFilePermission.setVisibility(View.VISIBLE);
}
try this way when you request for permission(I have used READ_CONTACTS permission you can use WRITE_EXTERNAL_STORAGE instead)
if (ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.READ_PHONE_STATE)
!= PackageManager.PERMISSION_GRANTED) {
requestContactsPermissions1();
} else {
setTextVisablity(false);
checkSimCard();
}
in onRequestPermissionsResult
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
switch (requestCode) {
case REQUEST_CODE_ASK_PERMISSIONS:
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// Permission Granted
readContacts();
setTextVisablity(false);
} else {
// Permission Denied
setTextVisablity(true);
Toast.makeText(MainActivity.this, "READ_CONTACTS Denied", Toast.LENGTH_SHORT)
.show();
}
break;
default:
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
}
in the end setTextVisablity method
public void setTextVisablity(boolean isShow) {
if (isShow) {
tv.setVisibility(View.VISIBLE);
} else {
tv.setVisibility(View.GONE);
}
}
Use this code:-
int hasWriteContactsPermission = ContextCompat.checkSelfPermission(this,Manifest.permission.WRITE_EXTERNAL_STORAGE);
if (hasWriteContactsPermission != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this,new String[] {Manifest.permission.WRITE_EXTERNAL_STORAGE},1);
return;
}
else{
//Permission already granted show textview here
}
#Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
switch (requestCode) {
case 1:
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// Permission Granted
Toast.makeText(MainActivity.this, " PERMISSION_GRANTED", Toast.LENGTH_SHORT)
.show();
} else {
// Permission Denied
Toast.makeText(MainActivity.this, " Denied", Toast.LENGTH_SHORT)
.show();
}
break;
default:
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
}

Categories

Resources