Android App exits when request Runtime Permissions - android

In the program below I ask for SMS permission before sending a message.
At first App execution, when the message request is confirmed, the permission are checked and, if missing, a pop up requesting the user to allow the app is issued.
When this pop up is issued, the app is moved in background.
I found many topics about this issue but no fixes for my problem. E.g. the nohistory entry is missing in the manifest.
private void checkForSmsPermission() {
if (ActivityCompat.checkSelfPermission(this,
Manifest.permission.SEND_SMS) !=
PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.SEND_SMS},
MY_PERMISSIONS_REQUEST_SEND_SMS);
return;
} else {
// Permission already granted. Enable the SMS button.
return;
}
}
#Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
if (requestCode == MY_PERMISSIONS_REQUEST_SEND_SMS) {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Toast.makeText(this, "Permission GRANTED", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "Permission DENIED", Toast.LENGTH_SHORT).show();
}
}
}
public void send_command (String text){
checkForSmsPermission();
//Get the SmsManager instance and call the sendTextMessage method to send message
SmsManager sms=SmsManager.getDefault();
sms.sendTextMessage(recipient_number, null, text, null,null);
}

This is happening because you are calling following method anyways.
SmsManager sms=SmsManager.getDefault();
sms.sendTextMessage(recipient_number, null, text, null,null);
You should only call them if you are sure that you have the permission else your app will crash.
Its always recommended to call these method on success of permission validation. but in your case it will get call whether the user provided the permission or not.

Related

security threat warning when I use SmsManager

I have to create an android App that can send message automatically. I found the code that do what I want but this send me the following warning [it is a translation since my message it is not in english]:
security threat
AppName seems to be infected. We advise you to uninstall the app immediately.
The line that give me the warning is:
smsManager.sendTextMessage(phoneNo, null, message, null, null);
My code is:
public void sendSMSMessage() {
if (ContextCompat.checkSelfPermission(this,
Manifest.permission.SEND_SMS)
!= PackageManager.PERMISSION_GRANTED) {
if (ActivityCompat.shouldShowRequestPermissionRationale(this,
Manifest.permission.SEND_SMS)) {
} else {
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.SEND_SMS},
MY_PERMISSIONS_REQUEST_SEND_SMS);
}
}
}
#Override
public void onRequestPermissionsResult(int requestCode,String permissions[], int[] grantResults) {
switch (requestCode) {
case MY_PERMISSIONS_REQUEST_SEND_SMS: {
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(phoneNo, null, message, null, null);
Toast.makeText(getApplicationContext(), "SMS sent.",
Toast.LENGTH_LONG).show();
} else {
Toast.makeText(getApplicationContext(),
"SMS faild, please try again.", Toast.LENGTH_LONG).show();
return;
}
}
}
}
I want to point out that this code work. The only problem is that warning that I would like to eliminate.
I tried to send sms with intent but those method need human interaction and I do not want it (if possible).
Thanks in advance.
Google has changed its security policies for dangerous permissions. As per it, you cant allow your app to handle such events without human interactions as these are dangerous permissions.
Google Play restricts the use of high risk or sensitive permissions,
including the SMS or Call Log permission groups.
Check this link for further information on the same:
https://support.google.com/googleplay/android-developer/answer/9047303?hl=en

android Build.GetSerial() throwing exception

