当特定通知到达时拨打 phone 电话
make phone call when specific notification arrive
我正在尝试拨打 phone 电话,当收到特定通知时,
我使用 Notification Service Listener 来读取传入的通知,
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;
}
一切正常,(我用断点来欺骗一切......)
"if this is my notification" 工作完美,我从 sbn extras 中获得名称,'numbers' arraylist 包括使用 "getPhoneNumbers" 方法后的所有联系电话,但是当我开始意图 nathing happend..
我的问题是什么? :/
让我们澄清一下:
问题
您的 onNotificationPosted
方法在调用 startActivity(intent);
.
时不会启动 phone 调用
为什么
NotificationListenerService
不是 activity。
解决方案
让你的 MainActivity
调用 startActivity(intent);
.
如何
在NotificationListenerService
中定义一个属性activity
,并定义一个接受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);
找到了从服务启动调用的解决方案:
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
我正在尝试拨打 phone 电话,当收到特定通知时, 我使用 Notification Service Listener 来读取传入的通知,
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;
}
一切正常,(我用断点来欺骗一切......) "if this is my notification" 工作完美,我从 sbn extras 中获得名称,'numbers' arraylist 包括使用 "getPhoneNumbers" 方法后的所有联系电话,但是当我开始意图 nathing happend..
我的问题是什么? :/
让我们澄清一下:
问题
您的 onNotificationPosted
方法在调用 startActivity(intent);
.
为什么
NotificationListenerService
不是 activity。
解决方案
让你的 MainActivity
调用 startActivity(intent);
.
如何
在NotificationListenerService
中定义一个属性activity
,并定义一个接受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);
找到了从服务启动调用的解决方案:
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