Skip to content

Commit 0debb73

Browse files
Added NotificationListener 4.4
1 parent d9d6c90 commit 0debb73

14 files changed

Lines changed: 338 additions & 0 deletions

File tree

NotificationListener44/README.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# Notification Listener with Android 4.4
2+
3+
NotificationListenerService and kitkat.
4+
5+
This repository contains example source code
6+
7+
For more info about the blog :
8+
* [NotificationListenerService and kitkat]
9+
10+
11+
**It uses a Notification Listener Service (4.3+). You have to enable it in Android Settings**
12+
13+
14+
15+
![ScreenShot](https://github.com/gabrielemariotti/androiddev/raw/master/NotificationListener/image.png)
16+
17+
18+
Credits
19+
-------
20+
21+
Author: Gabriele Mariotti ([email protected])
22+
23+
License
24+
-------
25+
26+
Copyright 2013 Gabriele Mariotti
27+
28+
Licensed under the Apache License, Version 2.0 (the "License");
29+
you may not use this file except in compliance with the License.
30+
You may obtain a copy of the License at
31+
32+
http://www.apache.org/licenses/LICENSE-2.0
33+
34+
Unless required by applicable law or agreed to in writing, software
35+
distributed under the License is distributed on an "AS IS" BASIS,
36+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
37+
See the License for the specific language governing permissions and
38+
limitations under the License.
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
buildscript {
2+
repositories {
3+
mavenCentral()
4+
}
5+
dependencies {
6+
classpath 'com.android.tools.build:gradle:0.6.+'
7+
}
8+
}
9+
apply plugin: 'android'
10+
11+
repositories {
12+
mavenCentral()
13+
}
14+
15+
android {
16+
compileSdkVersion 19
17+
buildToolsVersion "19.0.0"
18+
19+
defaultConfig {
20+
minSdkVersion 19
21+
targetSdkVersion 19
22+
}
23+
24+
}
25+

NotificationListener44/image.png

118 KB
Loading
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
3+
package="it.gmariotti.android.examples.notificationlistener"
4+
android:versionCode="1"
5+
android:versionName="1.0" >
6+
7+
<uses-sdk
8+
android:minSdkVersion="19"
9+
android:targetSdkVersion="19" />
10+
11+
<application
12+
android:allowBackup="true"
13+
android:debuggable="true"
14+
android:label="@string/app_name"
15+
android:icon="@drawable/ic_launcher"
16+
android:theme="@android:style/Theme.Holo.Light">
17+
18+
<activity
19+
android:name=".MainActivity"
20+
android:label="Notification Listener">
21+
<intent-filter>
22+
<action android:name="android.intent.action.MAIN" />
23+
<category android:name="android.intent.category.LAUNCHER" />
24+
</intent-filter>
25+
</activity>
26+
27+
28+
<service
29+
android:name="it.gmariotti.android.examples.notificationlistener.SimpleKitkatNotificationListener"
30+
android:label="@string/service_name"
31+
android:debuggable="true"
32+
android:permission="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE" >
33+
<intent-filter>
34+
<action android:name="android.service.notification.NotificationListenerService" />
35+
</intent-filter>
36+
37+
</service>
38+
39+
</application>
40+
41+
</manifest>
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
package it.gmariotti.android.examples.notificationlistener;
2+
3+
import android.app.Activity;
4+
import android.app.Notification;
5+
import android.content.BroadcastReceiver;
6+
import android.content.Context;
7+
import android.content.Intent;
8+
import android.content.IntentFilter;
9+
import android.graphics.Bitmap;
10+
import android.os.Bundle;
11+
import android.view.Menu;
12+
import android.view.MenuInflater;
13+
import android.view.MenuItem;
14+
import android.widget.ImageView;
15+
import android.widget.TextView;
16+
17+
/**
18+
* @author Gabriele Mariotti ([email protected])
19+
*/
20+
public class MainActivity extends Activity {
21+
22+
protected MyReceiver mReceiver = new MyReceiver();
23+
public static String INTENT_ACTION_NOTIFICATION = "it.gmariotti.notification";
24+
25+
protected TextView title;
26+
protected TextView text;
27+
protected TextView subtext;
28+
protected ImageView largeIcon;
29+
30+
public void onCreate(Bundle savedInstanceState) {
31+
super.onCreate(savedInstanceState);
32+
setContentView(R.layout.activity_main);
33+
34+
//Retrieve ui elements
35+
title = (TextView) findViewById(R.id.nt_title);
36+
text = (TextView) findViewById(R.id.nt_text);
37+
subtext = (TextView) findViewById(R.id.nt_subtext);
38+
largeIcon = (ImageView) findViewById(R.id.nt_largeicon);
39+
40+
}
41+
42+
@Override
43+
public boolean onCreateOptionsMenu(Menu menu) {
44+
MenuInflater inflater = getMenuInflater();
45+
inflater.inflate(R.menu.main, menu);
46+
return super.onCreateOptionsMenu(menu);
47+
}
48+
49+
@Override
50+
public boolean onOptionsItemSelected(MenuItem item) {
51+
switch (item.getItemId()) {
52+
case R.id.action_autorize:
53+
Intent intent = new Intent("android.settings.ACTION_NOTIFICATION_LISTENER_SETTINGS");
54+
startActivity(intent);
55+
return true;
56+
}
57+
return super.onOptionsItemSelected(item);
58+
}
59+
60+
@Override
61+
protected void onResume() {
62+
super.onResume();
63+
if (mReceiver == null) mReceiver = new MyReceiver();
64+
registerReceiver(mReceiver, new IntentFilter(INTENT_ACTION_NOTIFICATION));
65+
}
66+
67+
@Override
68+
protected void onPause() {
69+
super.onPause();
70+
unregisterReceiver(mReceiver);
71+
}
72+
73+
public class MyReceiver extends BroadcastReceiver {
74+
75+
@Override
76+
public void onReceive(Context context, Intent intent) {
77+
78+
if (intent != null) {
79+
Bundle extras = intent.getExtras();
80+
String notificationTitle = extras.getString(Notification.EXTRA_TITLE);
81+
int notificationIcon = extras.getInt(Notification.EXTRA_SMALL_ICON);
82+
Bitmap notificationLargeIcon = ((Bitmap) extras.getParcelable(Notification.EXTRA_LARGE_ICON));
83+
CharSequence notificationText = extras.getCharSequence(Notification.EXTRA_TEXT);
84+
CharSequence notificationSubText = extras.getCharSequence(Notification.EXTRA_SUB_TEXT);
85+
86+
title.setText(notificationTitle);
87+
text.setText(notificationText);
88+
subtext.setText(notificationSubText);
89+
90+
if (notificationLargeIcon != null) {
91+
largeIcon.setImageBitmap(notificationLargeIcon);
92+
}
93+
}
94+
95+
}
96+
}
97+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package it.gmariotti.android.examples.notificationlistener;
2+
3+
import android.app.Notification;
4+
import android.app.PendingIntent;
5+
import android.content.Intent;
6+
import android.os.Bundle;
7+
import android.service.notification.NotificationListenerService;
8+
import android.service.notification.StatusBarNotification;
9+
10+
/**
11+
* @author Gabriele Mariotti ([email protected])
12+
*/
13+
public class SimpleKitkatNotificationListener extends NotificationListenerService {
14+
15+
@Override
16+
public void onCreate() {
17+
super.onCreate();
18+
//android.os.Debug.waitForDebugger();
19+
}
20+
21+
@Override
22+
public void onNotificationPosted(StatusBarNotification sbn) {
23+
Notification mNotification=sbn.getNotification();
24+
if (mNotification!=null){
25+
Bundle extras = mNotification.extras;
26+
27+
Intent intent = new Intent(MainActivity.INTENT_ACTION_NOTIFICATION);
28+
intent.putExtras(mNotification.extras);
29+
sendBroadcast(intent);
30+
31+
Notification.Action[] mActions=mNotification.actions;
32+
if (mActions!=null){
33+
for (Notification.Action mAction:mActions){
34+
int icon=mAction.icon;
35+
CharSequence actionTitle=mAction.title;
36+
PendingIntent pendingIntent=mAction.actionIntent;
37+
}
38+
}
39+
}
40+
}
41+
42+
@Override
43+
public void onNotificationRemoved(StatusBarNotification sbn) {
44+
45+
}
46+
}
5.38 KB
Loading
3.12 KB
Loading
1.63 KB
Loading
8.01 KB
Loading

0 commit comments

Comments
 (0)