I'm developing a Xamarin.Android project,where I need to get the Device Serial number.
I have implemented it the way it is shown below.
Also added the permissions in the manifest file:
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.READ_PRIVILEGED_PHONE_STATE" />
string serial;
if (Build.VERSION.SdkInt >= BuildVersionCodes.O)
{
serial = Build.GetSerial();
}
else
{
serial = Build.Serial;
}
I have tried it on two different devices(both android 9.0).Sadly i get the following exception when the GetSerial() function is called(huawei p10): Java.Lang.SecurityException: <Timeout exceeded getting exception details> .
On an other device(galaxy s8) I get this exception:
Java.Lang.SecurityException: getSerial requires READ_PHONE_STATE or
READ_PRIVILEGED_PHONE_STATE permission
I really dont understand what the problem is,because I have added both permissions in the manifest,which the exception sais...
Any idea what am I doing wrong?
You should faced a persmission issue. And since Android Marshmallow, you need to ask the user for the permissions.
Besides adding the permission in android manifest file, you can also add runtime permissions like this:
static readonly int REQUEST_PHONE_STATE = 1;
public void checkPermission()
{
Log.Info(TAG, "Checking permission.");
// Check if the permission is already available.
if (ActivityCompat.CheckSelfPermission(this, Manifest.Permission.ReadPhoneState) != (int)Permission.Granted)
{
// permission has not been granted
RequestPhoneStatePermission();
}
else
{
// permissions is already available, show the camera preview.
Log.Info(TAG, " permission has already been granted.");
getInfo();
}
}
Method RequestPhoneStatePermission
private void RequestPhoneStatePermission()
{
Log.Info(TAG, "PhoneState permission has NOT been granted. Requesting permission.");
if (ActivityCompat.ShouldShowRequestPermissionRationale(this, Manifest.Permission.ReadPhoneState))
{
Log.Info(TAG, "Displaying PhoneState permission rationale to provide additional context.");
Snackbar.Make(layout, Resource.String.permission_phonestate_rationale,
Snackbar.LengthIndefinite).SetAction(Resource.String.ok, new Action<View>(delegate (View obj) {
ActivityCompat.RequestPermissions(this, new String[] { Manifest.Permission.ReadPhoneState }, REQUEST_PHONE_STATE);
})).Show();
}
else
{
// PhoneState permission has not been granted yet. Request it directly.
ActivityCompat.RequestPermissions(this, new String[] { Manifest.Permission.ReadPhoneState }, REQUEST_PHONE_STATE);
}
}
Method OnRequestPermissionsResult
public override void OnRequestPermissionsResult(int requestCode, string[] permissions, Permission[] grantResults)
{
if (requestCode == REQUEST_PHONE_STATE)
{
// Received permission result for camera permission.
Log.Info(TAG, "Received response for phone state permission request.");
// Check if the only required permission has been granted
if (grantResults.Length == 1 && grantResults[0] == Permission.Granted)
{
// Camera permission has been granted, preview can be displayed
Log.Info(TAG, "phonestate permission has now been granted. Showing preview.");
Snackbar.Make(layout, Resource.String.permission_available_phonestate, Snackbar.LengthShort).Show();
getInfo();
}
else
{
Log.Info(TAG, "phonestate permission was NOT granted.");
Snackbar.Make(layout, Resource.String.permissions_not_granted, Snackbar.LengthShort).Show();
}
}
else
{
base.OnRequestPermissionsResult(requestCode, permissions, grantResults);
}
}
method getInfo
private void getInfo() {
string serial;
if (Build.VERSION.SdkInt >= BuildVersionCodes.O)
{
serial = Build.GetSerial();
}
else
{
serial = Build.Serial;
}
Log.Info(TAG, "serial = " + serial);
}
Here is a full demo, you can check it.
After that, you can get the effect:
For more details,you can check:
https://learn.microsoft.com/en-us/xamarin/android/app-fundamentals/permissions?tabs=windows
https://devblogs.microsoft.com/xamarin/requesting-runtime-permissions-in-android-marshmallow/

How to make user must allow certain permission to continue using apps

I am developing an apps where permission to write internal storage is crucial. When I am requesting permission for api >23, there is an option to never show the request permission dialog again. I wanted to disable this because if the user choose to deny and check never show the request dialog again, then the user will have to personally go to settings and clear the apps reference himself, which I want to avoid. I also want to keep requesting for permission if the user choose deny, but resulting in error java.lang.ArrayIndexOutOfBoundsException in grantResults[0].
To summarize my problem:
1. how to disable the "never show again check"
2. my error's fix
here's my code in requesting permission again if the user choose deny:
#Override
public void onRequestPermissionsResult(int requestCode, #NonNull String[] permissions, #NonNull int[] grantResults) {
switch (requestCode) {
case PERMISSION_WRITE_EXTERNAL_STORAGE:
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Update();
} else {
ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, PERMISSION_WRITE_EXTERNAL_STORAGE);
}
break;
}
}
You can't stop the user from permanently denying the permission. When the user does an action that requires the permission, and the permission is denied, you can flash a message - a Toast or a Snackbar, for example.
try this my friend
String permission = Manifest.permission.WRITE_EXTERNAL_STORAGE;
int grant = ContextCompat.checkSelfPermission(this, permission);
if (grant != PackageManager.PERMISSION_GRANTED) {
String[] permission_list = new String[1];
permission_list[0] = permission;
ActivityCompat.requestPermissions(this, permission_list, 1);
}
#Override
public void onRequestPermissionsResult ( int requestCode, #NonNull String[] permissions,
#NonNull int[] grantResults){
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == 1) {
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Toast.makeText(AccountClass.this, "permission granted", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(AccountClass.this, "permission not granted", Toast.LENGTH_SHORT).show();
}
}
}

How to grant android.permission.RECORD_AUDIO permissions to android shell user?

