【问题标题】:make phone call when specific notification arrive特定通知到达时拨打电话
【发布时间】:2015-05-21 16:14:11
【问题描述】:

当特定通知到达时,我正在尝试拨打电话, 我使用通知服务侦听器来读取传入的通知,

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);
}

“getPhoneNumbers”方法就是这个

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;

}

一切正常,(我用断点来欺骗一切......) “如果这是我的通知”工作完美,我从 sbn extras 中获取名称,“数字”数组列表包括使用“getPhoneNumbers”方法之后的所有联系号码,但是当我开始意图时,nathing 发生了..

我的问题是什么? :/

【问题讨论】:

    标签: java android notifications call


    【解决方案1】:

    让我们澄清一下:

    问题
    您的onNotificationPosted 方法在调用startActivity(intent); 时不会发起电话呼叫。

    为什么
    NotificationListenerService 不是活动。

    解决方案
    让您的MainActivity 拨打startActivity(intent);

    如何
    NotificationListenerService中定义一个属性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);
    

    然后在onNotificationPosted里面你会看到属性,所以你可以:

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

    【讨论】:

    • 我想要 numbers.get(1)..(这个联系人有超过 5 个号码,所以我认为这不是问题......),这个解决方案不起作用:/跨度>
    • @Didi78 检查我的编辑,我认为问题是您在onNotificationPosted 内看不到MainActivity....
    • 你说我需要定义 myActivity = this;在 onNotificationPosted 中?我认为这是不可能的,因为 NotificationListenerService 不是一项活动.. 或者我不太了解你?我需要在我的 MainActivity 中定义它吗? (我有一个..)
    • 是的,您的主要活动必须启动电话呼叫意图
    • 好吧,30 分钟,我也没明白你的意思,在主要活动中我需要 - public Activity getActivity() { return this;没关系,但接下来呢? NotificationListenerService 中的“public Activity myActivity = this”仍然不起作用,如果您说我的主要活动必须启动电话呼叫意图,那么 - 1. 我为什么要写“Intent intent = new In(..; myActivity. onNotificationPosted 中的 startActivity(intent) (就像你写的那样..) 2. 我不想在通知到达时打开任何布局,只需开始通话.. 对不起我的愚蠢:/
    【解决方案2】:

    找到了从服务发起呼叫的解决方案:

    Intent intent = new Intent(Intent.ACTION_CALL);
                intent.setData(Uri.parse("tel:" + number));
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                intent.addFlags(Intent.FLAG_FROM_BACKGROUND);
                startActivity(intent);
    

    解决方案来自:Android: Make phone call from service

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-12-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多