强制关闭使用getContentResolver()

时间:2011-11-30 22:06:02

标签: android

我使用下面的代码在收件箱中插入新的短信,但是当我点击按钮时,程序给力关闭,有什么问题? 感谢

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    Button BTN = (Button) findViewById(R.id.button1);
    BTN.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            ContentValues values = new ContentValues();
            values.put("address", "9878782944");
            values.put("body", "foo bar");
            values.put("date", "1322039220502");
            values.put("read", "1");
            getContentResolver().insert(Uri.parse("content://sms/inbox"), values);
        }
    });
}

发出此错误:

 FATAL EXCEPTION: main
 java.lang.SecurityException: Permission Denial: reading com.android.providers.telephony.MmsSmsProvider uri content://mms-sms/threadID?recipient=9878782944 from pid=337, uid=10038 requires android.permission.READ_SMS
     at android.os.Parcel.readException(Parcel.java:1247)
     at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:160)
     at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:114)
     at android.content.ContentProviderProxy.insert(ContentProviderNative.java:408)
     at android.content.ContentResolver.insert(ContentResolver.java:587)
     at ir.fadesign.sms.Main$1.onClick(Main.java:25)
     at android.view.View.performClick(View.java:2408)
     at android.view.View$PerformClick.run(View.java:8816)
     at android.os.Handler.handleCallback(Handler.java:587)
     at android.os.Handler.dispatchMessage(Handler.java:92)
     at android.os.Looper.loop(Looper.java:123)
     at android.app.ActivityThread.main(ActivityThread.java:4627)
     at java.lang.reflect.Method.invokeNative(Native Method)
     at java.lang.reflect.Method.invoke(Method.java:521)
     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
     at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:2)

根据您的堆栈跟踪,您需要为AndroidManifest.xml添加“android.permission.READ_SMS”权限。

添加以下行:

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

到您的AndroidMainfest.xml文件。

http://developer.android.com/guide/topics/security/security.html#permissions

侧面说明: 看来你正试图用短信做一些事情。根据您的最终目标,您可能需要其他权限。

转到此处:http://developer.android.com/reference/android/Manifest.permission.html

并查看其他短信权限: READ_SMS, WRITE_SMS, 发送短信, 许可权, 等