I'm trying to setup a simple caller ID for a messaging application I'm creating for learning purposes. I'm having trouble using the ContactsContact content provider to do this. here is the problem:
I am running into this Error:
Cannot make a static reference to the non-static method updateMessageBox() from the type MainActivity
The simplest fix would be to make updateMessageBox() a static method. However, I can't make updateMessageBox() a static method because it implements getContentResolver(), which cannot be used in a static method. I think I'm having some trouble understanding the static/non-static declaration (obviously). Here is the MainActivity Class and the SmsReceiver Class. I won't include the Android Manifest file but I will note that it does have the SmsReceiver file registered to receive SMS.
MainActivity
public class MainActivity extends Activity {
Button btnSendSMS;
public String recipient = "phone Number was here and it worked";
public static String callerId;
public static String message;
public String sender = "Jeff";
static TextView textView;
public static String LOGTAG = "LOGGED";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnSendSMS = (Button) findViewById(R.id.button1);
btnSendSMS.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
final EditText textInput=(EditText)findViewById(R.id.editText1);
textView=(TextView)findViewById(R.id.textView1);
final String str=textInput.getText().toString();
sendSMS(recipient, str);
textInput.setText("");
textView.append("\n" + sender + ": " + str);
}
});
}
private void sendSMS(String phoneNumber, String message)
{String SENT = "SMS SENT";
String DELIVERED = "SMS DELIVERED";
PendingIntent sentPI = PendingIntent.getBroadcast(this, 0,
new Intent(SENT), 0);
PendingIntent deliveredPI = PendingIntent.getBroadcast(this, 0,new
Intent(DELIVERED), 0);
registerReceiver(new BroadcastReceiver(){
@Override
public void onReceive(Context arg0, Intent arg1) {
switch (getResultCode())
{
case Activity.RESULT_OK:
Toast.makeText(getBaseContext(), "SMS DELIVERED",
Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
Toast.makeText(getBaseContext(), "GENERIC FAILURE",
Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_NO_SERVICE:
Toast.makeText(getBaseContext(), "NO SERVICE",
Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_NULL_PDU:
Toast.makeText(getBaseContext(), "NULL PDU",
Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_RADIO_OFF:
Toast.makeText(getBaseContext(), "RADIO OFF",
Toast.LENGTH_SHORT).show();
break;
}
}
}, new IntentFilter(SENT));
//---when the SMS has been delivered---
registerReceiver(new BroadcastReceiver(){
@Override
public void onReceive(Context arg0, Intent arg1) {
switch (getResultCode())
{
case Activity.RESULT_OK:
Toast.makeText(getBaseContext(), "SMS Delivered",
Toast.LENGTH_SHORT).show();
break;
case Activity.RESULT_CANCELED:
Toast.makeText(getBaseContext(), "SMS Not Delivered",
Toast.LENGTH_SHORT).show();
break;
}
}
}, new IntentFilter(DELIVERED));
SmsManager sms = SmsManager.getDefault();
sms.sendTextMessage(phoneNumber, null, message, sentPI, deliveredPI);
}
public void updateMessageBox()
{
Cursor contactLookupCursor =
getContentResolver().query(
Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI,
Uri.encode(callerId)),
new String[] {PhoneLookup.DISPLAY_NAME, PhoneLookup._ID},
null,
null,
null);
try {
while(contactLookupCursor.moveToNext()){
String contactName = contactLookupCursor.getString(contactLookupCursor.getColumnIndexOrThrow(PhoneLookup.DISPLAY_NAME));
String contactId = contactLookupCursor.getString(contactLookupCursor.getColumnIndexOrThrow(PhoneLookup._ID));
Log.d(LOGTAG, "contactMatch name: " + contactName);
Log.d(LOGTAG, "contactMatch id: " + contactId);
if (contactName != null) {
callerId = contactName;
}
}
} finally {
textView.append("\n" + callerId + ": " + message);
contactLookupCursor.close();
}
}
}
And the SmsReceiver file
public class SmsReceiver extends BroadcastReceiver {
@Override
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){
MainActivity.callerId = msg.getOriginatingAddress();
MainActivity.message = msg.getMessageBody();
MainActivity.updateMessageBox();
}
}
}
Any tips pointing me the right direction would be very much appreciated. I've been workin on this for a few days and Its driving me nuts. Thanks in advance.