security threat warning when I use SmsManager - android

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

Related

Android App exits when request Runtime Permissions

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.

android airplane permission doesn't work properly - full example?

I've found a lot of examples here but no one works for me...I can't understand what's wrong.what I need is to enter the "airplane mode" in my app and I search for a full example.
What I have done is
1. request permission in manifest (I've seen I need "WRITE_SETTINGS" permission)
<uses-permission-sdk-m android:name="android.permission.WRITE_SETTINGS" />
2. in the code, when I want to change Airplane Mode, I check for permission
private void setAirplaneMode() {
boolean permission;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
permission = Settings.System.canWrite(this);
} else {
permission = ContextCompat.checkSelfPermission(this, android.Manifest.permission.WRITE_SETTINGS) == PackageManager.PERMISSION_GRANTED;
}
if (permission) {
//here I go to activate the airplane mode
setAirplaneModeIntent(true);
} else {
if (ActivityCompat.shouldShowRequestPermissionRationale(this, android.Manifest.permission.WRITE_SETTINGS)) {
//I need to explain why I want permission
//TODO
} else {
ActivityCompat.requestPermissions(this, new String[] android.Manifest.permission.WRITE_SETTINGS}, MY_PERMISSION_REQUEST_WRITE_SETTINGS);
}
}
3. I override the "onRequestPermissionsResult"
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResult) {
switch (requestCode) {
case MY_PERMISSION_REQUEST_WRITE_SETTINGS: {
if( grantResult.length > 0 &&
grantResult[0] == PackageManager.PERMISSION_GRANTED) {
setAirplaneModeIntent(true);
}
}
}
}
4. in the "setAirplaneModeIntent" I set the Airplane mode
private void setAirplaneModeIntent(boolean activateAirplaneMode) {
Settings.System.putInt(getContentResolver(), Settings.System.AIRPLANE_MODE_ON,
activateAirplaneMode ? 1 : 0);
Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);
intent.putExtra("state", activateAirplaneMode);
sendBroadcast(intent);
}
when I debug, I enter in the "ActivityCompat.requestPermissions" code but when I reach the "onRequestPermissionsResult" the result is not PackageManager.PERMISSION_GRANTED.
What's wrong?
EDIT
I solve the "requestPermissions" part. For the "WRITE_SETTINGS" permission you need the ACTION_MANAGE_WRITE_SETTINGS intent.
See here:
https://developer.android.com/reference/android/provider/Settings.html#ACTION_MANAGE_WRITE_SETTINGS
https://developer.android.com/reference/android/Manifest.permission.html#WRITE_SETTINGS
http://stackoverflow.com/questions/39224303/write-settings-permission-not-granted-marshmallow-android
Anyway I got an error in the point 4, when I send the broadcast.
If I don't sent the broadcast, the "Settings.System.putInt" command seems doing nothing.
Someone can help me?
EDIT 2
I surrender. The only solution I have found that works is to open the network settings in this way
Intent intentAirplaneMode = new Intent(Settings.ACTION_AIRPLANE_MODE_SETTINGS);
intentAirplaneMode.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intentAirplaneMode);

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);
}
}

Android permissions READ_CONTACT [duplicate]

This question already has answers here:
Android permission doesn't work even if I have declared it
(11 answers)
Closed 6 years ago.
Why i get this message
if the i set the permission in the manifest file:
Thank you for any help.
You need request permission at runtime in android >= 6.0
https://developer.android.com/training/permissions/requesting.html
You have to grant permission in android version >=6.0 (Marshmallow and above).
For Android 6.0+, you have to request the permission at runtime as well as adding it to the manifest. So something like this added to your Activity onCreate();
List<String> permissions = new ArrayList<>();
permissions.add(Manifest.permission.READ_CONTACTS);
//...add any others you need to this arraylist
// Convert the List to an Array
String[] permissionsArray = permissions.toArray(new String[0]);
ActivityCompat.requestPermissions(getActivity(), permissionsArray, REQUEST_CODE);
//The REQUEST_CODE is just a integer that your callback method will check, set it to some number
Then override this method in your activity to check whether the request was granted or not
#Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
switch (requestCode) {
case REQUEST_CODE: {
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
//Permission Granted, do your thing
} else {
//Permission denied, you can't do what you want to because user didn't give you permission
}
return;
}
}
See here for more info https://developer.android.com/training/permissions/requesting.html
Change your target sdk version to 22 in your app's build.gradle if you donot want to request the permission to the user at run time.
Else if you want to keep the target sdk version to 23 then you would need to request the permission from the user at run time as mentioned above by #wanpanman
Call this permission.
//Check permissions
public boolean reqPermission() {
if (!Permissions.checkPermission(this, Manifest.permission.READ_CONTACTS)) {
if (shouldShowRationale(Manifest.permission.READ_CONTACTS)) {
Snackbar.make(yourLayout, "Need to enable contact permission to use this feature. bla bla bla",
Snackbar.LENGTH_INDEFINITE)
.setAction(R.string.settings, new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent();
intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
Uri uri = Uri.fromParts("package", getPackageName(), null);
intent.setData(uri);
startActivity(intent);
}
})
.show();
return true;
} else {
Permissions.grantPermission(this, PERMISSION_REQUEST_CODE);
return true;
}
} else {
return false;
}
}
#TargetApi(Build.VERSION_CODES.M)
public boolean shouldShowRationale(String permission) {
return shouldShowRequestPermissionRationale(permission);
}

Lollipop : Accept incoming call Programatically in android lollipop

I'm using the below code to answer an incoming call from my app(BroadcastReceiver's onReceive()) , it is working in Kitkat . The same code is not working in Lollipop.
Intent intent = new Intent(Intent.ACTION_MEDIA_BUTTON);
intent.putExtra(Intent.EXTRA_KEY_EVENT, new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_HEADSETHOOK));
// send ordered broadcast
context.sendOrderedBroadcast(intent, null);
Please let me know how can I answer a call in Lollipop.
Thank you.
This worked for me.Put this code in your broadcast reciever for action "android.intent.action.PHONE_STATE" .Your phone needs to be rooted.Generate an apk file of ur app and put it into /system/priv-apps/ .Works for Android v 5.0 i.e lollipop.
final String LOG_TAG = "TelephonyAnswer";
TelephonyManager tm = (TelephonyManager) context
.getSystemService(Context.TELEPHONY_SERVICE);
try {
if (tm == null) {
// this will be easier for debugging later on
throw new NullPointerException("tm == null");
}
// do reflection magic
tm.getClass().getMethod("answerRingingCall").invoke(tm);
} catch (Exception e) {
}
Do not forget to add permission in ur manifest.
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<uses-permission android:name="android.permission.MODIFY_PHONE_STATE"/>
if modify_phone_state doesnt work explicitly ask for one by using this code
ActivityCompat.requestPermissions(MainActivity.this,
new String[]{Manifest.permission.MODIFY_PHONE_STATE},
1);
and overide method
#Override
public void onRequestPermissionsResult(int requestCode,
String permissions[], int[] grantResults) {
switch (requestCode) {
case 1: {
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
} else {
Toast.makeText(MainActivity.this, "Permission denied to pick call", Toast.LENGTH_SHORT).show();
}
return;
}
}
}
From Lollipop, if our application is System app or application has root access then only we can programmatically answer incoming call
For 3rd party developer application, Lollipop OS do not allow to
answer programmatically to incoming call
You can check if how to do it for system app or with root access : In this Answer

Categories

Resources