uid does not have android.permission.SEND_SMS [duplicate] - java

This question already has answers here:
Android permission doesn't work even if I have declared it
(11 answers)
Closed 1 year ago.
I'm trying to send a message (SMS) from one emulator to another:
AndroidManifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="eheio.com.exo2">
<uses-permission android:name="android.permission.SEND_SMS"/>
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Send message method:
public void sendMessage(View view) {
EditText number = findViewById(R.id.number);
EditText message = findViewById(R.id.message);
try {
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(number.getText().toString(), null, message.getText().toString(), null, null);
Toast.makeText(getApplicationContext(), "SMS Sent!", Toast.LENGTH_LONG).show();
} catch (Exception e) {
Toast.makeText(getApplicationContext(), "SMS failed, please try again later!", Toast.LENGTH_LONG).show();
e.printStackTrace();
}
}
I'm getting the following exception:
java.lang.SecurityException: Sending SMS message: uid 10082 does not have android.permission.SEND_SMS.

For starters, you can inspect what permissions are granted to your apps by doing:
adb shell dumpsys package <your package>
and you'll see something like:
grantedPermissions:
android.permission.MANAGE_ACCOUNTS
android.permission.WRITE_SYNC_SETTINGS
android.permission.RECEIVE_BOOT_COMPLETED
The android.permission.SEND_SMS permission is dangerous protection level. That means as of API 23 you need to prompt the user to accept the permission. It's not enough to declare its use in the manifest. You can read about that here:
https://developer.android.com/guide/topics/permissions/overview#dangerous-permission-prompt

The android.permission.SEND_SMS is dangerous protection level. So It's not enough to declare <uses-permission android:name="android.permission.SEND_SMS"/> in the manifest.
So, above API level 23 you need to prompt the user to accept the permission, when the app is running.
You can try the following code:
SmsManager smsManager = SmsManager.getDefault();
ActivityCompat.requestPermissions(this,new String[] { Manifest.permission.SEND_SMS}, 1);
smsManager.sendTextMessage(strPhoneNo, null, strMessage, null, null);

Related

Query all images from phone works on api 18 but not on api 30

