0

我使用了广播接收器并捕获了短信..我只想将短信从我的应用程序发送到安装了我的应用程序的另一部手机..我使用了优先级,但它不起作用..我希望移动短信默认收件箱不会收到短信..只有我的应用收件箱会收到短信..我已经为短信收件箱制作了自己的数据库...该怎么办?

//my sms receiver class
package com.example.crypton;
import java.util.ArrayList;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.provider.ContactsContract.PhoneLookup;
import android.telephony.SmsMessage;
import android.widget.Toast;



public class SmsReceiver extends BroadcastReceiver  {

ArrayList<String> your_array_list = new ArrayList<String>();
@Override
public void onReceive(Context context, Intent intent) 
{
    //---get the SMS message passed in---
    Bundle bundle = intent.getExtras();        
    SmsMessage[] msgs = null;
   String str = "";    
   String str2 = ""; 
    if (bundle != null)
    {
        //---retrieve the SMS message received---
        Object[] pdus = (Object[]) bundle.get("pdus");
        msgs = new SmsMessage[pdus.length];            
        for (int i=0; i<msgs.length; i++){
            msgs[i] = SmsMessage.createFromPdu((byte[])pdus[i]);                

            str2 +=  msgs[i].getOriginatingAddress();                     
           // str2 += " :";
            str += msgs[i].getMessageBody().toString();
            DatabaseHandler db = new DatabaseHandler(context);
            db.addSMS(new IncomingSMS(str,str2));      
        }


        Uri lookupUri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(str2));
        Cursor c = context.getContentResolver().query(lookupUri, new String[]{ContactsContract.Data.DISPLAY_NAME},null,null,null);
        try {
            c.moveToFirst();
         String  displayName = c.getString(0);
         str2 = displayName;   

        } catch (Exception e) {
            e.printStackTrace();
        }finally{
            c.close();
        }
        //---display the new SMS message---
        Toast.makeText(context, str, Toast.LENGTH_LONG).show();

    /* Intent i = new Intent(context, StoreMsg.class);
        i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        i.putExtra("sms", str);
        i.putExtra("phonenumber",str2);
        context.startActivity(i);*/


    }   
}  

}

4

1 回答 1

1

停止广播使用

this.abortBroadcast();

就在 finally 块之后

于 2013-09-07T18:42:47.287 回答