Hello guys iam facing problem in android programming i want to make
program that receives messages in a broadcast and display
notifications on the notification bar and when i press on the
notification i want to display an activity includes alert dialog
contains the Contact Name

i Tried to put the code of getContactDisplayNameByNumber(String
number) inside normal activity with button and ((without))
notification and it's worked nicly idon't know where is the problem
please help
me the code like that



public class SMSReceiver extends BroadcastReceiver
{


@Override
public void onReceive(Context context, Intent intent)


{
//---get the SMS message passed in---
Bundle bundle = intent.getExtras();
SmsMessage[] msgs = null;
String txtMessage = "";

final NotificationManager mNotificationManager;
if (bundle != null)
{
    String PhoneNo = "";

//---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]);
PhoneNo = msgs[i].getOriginatingAddress();

txtMessage += msgs[i].getMessageBody().toString();


}//end of for
int icon = R.drawable.ic_launcher;
    CharSequence tickerText = "Location Request";
    long when = System.currentTimeMillis();
    //Context

    CharSequence contentTitle = "Hi ?";
    CharSequence contentText = "Sms from: "+PhoneNo;
    Intent notificationIntent = new Intent(context,
GetContactname.class);
    notificationIntent.putExtra("Phone",PhoneNo);

    PendingIntent contentIntent = PendingIntent.getActivity(context,
0, notificationIntent, 0);
    final Notification notification = new Notification(icon,
tickerText, when);
notification.setLatestEventInfo(context, contentTitle, contentText,
contentIntent);
    notification.flags |= Notification.FLAG_INSISTENT;
    notification.flags |= Notification.FLAG_AUTO_CANCEL;

    /** Retrieve reference from NotificationManager */
    String ns = Context.NOTIFICATION_SERVICE;
     mNotificationManager = (NotificationManager)
context.getSystemService(ns);

    /** Listener when the Broadcast starts  */
    mNotificationManager.notify(Nid, notification);
}//on receive
}// broad cast



public class GetContactname extends Activity{

     String Phno ="";




    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);
        String Name="";
Phno=getIntent().getStringExtra("Phone");
Name= getDisplayNameFromPhoneNo(Phno);


Toast.makeText(this,Name,Toast.LENGTH_SHORT).show();//just for testing
my code


}
 public String getContactDisplayNameByNumber(String number) {
            Uri uri =
Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI,
Uri.encode(number));
            String name = "?";

            ContentResolver contentResolver = 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));

                }
                else{
                    name=number;
                }
            }
            catch (Exception e) {
                  Toast.makeText(this, e.getMessage(),
Toast.LENGTH_SHORT);
                }

            return name;
        }
}

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to