Send sms permission doesn't work

邮差的信 提交于 2019-12-01 10:39:06

All you need to do is,

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.roa.sendsms"
    android:versionCode="1"
    android:versionName="1.0">

    <uses-permission android:name="android.permission.SEND_SMS"/>

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="19"/>

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme">

        <activity
            android:name="com.example.homesafe.MainActivity"
            android:label="@string/app_name">

            <intent-filter>
                <action android:name="android.intent.action.MAIN" />    
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>

Try to run into the mobile instead checking it to the emulator. Just because emulator does not have the sim card as our mobiles have, so.

Try to use something like this. Not tested, but should work fine. Drop a note when you see some problem here.

This is just to show how permission granting in Android M works. Please extend it by functionalities mentioned on Android tutorial site about permissions.

You will need add ActivityCompat.shouldShowRequestPermissionRationale check to match best practices. I can extend this answer but I think it's not necessary. Just make sure you are granting permissions in runtime (or use targetSdkVersion lower then 23 - this is however not recommended)

private static final int PERMISSION_SEND_SMS = 123;

private void requestSmsPermission() {

    // check permission is given
    if (ContextCompat.checkSelfPermission(thisActivity, Manifest.permission.READ_CONTACTS) != PackageManager.PERMISSION_GRANTED) {
        // request permission (see result in onRequestPermissionsResult() method)
        ActivityCompat.requestPermissions(thisActivity,
            new String[]{Manifest.permission.SEND_SMS},
            PERMISSION_SEND_SMS);
    } else {
        // permission already granted run sms send
        sendSms(phone, message);
    }
}

@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
    switch (requestCode) {
        case MY_PERMISSIONS_REQUEST_READ_CONTACTS: {

            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                // permission was granted
                sendSms(phone, message);
            } else {
                // permission denied
            }
            return;
        }
    }
}

private void sendSms(String phoneNumber, String message){
    SmsManager sms = SmsManager.getDefault();
    sms.sendTextMessage(phoneNumber, null, message, null, null);
}

You may try this way:

private void sendSms(String phoneNumber, String message) {
   Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse( "sms:" + phoneNumber ) ); 
   intent.putExtra( "sms_body", message ); 
   startActivity(intent);
}

That should work

PackageManager pm = this.getPackageManager();

 if (pm.hasSystemFeature(PackageManager.FEATURE_TELEPHONY)) {
     System.out.println("Supported");
 } else {
     System.out.println("nope");
 }

i had same problem you need to get runtime permission on setting>app>"your app" >enable sms permission

I tried this in Huawei Android 8, besides adding permission READ_PHONE_STATE in manifest, the phone needs to allow to "access phone ID" to the app. Then it works.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!