Solved by requesting permissions again at runtime
Recently I was working on my app and I wanted to query all images on my phone so I read Android developers tutorial
And could retrieve all images normally on a phone with api 18, but when I tried it on a phone with api 30 it didn't retrieve even one image though it's the same application.
I even later tried their code to get videos but it also didn't work, so I'm now don't know what's missing, any advice would be really appreciated :(
My query code :
public ArrayList<CustomPhoto> getPhotosFromExternalStorage() {
Uri collection;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
collection = MediaStore.Images.Media.getContentUri(MediaStore.VOLUME_EXTERNAL);
} else {
collection = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
}
String[] projection = new String[]{
MediaStore.Images.Media._ID,
MediaStore.Images.Media.DISPLAY_NAME,
MediaStore.Images.Media.BUCKET_DISPLAY_NAME
};
String sortOrder = MediaStore.Images.Media.DATE_ADDED + " ASC";
ArrayList<CustomPhoto> queryResult = new ArrayList<>();
try (Cursor cursor = context.getApplicationContext().getContentResolver().query(
collection,
projection,
null,
null,
sortOrder
)) {
int idColumn = cursor.getColumnIndexOrThrow(MediaStore.Images.Media._ID);
int nameColumn =
cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DISPLAY_NAME);
int bucketColumn = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.BUCKET_DISPLAY_NAME);
Log.e("retrieved = " , Integer.valueOf(cursor.getCount()).toString());
while (cursor.moveToNext()) {
// Get values of columns for a given video.
long id = cursor.getLong(idColumn);
String name = cursor.getString(nameColumn);
String bucket_display_name = cursor.getString(bucketColumn);
Uri contentUri = ContentUris.withAppendedId(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI, id);
Log.e("uri" , bucket_display_name);
queryResult.add(new CustomPhoto(contentUri.toString(),bucket_display_name, name));
}
} catch (IllegalArgumentException e) {
e.printStackTrace();
}
return queryResult;
}
My manifest :
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.pretest">
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"
/>
<!-- android:hardwareAccelerated="false"
android:largeHeap="true"-->
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/Theme.FacebookTest">
<meta-data android:name="com.facebook.sdk.ApplicationId" android:value="#string/facebook_app_id"/>
<meta-data android:name="com.facebook.sdk.ClientToken" android:value="#string/facebook_client_token"/>
<activity android:name="com.facebook.FacebookActivity"
android:configChanges=
"keyboard|keyboardHidden|screenLayout|screenSize|orientation"
android:label="#string/app_name" />
<activity
android:name="com.facebook.CustomTabActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="#string/fb_login_protocol_scheme" />
</intent-filter>
</activity>
<activity
android:name="com.example.pretest.MainActivity"
android:label="#string/app_name"
android:theme="#style/Theme.FacebookTest.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Solution was by asking for permissions again at runtime so that is my code that worked for me after reading the following tutorial Request app permissions at runtime:
First initialize an ActivityResultLauncher and register it as an activity result which must be before {initialization, onAttach(), or onCreate()} fragment creation methods, so the only way was to initialize it as a global fragment variable
Then when I need to start querying images from external storage, I use the ActivityResultLauncher I initialized before to launch a request for permissions needed.
If permissions was granted the callback should run the method responsible for getting photos, else (temporarily) it will show a toast message telling user why he can't continue with his action
My fragment code :
public class myFragment extends Fragment {
ActivityResultLauncher<String> requestPermissionLauncher =
registerForActivityResult(new ActivityResultContracts.RequestPermission(), isGranted -> {
if (isGranted) goFetchPhotos();
else
Toast.makeText(getContext(), "Can't continue without the required permissions", Toast.LENGTH_LONG).show();
});
//this function runs when something happens in my app
public void getNecessaryPermissionsAndFetchPhotos() {
if (ActivityCompat.checkSelfPermission(getContext(),
Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
requestPermissionLauncher.launch(Manifest.permission.READ_EXTERNAL_STORAGE);
}else goFetchPhotos();
}

MediaPlayer getDataSource error permission acces file [duplicate]

This question already has answers here:
Android permission doesn't work even if I have declared it
(11 answers)
Storage permission error in Marshmallow
(12 answers)
Closed 2 years ago.
I try to play a music with MediaPlayer.
The problem is I don't succed to acces file even if I ask for permissions in manifest
My Manifest:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.nkm.metaextract" >
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_INTERNAL_STORAGE"/>
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme" >
<activity android:name=".MainActivity" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
and the java code:
public class MainActivity extends AppCompatActivity {
//public class MetaExtractActivity extends Activity {
ImageView album_art;
TextView album, artist, genre;
MediaPlayer mp;
MediaMetadataRetriever metaRetriever;
byte[] art;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getInit();
File dir =Environment.getExternalStorageDirectory();
File file=new File(dir + File.separator +"Music/Wholesome.mp3");
if (file.exists()) {
genre.setText(file.toString());
Log.i("DEBUG", "Trouvé "+file.toString());
}else {genre.setText("pas trouvé");
Log.i("DEBUG", "pas trouvé");
}
mp = new MediaPlayer();
try {
mp.setDataSource("/storage/emulated/0/Music/Wholesome.mp3");
Log.i("DEBUG", "ok");
artist.setText("ok");
} catch (IOException e) {
e.printStackTrace();
Log.i("DEBUG", "Erreur "+ e);
artist.setText("pas trouvé");
}
Nota: the file exist and i can read it with store apps
I tried to change de path whit "/storage/emulated/0/Music/Wholesome.mp3" and few other way without change
Where i test if it's a file I obtain:
"I/DEBUG: Trouvé /storage/emulated/0/Music/Wholesome.mp3" but just next
"W/System.err: java.io.FileNotFoundException: /storage/emulated/0/Music/Wholesome.mp3 (Permission denied)"
and where i try to acces I obtain:
"I/DEBUG: Erreur java.io.FileNotFoundException: /storage/emulated/0/Music/Wholesome.mp3 (Permission denied)"
When app start, Android doesn't ask me for acces...and app can't acces file.
is somebody can help ?
Since Android 6.0 you have to ask for the permission in the runtime:
https://developer.android.com/training/permissions/requesting
What you can do for now, is to go to settings of the app, go to permission section, enable those permissions and restart the app. If it doesn't work, I would add runtime permission checks from the link I posted above
Thank you very much Mariusz.
I add
requestPermissions(new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, 1);
and it wors well...

unable to send SMS on android using code I wrote

I am trying To build my first application which will send ans SMS message.
everything looks OK:
1. SMS application opens
2. The URI is inserted
3. Text message is typed
Only the last bit of pressing the "SEND" button is not performed.
I am using the code demonstrated here on youtube:
which includes this AndroidManifest.XML
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="il.ac.ruppin.reco.www.sendsmsyoutube">
<uses-permission android:name="android.permission.SEND_SMS"/>
<uses-permission android:name="android.permission.SENR_RESPONSE_VIA_MESSAGE"/>
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
and this MainActivity.java
package il.ac.ruppin.reco.www.sendsmsyoutube;
import android.content.Intent;
import android.net.Uri;
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);
Uri uri = Uri.parse("smsto:+972528524520");
Intent intent = new Intent(Intent.ACTION_SENDTO,uri);
intent.putExtra("sms_body","Message from my new application");
startActivity(intent);
}
}
Thanks for your help
The correct URI format for SMS is sms: (and not smsto:)
String number = "+972528524520"
Uri uri = Uri.parse("sms:" + number);
Intent intent = new Intent(Intent.ACTION_SENDTO, uri);
intent.putExtra("sms_body", "Message from my new application");
startActivity(intent);
This starts the default activity for sending an SMS message.
You can try it with the SmsManager Class
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(phoneNo, null, sms, null, null)
where,
phoneNo = send To , sms = your message to be passed
Ok according to Hardik Vegad answer I used the SmsManager.
What I had to do is to allow the application to send sms.
Setting>apps>MyAp>permissions
Thanks for your help

