I am working on an application that needs to get some sort of notification
/ Receiver
when the WhatsApp call is being started (Both on the Caller and Receiver end) or ended. Is it possible to get Incoming/Outgoing WhatsApp call information within my application?
I have tried to use Accessibility Service
Using package name as "com.whatsapp
", I'm unable to fulfil my requirement.
Will anyone suggest me what should I do? Or Can this actually be done? IF yes, Then please explain how.
Creating a BroadcastReceiver The intent object is passed with all the additional data. A Context object is also available and is used to start an activity or service using context. startActivity(myIntent); or context.
Definition. A broadcast receiver (receiver) is an Android component which allows you to register for system or application events. All registered receivers for an event are notified by the Android runtime once this event happens.
A broadcast receiver is a dormant component of the Android system. Only an Intent (for which it is registered) can bring it into action. The Broadcast Receiver's job is to pass a notification to the user, in case a specific event occurs. Using a Broadcast Receiver, applications can register for a particular event.
I tried it and I am able to get capture the whatsapp call button click and call end button click actions. Below is the simple AccessibilityService that I used and it no more different than the example available in the Android Developers website
public class MyAccessibilityService extends AccessibilityService {
@Override
protected void onServiceConnected() {
AccessibilityServiceInfo info = new AccessibilityServiceInfo();
// Set the type of events that this service wants to listen to. Others
// won't be passed to this service.
info.eventTypes = AccessibilityEvent.TYPE_VIEW_CLICKED |
AccessibilityEvent.TYPE_VIEW_FOCUSED;
// If you only want this service to work with specific applications, set their
// package names here. Otherwise, when the service is activated, it will listen
// to events from all applications.
info.packageNames = new String[]
{"com.whatsapp","com.android.calendar"};
// Set the type of feedback your service will provide.
info.feedbackType = AccessibilityServiceInfo.FEEDBACK_SPOKEN;
// Default services are invoked only if no package-specific ones are present
// for the type of AccessibilityEvent generated. This service *is*
// application-specific, so the flag isn't necessary. If this was a
// general-purpose service, it would be worth considering setting the
// DEFAULT flag.
// info.flags = AccessibilityServiceInfo.DEFAULT;
info.notificationTimeout = 100;
this.setServiceInfo(info);
}
@Override
public void onAccessibilityEvent(AccessibilityEvent event) {
final int eventType = event.getEventType();
String eventText = null;
switch(eventType) {
case AccessibilityEvent.TYPE_VIEW_CLICKED:
eventText = "Focused: ";
break;
case AccessibilityEvent.TYPE_VIEW_FOCUSED:
eventText = "Focused: ";
break;
}
//eventText = eventText + event.getContentDescription();
// Do something nifty with this text, like speak the composed string
// back to the user.
Toast.makeText(getApplicationContext(),""+eventText +" --- "+event.getContentDescription(),Toast.LENGTH_LONG).show();
}
@Override
public void onInterrupt() {
}
}
In the above code I have showed a toast message and the trick for drawable we will be providing the contentDescription which could be used by the system while in "Talkback" accessibility mode. Hope this helps!!!
Let's solve the query.... Accessibility Service will help you to get notified that when you will receive the notifications against your required package name. for example "com.whatsapp".
Now good thing is that you can parse the notification message within since Android 4.2 within Accessibility Service by a little effort. Unluckily for you, There was a github project that was exactly doing your desired thing but it is currently unavailable.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With