I want to receive messages from a specific number. When I run the app, I get messages from everyone in my inbox. I got the source code from this site (android authority)
package com.nqr.smsapp;
import android.Manifest; import android.content.ContentResolver;
import android.content.pm.PackageManager; import
android.database.Cursor; import android.net.Uri; import
android.support.annotation.NonNull; import
android.support.v4.content.ContextCompat; import
android.support.v7.app.AppCompatActivity; import android.os.Bundle;
import android.telephony.SmsManager; import android.view.View; import
android.widget.ArrayAdapter; import android.widget.EditText; import
android.widget.ListView; import android.widget.Toast;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
ArrayList<String> smsMessagesList = new ArrayList<>();
ListView messages;
ArrayAdapter arrayAdapter;
EditText input;
SmsManager smsManager = SmsManager.getDefault();
private static MainActivity inst;
private static final int READ_SMS_PERMISSIONS_REQUEST = 1;
public static MainActivity instance() {
return inst;
}
@Override
public void onStart() {
super.onStart();
inst = this;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
messages = (ListView) findViewById(R.id.messages);
input = (EditText) findViewById(R.id.input);
arrayAdapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, smsMessagesList);
messages.setAdapter(arrayAdapter);
if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_SMS)
!= PackageManager.PERMISSION_GRANTED) {
getPermissionToReadSMS();
} else {
refreshSmsInbox();
}
}
public void updateInbox(final String smsMessage) {
arrayAdapter.insert(smsMessage, 0);
arrayAdapter.notifyDataSetChanged();
}
public void onSendClick(View view) {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.SEND_SMS)
!= PackageManager.PERMISSION_GRANTED) {
getPermissionToReadSMS();
} else {
smsManager.sendTextMessage("07911127456", null, input.getText().toString(), null, null);
Toast.makeText(this, "Message sent!", Toast.LENGTH_SHORT).show();
} }
public void getPermissionToReadSMS() {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_SMS)
!= PackageManager.PERMISSION_GRANTED) {
if (shouldShowRequestPermissionRationale(
Manifest.permission.READ_SMS)) {
Toast.makeText(this, "Please allow permission!", Toast.LENGTH_SHORT).show();
}
requestPermissions(new String[]{Manifest.permission.READ_SMS},
READ_SMS_PERMISSIONS_REQUEST);
}
}
@Override
public void onRequestPermissionsResult(int requestCode,
@NonNull String permissions[],
@NonNull int[] grantResults) {
// Make sure it's our original READ_CONTACTS request
if (requestCode == READ_SMS_PERMISSIONS_REQUEST) {
if (grantResults.length == 1 &&
grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Toast.makeText(this, "Read SMS permission granted", Toast.LENGTH_SHORT).show();
refreshSmsInbox();
} else {
Toast.makeText(this, "Read SMS permission denied", Toast.LENGTH_SHORT).show();
}
} else {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
}
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()); //messages.setSelection(arrayAdapter.getCount() - 1);
}
}
>
I want the messages to be delivered from a specific number to my inbox: for instance messages only from 07911 127456 in my inbox
I only need a specific conversation
broadcast receiver
package com.nqr.smsapp;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsMessage;
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) {
String format = intentExtras.getString("format");
SmsMessage smsMessage = SmsMessage.createFromPdu((byte[]) sms[i], format);
String smsBody = smsMessage.getMessageBody().toString();
String address = smsMessage.getOriginatingAddress();
smsMessageStr += "SMS From: " + address + "\n";
smsMessageStr += smsBody + "\n";
}
MainActivity inst = MainActivity.instance();
inst.updateInbox(smsMessageStr);
}
}
}
Based on this tutorial site(android authority)
In your BroadcastReceiver
int yourNumber=1234567890; // your specific number
String address = smsMessage.getDisplayOriginatingAddress(); // incoming SMS number
if(address ==yourNumber){
//display message
}
Update
public class SmsBroadcastReceiver extends BroadcastReceiver {
String address;
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) {
String format = intentExtras.getString("format");
SmsMessage smsMessage = SmsMessage.createFromPdu((byte[]) sms[i], format);
String smsBody = smsMessage.getMessageBody().toString();
address = smsMessage.getOriginatingAddress();
smsMessageStr += "SMS From: " + address + "\n";
smsMessageStr += smsBody + "\n";
}
if(address =="7911127456"){
MainActivity inst = MainActivity.instance();
inst.updateInbox(smsMessageStr);
}
}
}
}
update
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 {
if(indexAddress ==7911127456){
String str = "SMS From: " + smsInboxCursor.getString(indexAddress) +
"\n" + smsInboxCursor.getString(indexBody) + "\n";
arrayAdapter.add(str);
}
} while (smsInboxCursor.moveToNext());
//messages.setSelection(arrayAdapter.getCount() - 1);
arrayAdapter.notifyDataSetChanged();
}