android - 发出带有权限拒绝的传出短信

标签 android

我想记录所有发送的短信并遵循下面的代码

SMSObserver 类

public class ServiceObserver extends ContentObserver {
private Context mContext;

//private String contactId = "", contactName = "";
private String smsBodyStr = "", phoneNoStr = "";
private long smsDatTime = System.currentTimeMillis();
static final Uri SMS_STATUS_URI = Uri.parse("content://sms/out");

public ServiceObserver(Handler handler, Context ctx) {
    super(handler);
    mContext = ctx;
}

public boolean deliverSelfNotifications() {
    return true;
}
public void onChange(boolean selfChange) {
    try{
        //Log.e("Info","Notification on SMS observer");
        Cursor sms_sent_cursor = mContext.getContentResolver().query(SMS_STATUS_URI, null, null, null, null);
        if (sms_sent_cursor != null) {
            if (sms_sent_cursor.moveToFirst()) {
                String protocol = sms_sent_cursor.getString(sms_sent_cursor.getColumnIndex("protocol"));
                Log.e("Info","protocol : " + protocol);
                int type = sms_sent_cursor.getInt(sms_sent_cursor.getColumnIndex("type"));
                    Log.e("Info","SMS Type : " + type);
                    // for actual state type=2
                    if(type == 2){
                        Log.e("Info","Id : " + sms_sent_cursor.getString(sms_sent_cursor.getColumnIndex("_id")));
                        Log.e("Info","Thread Id : " + sms_sent_cursor.getString(sms_sent_cursor.getColumnIndex("thread_id")));
                        Log.e("Info","Address : " + sms_sent_cursor.getString(sms_sent_cursor.getColumnIndex("address")));
                        Log.e("Info","Person : " + sms_sent_cursor.getString(sms_sent_cursor.getColumnIndex("person")));
                        Log.e("Info","Date : " + sms_sent_cursor.getLong(sms_sent_cursor.getColumnIndex("date")));
                        Log.e("Info","Read : " + sms_sent_cursor.getString(sms_sent_cursor.getColumnIndex("read")));
                        Log.e("Info","Status : " + sms_sent_cursor.getString(sms_sent_cursor.getColumnIndex("status")));
                        Log.e("Info","Type : " + sms_sent_cursor.getString(sms_sent_cursor.getColumnIndex("type")));
                        Log.e("Info","Rep Path Present : " + sms_sent_cursor.getString(sms_sent_cursor.getColumnIndex("reply_path_present")));
                        Log.e("Info","Subject : " + sms_sent_cursor.getString(sms_sent_cursor.getColumnIndex("subject")));
                        Log.e("Info","Body : " + sms_sent_cursor.getString(sms_sent_cursor.getColumnIndex("body")));
                        Log.e("Info","Err Code : " + sms_sent_cursor.getString(sms_sent_cursor.getColumnIndex("error_code")));

                        smsBodyStr = sms_sent_cursor.getString(sms_sent_cursor.getColumnIndex("body")).trim();
                        phoneNoStr = sms_sent_cursor.getString(sms_sent_cursor.getColumnIndex("address")).trim();
                        smsDatTime = sms_sent_cursor.getLong(sms_sent_cursor.getColumnIndex("date"));

                        Log.e("Info","SMS Content : "+smsBodyStr);
                        Log.e("Info","SMS Phone No : "+phoneNoStr);
                        Log.e("Info","SMS Time : "+smsDatTime);
                    }
                }
            }
        }
        else
            Log.e("Info","Send Cursor is Empty");
    }
    catch(Exception sggh){
        Log.e("Error", "Error on onChange : "+sggh.toString());
    }
    super.onChange(selfChange);
}

在主要 Activity 上

ServiceObserver smsSentObserver = new ServiceObserver(new Handler(), this);
this.getContentResolver().registerContentObserver(SMS_STATUS_URI, true, smsSentObserver);

并在 list 文件中添加了后续权限

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

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

<uses-permssion android:name="android.permission.READ_SMS"/>
<uses-permssion android:name="android.permission.WRITE_SMS" />
    <uses-permission android:name="android.permission.RECEIVE_SMS"></uses-permission>    
    <uses-permission android:name="android.permission.INTERNET" />    
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name" >
    <activity
        android:label="@string/app_name"
        android:theme="@android:style/Theme.NoTitleBar"
        android:name="com.test.sms.mainactivity" >
        <intent-filter >
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <receiver android:name="com.test.sms.ServiceReceiver"> 
        <intent-filter> 
            <action android:name="android.provider.Telephony.SMS_RECEIVED" /> 
            <action android:name="android.net.conn.CONNECTIVITY_CHANGE"/>                
        </intent-filter> 
    </receiver>        
</application>

但是在尝试将短信从第一个模拟器发送到第二个之后,直到得到这样的权限错误

*onChange 时出错:java.lang.SecurityException:权限被拒绝:打开提供程序 com.android.providers.telephony.SmsProvider....需要 android.permission.READ_SMS 或 android.permission.WRITE_SMS*

任何人都可以帮助......谢谢......

最佳答案

这是因为您的拼写错误(permssion 而不是 permission)。只需将相应的行更改为:

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

关于android - 发出带有权限拒绝的传出短信,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10579351/

相关文章:

android - 在带有填充角的自定义按钮上添加点击效果

java - Android TCP 客户端。服务器仅在进程停止后接收消息

java - 使用 ArrayList 中的数据填充 Android ListView 是将每个项目设置为与第一个项目相同

Android 处理程序更改 WeakReference

android - 如何在未扩展 Activity 的类中使用 getBaseContext()

Android:如何在提取音频之前拍摄视频

java - 使用 Gson 时出现奇怪的 "nameValuePairs"键

android - 如何将YUV_420_888图像转换为位图

android - 布局 XML 中的注释不起作用

android - 我想在我的应用程序中按下一个按钮,结果自动启动谷歌地图