How to receive SMS messages from the telephone operator?

I can get the user's phone operator using TelephonyManager, and I need to use this name to receive texts received from the operator. How can I use something like a SQL where clause with the Android ContextResolver.query () method? Here is my code to receive ALL text messages received by the user. I need to configure it to receive messages from the phone operator.

public void fetchSMS() {

    TelephonyManager tMgr = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
    String operator=tMgr.getNetworkOperatorName();
    Uri mSmsinboxQueryUri = Uri.parse("content://sms/inbox");
    String[] cols = new String[]{"_id", "thread_id", "address", "person", "date", "body", "type"};
    Cursor cursor1 = getContentResolver().query(
            mSmsinboxQueryUri,
            cols, null, null, null);
    if (cursor1!=null && cursor1.moveToFirst()) {
        do {
            String address = cursor1.getString(cursor1
                    .getColumnIndex(cols[2]));
            //String address2=(address.split("-"))[1];

                String date = cursor1.getString(cursor1
                        .getColumnIndex(cols[4]));
                String msg = cursor1.getString(cursor1
                        .getColumnIndex(cols[5]));
                String type = cursor1.getString(cursor1
                        .getColumnIndex(cols[6]));

                smsLog.append("\n" + "Address:").append(address).append("\n" + "Date:").append(date).append("\n" + "Message:").append(msg).append("\n" + "Type:").append(type).append("\n\n");
            cursor1.moveToNext();
        }
        while (!cursor1.isAfterLast());
        cursor1.close();
    }
}
+4
source share
1 answer

Maybe this is good for you.

Additional information links to this page .

SmsBroadcastReceiver.java

BroadcastReceiver, SMS- . onReceive, SMS- -, , SMS- SMS. SMS-, .

public class SmsBroadcastReceiver extends BroadcastReceiver {

    public static final String SMS_BUNDLE = "pdus";

    public void onReceive(Context context, Intent intent) {
        Bundle intentExtras = intent.getExtras();
        if (intentExtras != null) {
            Object[] sms = (Object[]) intentExtras.get(SMS_BUNDLE);
            String smsMessageStr = "";
            for (int i = 0; i < sms.length; ++i) {
                SmsMessage smsMessage = SmsMessage.createFromPdu((byte[]) sms[i]);

                String smsBody = smsMessage.getMessageBody().toString();
                String address = smsMessage.getOriginatingAddress();

                smsMessageStr += "SMS From: " + address + "\n";
                smsMessageStr += smsBody + "\n";
            }
            Toast.makeText(context, smsMessageStr, Toast.LENGTH_SHORT).show();

            //this will update the UI with message
            SmsActivity inst = SmsActivity.instance();
            inst.updateList(smsMessageStr);
        }
    }
}

SmsActivity.java SMS Android. SMS. ListView SMS-. onCreate , SMS, Uri ListView.

public class SmsActivity extends Activity implements OnItemClickListener {

    private static SmsActivity inst;
    ArrayList<String> smsMessagesList = new ArrayList<String>();
    ListView smsListView;
    ArrayAdapter arrayAdapter;

    public static SmsActivity instance() {
        return inst;
    }

    @Override
    public void onStart() {
        super.onStart();
        inst = this;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sms);
        smsListView = (ListView) findViewById(R.id.SMSList);
        arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, smsMessagesList);
        smsListView.setAdapter(arrayAdapter);
        smsListView.setOnItemClickListener(this);

        refreshSmsInbox();
    }

    public void refreshSmsInbox() {
        ContentResolver contentResolver = getContentResolver();
        Cursor smsInboxCursor = contentResolver.query(Uri.parse("content://sms/inbox"), null, null, null, null);
        int indexBody = smsInboxCursor.getColumnIndex("body");
        int indexAddress = smsInboxCursor.getColumnIndex("address");
        if (indexBody < 0 || !smsInboxCursor.moveToFirst()) return;
        arrayAdapter.clear();
        do {
            String str = "SMS From: " + smsInboxCursor.getString(indexAddress) +
                    "\n" + smsInboxCursor.getString(indexBody) + "\n";
            arrayAdapter.add(str);
        } while (smsInboxCursor.moveToNext());
    }

    public void updateList(final String smsMessage) {
        arrayAdapter.insert(smsMessage, 0);
        arrayAdapter.notifyDataSetChanged();
    }

    public void onItemClick(AdapterView<?> parent, View view, int pos, long id) {
        try {
            String[] smsMessages = smsMessagesList.get(pos).split("\n");
            String address = smsMessages[0];
            String smsMessage = "";
            for (int i = 1; i < smsMessages.length; ++i) {
                smsMessage += smsMessages[i];
            }

            String smsMessageStr = address + "\n";
            smsMessageStr += smsMessage;
            Toast.makeText(this, smsMessageStr, Toast.LENGTH_SHORT).show();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
+1

All Articles