android4.4以上的版本,插入短彩信怎么做

代码如下,用4.4以下的版本能插入,4.4以上的不行,需要做些什么,请大神们帮助,不禁感谢!

manifest文件:

<?xml version="1.0" encoding="utf-8"?>
package="com.test.contact"
android:versionCode="1"
android:versionName="1.0" >

<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.test.contact.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>
    <activity 
        android:name=".AddContactActivity"
        android:label="@string/contact_name"></activity>
   <activity 
       android:name=".AddSmsActivity"
       android:label="@string/sms_name">
       <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>
   <activity 
       android:name=".AddMmsActivity"
       android:label="@string/mms_name"></activity>
    <activity
        android:name=".AddCallLogActivity"
        android:label="通话记录"></activity>

</application>
<!--添加联系人权限-->
<uses-permission android:name="android.permission.READ_CONTACTS"/>
<uses-permission android:name="android.permission.WRITE_CONTACTS"/>
<uses-permission android:name="android.permission.READ_SMS"/>
<uses-permission android:name="android.permission.WRITE_SMS"/>
<uses-permission android:name="android.permission.WRITE_CALL_LOG"/>
<uses-permission android:name="android.permission.READ_CALL_LOG"/>

插入短信activity:

package com.test.contact;

import android.app.Activity;
import android.content.ContentValues;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
import android.provider.Telephony;

public class AddSmsActivity extends Activity{

String TAG = "AddSmsActivity";

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_addsms);

    String defaultSmsPkg = Telephony.Sms.getDefaultSmsPackage(this);
    String mySmsPkg= this.getPackageName();
    if(!defaultSmsPkg.equals(mySmsPkg)){
        //            如果这个App不是默认的Sms App,则修改成默认的SMS APP
        //            因为从Android 4.4开始,只有默认的SMS APP才能对SMS数据库进行处理
        Intent intent=new Intent(Telephony.Sms.Intents.ACTION_CHANGE_DEFAULT);
        intent.putExtra(Telephony.Sms.Intents.EXTRA_PACKAGE_NAME,mySmsPkg);
        startActivity(intent);
    }
}


public void addSmsButton_Click(View view){
    Toast toast = Toast.makeText(this,"开始添加短信",Toast.LENGTH_SHORT);
    toast.show();
    for(int i = 0;i < this.getCount();i++){
        insertSms(this.getPhoneNum()+i);
        Log.e(TAG,"addSmsButton_Click:"+this.getPhoneNum());
    }
    Toast toast1 = Toast.makeText(this,"添加成功",Toast.LENGTH_SHORT);
    toast1.show();
}

public Long getPhoneNum(){
    Long smsNum;
    String temp;
    final EditText inputNum = (EditText)findViewById(R.id.sms_num);
    if(TextUtils.isEmpty(inputNum.getText())){
        temp = "10086";
    }
    else temp = inputNum.getText().toString();
    smsNum = Long.parseLong(temp);
    return smsNum;
}

public String getContent(){
    String content;
    final EditText inputContent = (EditText)findViewById(R.id.sms_content);
    if(TextUtils.isEmpty(inputContent.getText())){
        content = "这是一条默认插入的短信,用来批量插入短信内容:1234567890!@#¥%……&*(),。、?good!";
    }
    else content = inputContent.getText().toString();
    return content;
}
public int getCount(){
    int count;
    String temp;
    final EditText inputTime = (EditText)findViewById(R.id.sms_time);
    if(TextUtils.isEmpty(inputTime.getText())){
        temp = "1";
    }
    else temp = inputTime.getText().toString();
    count = Integer.parseInt(temp);
    return count;
}

public void insertSms(Long number){
    ContentValues values = new ContentValues();
    values.put(Telephony.Sms.DATE,System.currentTimeMillis());//信息时间
    values.put(Telephony.Sms.READ,false);//阅读状态
    values.put(Telephony.Sms.TYPE,Telephony.Sms.MESSAGE_TYPE_INBOX);//1为收;2为发
    values.put(Telephony.Sms.ADDRESS,Long.toString(number));
    values.put(Telephony.Sms.BODY,this.getContent());
    Uri uri = getContentResolver().insert(Telephony.Sms.CONTENT_URI,values);
    Log.e(TAG,"number:"+Long.toString(number));
    Log.e(TAG,"content:"+this.getContent());
    Log.e(TAG,"uri:"+uri);
}

}

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问