情况是这个样子滴:
可以读取手机上的短信,可以读取SIM卡上的联系人,就是读不到SIM卡上的短信,也设置权限了, <uses-permission android:name="android.permission.READ_PHONE_STATE"></uses-permission> 还是读不到,myCursor 返回的是null。
为什么就读不到呢?
- Java code
package simok.ok;import android.app.Activity;import android.os.Bundle;import android.content.ContentResolver;import android.database.Cursor;import android.provider.Contacts.People;import android.util.Log;import android.widget.Toast;import android.telephony.TelephonyManager;import android.content.Context;import android.content.Intent;import android.telephony.gsm.*;import android.content.Intent;import android.widget.TextView; import android.telephony.gsm.SmsMessage;import android.database.sqlite.SQLiteException; import android.net.Uri; @SuppressWarnings("unused")public class simok extends Activity {private static final String LOG_TAG = "Sms Query";//private TelephonyManager telMgr;//String TAG = "GameScene"; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); //telMgr = (TelephonyManager)getSystemService(TELEPHONY_SERVICE) getSmsAndSendBack(); } public void getSmsAndSendBack() { String[] projection = new String[] {"_id","address","person","body" }; //StringBuilder str=new StringBuilder(); // Cursor myCursor = cr.query(Uri.parse("content://sms/inbox"),projection,null, null ,null); //Uri uri = Uri.parse("content://sms/icc"); //Cursor myCursor = managedQuery(Uri.parse("content://sms/inbox"),projection,null, null ,null); //Cursor myCursor = managedQuery(Uri.parse("content://sms/icc"),projection,null, null ,null); Cursor myCursor = managedQuery(Uri.parse("content://icc/adn"),projection,null, null ,null); // Log.v(TAG, "onTouchDownReal()"); processResults(myCursor); } private void processResults(Cursor cur) { if (cur.moveToFirst()) //int count=cur.getCount(); //cur.moveToFirst(); //for(int i=0;i<count;i++) { String name; String phoneNumber; String sms; int nameColumn = cur.getColumnIndex("person"); int phoneColumn = cur.getColumnIndex("address"); int smsColumn = cur.getColumnIndex("body"); do { name = cur.getString(nameColumn); phoneNumber = cur.getString(phoneColumn); sms = cur.getString(smsColumn); Log.i(LOG_TAG,"name="+name); Log.i(LOG_TAG,"phoneNumber="+phoneNumber); Toast.makeText(this,name+""+phoneNumber+""+sms,Toast.LENGTH_LONG).show(); } while (cur.moveToNext()); } //cur.moveToNext(); else { Toast.makeText(this,"你好",Toast.LENGTH_LONG).show();} }//processResults // public final static String SMS_URI_ALL = "content://sms/"; //0 //public final static String SMS_URI_INBOX = "content://sms/inbox";//1 public final static String SMS_URI_SEND = "content://sms nt";//2 public final static String SMS_URI_DRAFT = "content://sms/draft";//3 public final static String SMS_URI_OUTBOX = "content://sms/outbox";//4 public final static String SMS_URI_FAILED = "content://sms/failed";//5 public final static String SMS_URI_QUEUED = "content://sms/queued";//6 //public final static String SMS_ICC = "content://sms/icc";//23 }