BroadcastReceiver for Incoming calls in android doesn't work

I'm trying to develop android app for that can record phone calls. So, in the initial step, I've to see if BroadcastReceiver is getting fired or not.
I've added permissions, receiver tag in AndroidManifest file. I'm testing on OnePlus X. Activity is gets started but BroadcastReceiver doesn't get fired when I get call. What's going wrong here?
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.myapp">
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity
android:name="com.example.myapp.MainActivity"
android:label="#string/app_name"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name=".PhoneStateReceiver">
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE" />
</intent-filter>
</receiver>
</application>
</manifest>
PhoneStateReceiver.Java
package com.example.myapp;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.telephony.TelephonyManager;
import android.widget.Toast;
public class PhoneStateReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
try {
System.out.println("Receiver start");
String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
String incomingNumber = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);
if(state.equals(TelephonyManager.EXTRA_STATE_RINGING)){
Toast.makeText(context,"Incoming Call State",Toast.LENGTH_SHORT).show();
Toast.makeText(context,"Ringing State Number is -"+incomingNumber,Toast.LENGTH_SHORT).show();
}
if ((state.equals(TelephonyManager.EXTRA_STATE_OFFHOOK))){
Toast.makeText(context,"Call Received State",Toast.LENGTH_SHORT).show();
}
if (state.equals(TelephonyManager.EXTRA_STATE_IDLE)){
Toast.makeText(context,"Call Idle State",Toast.LENGTH_SHORT).show();
}
}
catch (Exception e){
e.printStackTrace();
}
}
}
The permission READ_PHONE_STATE is a dangerous permission, if you are on a marshmallow device you must request runtime permission else your broadcast receiver will not work neither it will throw an error.
That is the most likely the cause of issue from your code because you have correctly registered in the Intent filter other than that there is nothing wrong as it is just a broadcast receiver and should work, i.e get called by the Android system.

Block SMS in android

i developing an application where i want to block SMS of some specific numbers.For testing i do SMS from that number that i have in IF CONDITION but this code is not blocking that number SMS. i try best but did't resolved. any one help me.Here is my code
Sms.java
public class Sms extends BroadcastReceiver {
final SmsManager sms = SmsManager.getDefault();
#Override
public void onReceive(Context context, Intent intent) {
// Retrieves a map of extended data from the intent.
final Bundle bundle = intent.getExtras();
try {
if (bundle != null) {
final Object[] pdusObj = (Object[]) bundle.get("pdus");
for (int i = 0; i < pdusObj.length; i++) {
SmsMessage currentMessage = SmsMessage
.createFromPdu((byte[]) pdusObj[i]);
String phoneNumber = currentMessage
.getDisplayOriginatingAddress();
String senderNum = phoneNumber;
String message = currentMessage.getDisplayMessageBody();
Log.i("SmsReceiver", "senderNum: " + senderNum
+ "; message: " + message);
// Show Alert
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, "senderNum: "
+ senderNum + ", message: " + message, duration);
toast.show();
if(senderNum=="+923215619915"){
Toast.makeText(context, "You r in if condition", Toast.LENGTH_LONG).show();
abortBroadcast();
}
} // end for loop
} // bundle is null
} catch (Exception e) {
Log.e("SmsReceiver", "Exception smsReceiver" + e);
}
}
}
Manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.tesingworkspace"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="7"
android:targetSdkVersion="19"
/>
<uses-permission android:name="android.permission.SEND_SMS" >
</uses-permission>
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.RECEIVE_SMS" >
</uses-permission>
<uses-permission android:name="android.permission.READ_SMS"/>
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.tesingworkspace.BroadCastSms"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name="com.example.tesingworkspace.Sms">
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
</application>
</manifest>
The best thing you need to do is debug your code, you have hard coded the phone number in a format that may or may not occur, make sure in what format your receiver phone number appears? When I was working over it I was sending message over 0333xxx-xxxx number and I was receiving as +92xxx. But it's not sure for all, different telecommunication companies may use different format, for that you should use
if(number.equals("+92333xxx-xxxxx")) or better use a contains, that would actually make it more appropriate to match the number and remove the possibility of format error
if(number.contains("333xxx-xxxxx")){
// Your code to abort message
}
Hope this helps
Equals used for comparing the object only.
try to use if(senderNum.equalsIngnorecase("+923215619915")) or if(senderNum.indexOf("+923215619915") != -1).

Categories

Resources