SecurityException Permission Denial:telephony.SmsProvider uri content:// sms / inbox

时间:2017-05-06 13:48:59

标签: android

我的手机是android 5.0 API 22。 我刚学习构建应用Android 我创建app假短信但崩溃:(

java.lang.SecurityException: Permission Denial: writing com.android.providers.telephony.SmsProvider uri content://sms/outbox from pid=23774, uid=10308 requires android.permission.WRITE_SMS, or grantUriPermission()

我的代码: 主要活动 我希望测试发送短信默认来自电话号码0901123456和消息"测试发送"到我的手机。

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

public static final String TAG = MainActivity.class.getName();
public static final int PERMISSION_RESULT_CODE = 123;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    setupView();

    setDefaultApp();

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        if (checkSelfPermission(Manifest.permission.READ_SMS) != PackageManager.PERMISSION_GRANTED) {
            requestPermissions(new String[]{
                    Manifest.permission.BROADCAST_SMS,
                    Manifest.permission.READ_SMS,
                    Manifest.permission.SEND_SMS,
                    Manifest.permission.RECEIVE_SMS}, PERMISSION_RESULT_CODE);
        }
    }
}

private void setupView(){
    findViewById(R.id.button_draft).setOnClickListener(this);
    findViewById(R.id.button_inbox).setOnClickListener(this);
    findViewById(R.id.button_outbox).setOnClickListener(this);
    findViewById(R.id.button_sent).setOnClickListener(this);

}

private void setDefaultApp(){
    final String myPackageName = getPackageName();
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
        if (!Telephony.Sms.getDefaultSmsPackage(this).equals(myPackageName)) {
            Intent intent =
                    new Intent(Telephony.Sms.Intents.ACTION_CHANGE_DEFAULT);
            intent.putExtra(Telephony.Sms.Intents.EXTRA_PACKAGE_NAME,
                    myPackageName);
            startActivity(intent);
        }
    }
}

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    if (requestCode == PERMISSION_RESULT_CODE){
        for (int i= 0; i< permissions.length; i++) {
            Log.d(TAG, permissions[i] + " " + grantResults[i]);
        }
    }
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}


@Override
public void onClick(View v) {
    switch(v.getId()){

        case R.id.button_draft:
            ContentValues values1 = new ContentValues();
            values1.put("address", "0901123456");
            values1.put("body", "test draft");
            getContentResolver().insert(Uri.parse("content://sms/draft"), values1);
            break;
        case R.id.button_inbox:
            ContentValues values2 = new ContentValues();
            values2.put("address", "0901123456");
            values2.put("body", "test inbox");
            getContentResolver().insert(Uri.parse("content://sms/inbox"), values2);
            break;
        case R.id.button_outbox:
            ContentValues values3 = new ContentValues();
            values3.put("address", "0901123456");
            values3.put("body", "test outbox");
            getContentResolver().insert(Uri.parse("content://sms/outbox"), values3);
            break;
        case R.id.button_sent:
            ContentValues values4 = new ContentValues();
            values4.put("address", "0901123456");
            values4.put("body", "test sent");
            getContentResolver().insert(Uri.parse("content://sms/sent"), values4);
            break;

    }
}

}

AndroidManifest

<?xml version="1.0" encoding="utf-8"?>

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

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    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>

    <!-- BroadcastReceiver that listens for incoming SMS messages -->
    <receiver
        android:name=".SmsReceiver"
        android:permission="android.permission.BROADCAST_SMS">
        <intent-filter>
            <action android:name="android.provider.Telephony.SMS_DELIVER" />
        </intent-filter>
    </receiver>

    <!-- BroadcastReceiver that listens for incoming MMS messages -->
    <receiver
        android:name=".MmsReceiver"
        android:permission="android.permission.BROADCAST_WAP_PUSH">
        <intent-filter>
            <action android:name="android.provider.Telephony.WAP_PUSH_DELIVER" />
            <data android:mimeType="application/vnd.wap.mms-message" />
        </intent-filter>
    </receiver>

    <receiver
        android:name=".FinderReceiver"
        android:enabled="true"
        android:permission="android.permission.RECEIVE_SMS">
        <intent-filter>
            <action
                android:name="android.provider.Telephony.SMS_RECEIVED"
                android:priority="999" />
        </intent-filter>
    </receiver>

    <!-- Activity that allows the user to send new SMS/MMS messages -->
    <activity
        android:name=".ComposeSmsActivity"
        android:exported="true">
        <intent-filter>
            <action android:name="android.intent.action.SEND" />
            <action android:name="android.intent.action.SENDTO" />

            <category android:name="android.intent.category.DEFAULT" />
            <category android:name="android.intent.category.BROWSABLE" />

            <data android:scheme="sms" />
            <data android:scheme="smsto" />
            <data android:scheme="mms" />
            <data android:scheme="mmsto" />
        </intent-filter>
    </activity>

    <!-- Service that delivers messages from the phone "quick response" -->
    <service
        android:name=".HeadlessSmsSendService"
        android:exported="true"
        android:permission="android.permission.SEND_RESPOND_VIA_MESSAGE">
        <intent-filter>
            <action android:name="android.intent.action.RESPOND_VIA_MESSAGE" />
            <category android:name="android.intent.category.DEFAULT" />

            <data android:scheme="sms" />
            <data android:scheme="smsto" />
            <data android:scheme="mms" />
            <data android:scheme="mmsto" />
        </intent-filter>
    </service>
</application>

谁能帮帮我? :(( 非常感谢你!

1 个答案:

答案 0 :(得分:0)

如果您的应用是用户选择的短信客户端,则只能写信给短信提供商:https://android-developers.googleblog.com/2013/10/getting-your-sms-apps-ready-for-kitkat.html