Reputation: 103
I wanted to display notifications that are posted by other applications in the status bar. So I made a button. Clicking the button would call executeButtonClick()
method. My code :
public void executeButtonClick(View view) {
NLService nlService = new NLService();
if(nlService.getActiveNotifications()!=null) {
Toast.makeText(MainActivity.this,"InsideIf",Toast.LENGTH_SHORT).show();
for (StatusBarNotification sbn : nlService.getActiveNotifications()) {
String temp = "Package Name: " + sbn.getPackageName() +
"\n" + "Title: " + sbn.getNotification().extras.getString("android.title") + "\n" +
"Text: " + sbn.getNotification().extras.getCharSequence("android.text");
String newText = textView.getText().toString() + temp;
textView.setText(newText);
}
}
}
But the notification is not shown and I am getting null pointer exception:
"Attempt to invoke virtual method 'java.lang.String android.content.Context.getPackageName()' on a null object reference"
in the following line: activity.executeButtonClick(com.example.asus.notificationtest.MainActivity.textView);
of the onNotificationPosted()
method mentioned below:
public class NLService extends NotificationListenerService {
Context context;
@Override
public void onCreate() {
super.onCreate();
context = getApplicationContext();
}
@Override
public void onNotificationPosted(StatusBarNotification sbn) {
if(MainActivity.textView != null)
activity.executeButtonClick(com.example.asus.notificationtest.MainActivity.textView);
//Toast.makeText(this,"Post from: "+sbn.getPackageName(),Toast.LENGTH_SHORT).show();
}
@Override
public void onNotificationRemoved(StatusBarNotification sbn) {
//Toast.makeText(this,"Post from: "+sbn.getPackageName(),Toast.LENGTH_SHORT).show();
}
Upvotes: 1
Views: 3630
Reputation: 176
There are a number of steps for getting a Notification listener service working? Are the notifications being detected at all?
Steps: 1) Request permission in the manifest (for instance):
<service android:name=".TheNotificationListener"
android:label="NotifiationListener"
android:permission="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE">
<intent-filter>
<action android:name="android.service.notification.NotificationListenerService" />
</intent-filter>
2) Request run time permission from the user:
private void assessPermissions() {
if(isPermissionRequired()){
requestNotificationPermission();
}else{
startBackground();
}
}
public boolean isPermissionRequired() {
ComponentName cn = new ComponentName(this, TheNotificationListener.class);
String flat = Settings.Secure.getString(this.getContentResolver(), "enabled_notification_listeners");
final boolean enabled = flat != null && flat.contains(cn.flattenToString());
return !enabled;
}
private void requestNotificationPermission() {
Intent intent=new Intent("android.settings.ACTION_NOTIFICATION_LISTENER_SETTINGS");
startActivityForResult(intent, 101);
}
HINT: Ensure that you remove binding operations from a notification listener service! It took me a day to figure out that I shouldn't have that code in there.
Upvotes: 3