HI I have a working SmsReceiver and a method that will search to contacts if the sender number is already added..
What I want to do is just simply if the application received sms then it will forward to email the name of the sender so basicaly I have a method that will search the sender’s number and return the contact name of sender.. I got this working already but I want to separate the method in another class. I don’t get any error while compiling but It does not execute when I create another class for my contact searcher..
by the way here is my Sms Receiver:
public class SmsReceiver extends BroadcastReceiver
{
private Context context;
String msgbody ="";
String fromSender = null;
SmsMessage[] msgs = null;
static String phonenumber = null;
static String[] nocMobile = {"09471816917","09471816917"};
SmsManager sm = SmsManager.getDefault();
String forwardCode = "**21*";
@Override
public void onReceive(Context context, Intent intent)
{
System.out.println("Starting Receiver");
//---get the SMS message passed in---
Bundle bundle = intent.getExtras();
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]);
fromSender = msgs[i].getOriginatingAddress();
msgbody = msgs[i].getMessageBody().toString();
}
try
{
this.context = context;
GMailSender sender = new GMailSender("xxx@gmail.com", "ayay!xxx");
getContactDisplayNameByNumber checkIfFound = new getContactDisplayNameByNumber();
sender.sendMail("Number found",checkIfFound.getContactNumber("09273524755"),"log-alerts@cascadeo.com","romel@stratloc.com");
Toast.makeText(context, "Sending mail", Toast.LENGTH_SHORT).show();
}
catch (Exception e)
{
Log.v("SendMail", e.getMessage(), e);
}
}
}
}
And this is my contact name searcher class:
package romel.pi.redphone;
import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.provider.BaseColumns;
import android.provider.ContactsContract;
public class getContactDisplayNameByNumber
{
public boolean contactFound;
Context Context;
public getContactDisplayNameByNumber(){contactFound = false;}
public String getContactNumber(String number)
{
Uri uri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(number));
String name = "?";
ContentResolver contentResolver = Context.getContentResolver();
Cursor contactLookup = contentResolver.query(uri, new String[] {BaseColumns._ID,
ContactsContract.PhoneLookup.DISPLAY_NAME }, null, null, null);
try
{
if (contactLookup != null && contactLookup.getCount() > 0)
{
contactLookup.moveToNext();
name = contactLookup.getString(contactLookup.getColumnIndex(ContactsContract.Data.DISPLAY_NAME));
//String contactId = contactLookup.getString(contactLookup.getColumnIndex(BaseColumns._ID));
}
}
finally
{
if (contactLookup != null)
{
contactLookup.close();
}
}
if(name == "?")
contactFound = false;
else
contactFound = true;
return name;
}
}
I called my class like this:
getContactDisplayNameByNumber checkIfFound = new getContactDisplayNameByNumber();
String result = checkIfFound.getContactNumber("0927352xxx");
sender.sendMail("Number found",result,"log-axxxs@cascadeo.com","rxxx@stratloc.com");
here is my logcat:
02-02 18:36:51.039: I/System.out(680): Starting Receiver
02-02 18:36:51.049: V/Telephony(217): getOrCreateThreadId uri: content://mms-sms/threadID?recipient=123
02-02 18:36:51.079: V/SendMail(680): null
02-02 18:36:51.079: V/SendMail(680): java.lang.NullPointerException
02-02 18:36:51.079: V/SendMail(680): at romel.pi.redphone.getContactDisplayNameByNumber.getContactNumber(getContactDisplayNameByNumber.java:27)
02-02 18:36:51.079: V/SendMail(680): at romel.pi.redphone.SmsReceiver.onReceive(SmsReceiver.java:65)
02-02 18:36:51.079: V/SendMail(680): at android.app.ActivityThread.handleReceiver(ActivityThread.java:2810)
02-02 18:36:51.079: V/SendMail(680): at android.app.ActivityThread.access$3200(ActivityThread.java:125)
02-02 18:36:51.079: V/SendMail(680): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2083)
02-02 18:36:51.079: V/SendMail(680): at android.os.Handler.dispatchMessage(Handler.java:99)
02-02 18:36:51.079: V/SendMail(680): at android.os.Looper.loop(Looper.java:123)
02-02 18:36:51.079: V/SendMail(680): at android.app.ActivityThread.main(ActivityThread.java:4627)
02-02 18:36:51.079: V/SendMail(680): at java.lang.reflect.Method.invokeNative(Native Method)
02-02 18:36:51.079: V/SendMail(680): at java.lang.reflect.Method.invoke(Method.java:521)
02-02 18:36:51.079: V/SendMail(680): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
02-02 18:36:51.079: V/SendMail(680): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
02-02 18:36:51.079: V/SendMail(680): at dalvik.system.NativeStart.main(Native Method)
02-02 18:36:51.209: D/dalvikvm(132): GC_EXPLICIT freed 10073 objects / 444712 bytes in 94ms
02-02 18:36:51.219: V/Telephony(217): getOrCreateThreadId cursor cnt: 1
02-02 18:36:51.449: D/Mms:app(217): getSmsNewMessageNotificationInfo: count=34, first addr=123, thread_id=2
02-02 18:36:51.489: W/NotificationService(59): STOP command without a player
02-02 18:36:51.580: D/MediaPlayer(59): Couldn't open file on client side, trying server side
02-02 18:36:51.590: E/MediaPlayerService(34): Couldn't open fd for content://settings/system/notification_sound
02-02 18:36:51.590: E/MediaPlayer(59): Unable to to create media player
02-02 18:36:51.610: W/NotificationService(59): error loading sound for content://settings/system/notification_sound
02-02 18:36:51.610: W/NotificationService(59): java.io.IOException: setDataSource failed.: status=0x80000000
02-02 18:36:51.610: W/NotificationService(59): at android.media.MediaPlayer.setDataSource(Native Method)
02-02 18:36:51.610: W/NotificationService(59): at android.media.MediaPlayer.setDataSource(MediaPlayer.java:716)
02-02 18:36:51.610: W/NotificationService(59): at android.media.MediaPlayer.setDataSource(MediaPlayer.java:671)
02-02 18:36:51.610: W/NotificationService(59): at com.android.server.NotificationPlayer$CreationAndCompletionThread.run(NotificationPlayer.java:88)
NOTE: The above code is working fine when I join it into one class..
It appears your calling getContentResolver() on the class Context, instead of an instance of Context.
Change your method:
public String getContactNumber(String number)toString getContactNumber(String number, Context context)and use context.getContentResolver() instead of Context.getContentResolver();
You pass the context from the broadcastreciever to your getContactDisplayNameByNumber class