0

i'm trying to make a phone call, when a specific notification arrive, i use Notification Service Listener to read incoming notificaion,

public void onNotificationPosted(StatusBarNotification sbn) {
    //  if(if this is my notificaion..){
    String name = sbn.getNotification().extras.getCharSequence(Notification.EXTRA_TITLE));
    List<String> numbers = getPhoneNumbers(name);
    Log.d(TAG, "i have all this numbers - " + numbers.toString());

    Intent intent = new Intent(Intent.ACTION_CALL);
    intent.setData(Uri.parse("tel:" + numbers.get(1)));
    startActivity(intent);
}

the "getPhoneNumbers" method is this one

public List<String> getPhoneNumbers(String name) {
    List<String> numbers = new ArrayList<String>();

    ContentResolver cr = getContentResolver();
    Cursor cursor = cr.query(ContactsContract.Contacts.CONTENT_URI, null,
            "DISPLAY_NAME = '" + name + "'", null, null);
    if (cursor.moveToFirst()) {
        String contactId =
                cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
        //  Get all phone numbers.
                    Cursor phones = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
                ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = " + contactId, null, null);
        while (phones.moveToNext()) {
            String number = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
           numbers.add(number);

        }
        phones.close();
    }

    cursor.close();
    return numbers;

}

all work fine, ( i used break points to cheak everything...) the "if this is my notification" work perfect, i get the name from the sbn extras, the 'numbers' arraylist include all the contact numbers after the "getPhoneNumbers" method used, but when i start the intent nathing happend..

what is my problem? :/

Ziem
  • 6,579
  • 8
  • 53
  • 86
Didi78
  • 247
  • 1
  • 15

1 Answers1

1

Let's clarify:

Problem
Your onNotificationPosted method does not launch a phone call when calling startActivity(intent);.

Why
NotificationListenerService is not an activity.

Solution
Make your MainActivity call the startActivity(intent);.

How
Define an attribute activity in NotificationListenerService and define a constructor that accepts an activity:

NotificationListenerService.java

// define attribute activity:
MainActivity activity;

public NotificationListenerService(MainActivity activity) {
    this.activity = activity;
}

MainActivity.java

// create a NotificationListenerService sending itself as reference
NotificationListenerService nls = new NotificationListenerService(this);

Then inside the onNotificationPosted you will see the attribute, so you can:

Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:"+ numbers.get(1)));
activity.startActivity(intent);
Jordi Castilla
  • 26,609
  • 8
  • 70
  • 109
  • i want numbers.get(1).. (there is more then 5 numbers for this contact, so i think thats not the problem...), and this soulation doesnt work :/ – Didi78 May 21 '15 at 16:18
  • @Didi78 check my edit, i think problem is you can't see `MainActivity` inside `onNotificationPosted`.... – Jordi Castilla May 21 '15 at 16:22
  • you say that i need to define myActivity = this; in the onNotificationPosted ? i think that's isnt possible because NotificationListenerService isnt an activity.. or maybe i didnt understand you well? i need to define it in my MainActivity? (i have one..) – Didi78 May 21 '15 at 16:27
  • Yes, your main activity must start the phone call intent – Jordi Castilla May 21 '15 at 16:31
  • well, 30 mins and i didnt figure out what did you mean too, in the main activity i need the - public Activity getActivity() { return this; } that's ok, but what next? the "public Activity myActivity = this" inside NotificationListenerService still doesnt work, and if you say that my main activity must start the phone call intent, so - 1. why should i write the "Intent intent = new In(..; myActivity.startActivity(intent) inside the onNotificationPosted (like you wrote..) 2. i dont want any layout to be opened when the notificiation arrive, just start the call.. sorry for my stupidity :/ – Didi78 May 21 '15 at 17:22
  • @Didi78 sorry for delay, yesterday i got no time. I totally updated my answer to clarify exactly what I meant, also I used a simpler technique, let me now if further doubts... – Jordi Castilla May 22 '15 at 07:38
  • thanks but i found a simpller solution to add 'Intent.FLAG_FROM_BACKGROUND' and then i can make a call from the service itself, according to http://stackoverflow.com/questions/11915086/android-make-phone-call-from-service , thank you very much for all your help!! – Didi78 May 22 '15 at 07:48