在android中只读最近的短信

时间:2014-01-21 05:24:39

标签: android sms

我正在开发一个从收件箱接收短信的应用程序,我正在接收短信成功但我的要求只收到新的(最近)消息,我不想从收件箱中读取所有消息,请帮助我

我的代码是

 public class MainActivity extends Activity {


    TextView showText;
StringBuilder sb = new StringBuilder();
ArrayAdapter<String> adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    showText = (TextView)findViewById(R.id.text);
    String message = getMessagess();

    showText.setText(message);
}

public String getMessagess(){
    Cursor cursor=null;
    Uri smsuri = Uri.parse("content://sms/inbox");
    try{
     cursor =getContentResolver().query(smsuri, null, null, null, null);
     if (cursor == null) {
         // Log.i(TAG, "cursor is null. uri: " + mSmsQueryUri);

      }

     for(boolean   
         hashdata=cursor.moveToFirst();hashdata;hashdata=cursor.moveToNext()){

        final String body =   
        cursor.getString(cursor.getColumnIndexOrThrow("body")).toString(); 
         final String senderNo= 
        cursor.getString(cursor.getColumnIndexOrThrow("address")).toString();
         final String  date = 
      cursor.getString(cursor.getColumnIndexOrThrow("date"));
         final String  type = 
      cursor.getString(cursor.getColumnIndexOrThrow("type"));

     if(senderNo.equals("+XXXXXXXXXX")){

         for(String bd:body.split(";"))
         sb.append(bd);

         sb.append("\n");

         }
     }
    if(sb==null){

        sb.append("no message found");
    }

    }
    catch(Exception e){
        e.printStackTrace();
    }
    finally{
    cursor.close();
    }
    return sb.toString();       
}

}

1 个答案:

答案 0 :(得分:3)

为此,您需要创建接收SMS_RECEIVED广播的BroadcastReceiver,并向您的应用程序发送新SMS已被告知的警报。

尝试使用以下代码。

ReceiveSMSActivity:

public class ReceiveSMSActivity extends Activity {

public static TextView messageBox;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    messageBox=(TextView)findViewById(R.id.messageBox);
}

public static void updateMessageBox(String msg) {
    messageBox.append(msg);
}

}

TextMessageReceiver:

public class TextMessageReceiver extends BroadcastReceiver {

public void onReceive(Context context, Intent intent) {
    Bundle bundle=intent.getExtras();

    Object[] messages=(Object[])bundle.get("pdus");
    SmsMessage[] sms=new SmsMessage[messages.length];

    for(int n=0;n<messages.length;n++){
        sms[n]=SmsMessage.createFromPdu((byte[]) messages[n]);
    }

    for(SmsMessage msg:sms) {
        ReceiveSMSActivity.updateMessageBox("\nFrom: "+msg.getOriginatingAddress()+"\n"+
                "Message: "+msg.getMessageBody()+"\n");
    }
}
}

AndroidManifest.xml

中添加此内容
<receiver android:name=".TextMessageReceiver"> 
    <intent-filter> 
        <action android:name="android.provider.Telephony.SMS_RECEIVED" /> 
    </intent-filter> 
</receiver>

以及此权限

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

另一个SampleCode:How-to-receive-SMS