I build some command line tool with android NDK and execute it in /data/local/tmp. Now it prompts me “requires android.permission.RECORD_AUDIO”. It's usually in AndroidManifest.xml for java application, but how to grant it to android shell user?
Source code like:
sp<AudioRecord> rec = new AudioRecord(AUDIO_SOURCE_MIC, 44100, AUDIO_FORMAT_PCM_16_BIT, AUDIO_CHANNEL_IN_MONO);
Logcat:
I/AudioFlinger( 3772): AudioFlinger's thread 0xf0e838c0 ready to run
W/ServiceManager( 3772): Permission failure:android.permission.RECORD_AUDIO from uid=2000 pid=-1
E/ ( 3772): Request requires android.permission.RECORD_AUDIO
E/AudioFlinger( 3772): openRecord() permission denied: recording not allowed
E/AudioRecord(14132): AudioFlinger could not create record track, status: -1
--------- beginning of crash
...
After Lollipop, Android platform is using real time permissions. If your app running in Marshmallow and after versions, you should grant your permissions in Activity.
Before request permissions:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.RECORD_AUDIO) ==
PackageManager.PERMISSION_GRANTED) {
// put your code for Version>=Marshmallow
} else {
if (shouldShowRequestPermissionRationale(Manifest.permission.RECORD_AUDIO)) {
Toast.makeText(this,
"App required access to audio", Toast.LENGTH_SHORT).show();
}
requestPermissions(new String[]{Manifest.permission.RECORD_AUDIO
}, REQUEST_CAMERA_PERMISSION_RESULT);
}
} else {
// put your code for Version < Marshmallow
}
After this, overraide this method and put your code:
#Override
public void onRequestPermissionsResult(int requestCode, #NonNull String[] permissions, #NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == REQUEST_AUDIO_PERMISSION_RESULT) {
if (grantResults[0] != PackageManager.PERMISSION_GRANTED) {
Toast.makeText(getApplicationContext(),
"Application will not have audio on record", Toast.LENGTH_SHORT).show();
}
}
}
private void requestRecordAudioPermission() {
String requiredPermission = Manifest.permission.RECORD_AUDIO;
// If the user previously denied this permission then show a message explaining why
// this permission is needed
if (getActivity().checkCallingOrSelfPermission(requiredPermission) == PackageManager.PERMISSION_GRANTED) {
} else {
Toast.makeText(getActivity(), "This app needs to record audio through the microphone....", Toast.LENGTH_SHORT).show();
requestPermissions(new String[]{requiredPermission}, 101);
}
}
#Override
public void onRequestPermissionsResult(int requestCode,
String permissions[], int[] grantResults) {
if (requestCode == 101 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// This method is called when the permissions are given
}
}
Execute this code if you have device with marshmallow or upper OS

Automatically detect to show allow permission after user deny in the first time asking

I working with anroid app in Android M and need some permission that allow to access CAMERA, RECORD_AUDIO, WRITE_EXTERNAL_STORAGE, ... so I put my permission checking when user open the app in the first time. But if user deny it, how can I detect it automatically that user is using the feature that required permission then ask them again? Or I have to put my condition in every feature that need permission.
I know when we use some feature that required permission but not allowed already it will throw the exception, so do we have any class that handle this task?
I use this logic: Every Activity extends a BaseActivity, in which there is a method that check the permissions everytime the onCreate() is called.
The method that I use is:
public static boolean hasPermissions(Context context, String... permissions) {
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && context != null && permissions != null) {
for (String permission : permissions) {
if (ActivityCompat.checkSelfPermission(context, permission) != PackageManager.PERMISSION_GRANTED) {
return false;
}
}
}
return true;
}
So, everytime the user launch a new Activity, the application check the permissions and display which permissions are not granted yet.
To manage the result, use onRequestPermissionsResult()
BTW, this logic will ask the user the permission even if the activity's feature doesn't require any permission.
If you want to ask the user the permissions only where the feature that requires a permission, you must check it in every activity
onRequestPermissionsResult you can check particular Permission is granted or not you can call Permission dialog again from there if particular permission not granted for Example
#Override
public void onRequestPermissionsResult(int requestCode, #NonNull String[] permissions,
#NonNull int[] grantResults) {
if (requestCode == REQUEST_CAMERA) {
// BEGIN_INCLUDE(permission_result)
// Received permission result for camera permission.
Log.i(TAG, "Received response for Camera permission request.");
// Check if the only required permission has been granted
if (grantResults.length == 1 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// Camera permission has been granted, preview can be displayed
Log.i(TAG, "CAMERA permission has now been granted. Showing preview.");
Snackbar.make(mLayout, R.string.permision_available_camera,
Snackbar.LENGTH_SHORT).show();
} else {
Log.i(TAG, "CAMERA permission was NOT granted.");
// Ask again for permission
}
// END_INCLUDE(permission_result)
} else if (requestCode == REQUEST_CONTACTS) {
Log.i(TAG, "Received response for contact permissions request.");
// We have requested multiple permissions for contacts, so all of them need to be
// checked.
if (PermissionUtil.verifyPermissions(grantResults)) {
// All required permissions have been granted, display contacts fragment.
Snackbar.make(mLayout, R.string.permision_available_contacts,
Snackbar.LENGTH_SHORT)
.show();
} else {
Log.i(TAG, "Contacts permissions were NOT granted.");
// Ask again for permission
}
} else {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
}

Categories

Resources