I am trying to develop an android service that knows if there are any notifications and then take an action. I don't know how to manage notifications.
I want to know how notifications work, and how to listen for them.
Asked
Active
Viewed 217 times
-1
-
What do you mean "listen to notifications"? Do you mean that you want a service to somehow read notifications and act upon them? – ravemir Apr 25 '13 at 18:23
-
Also, do you know which notifications you want to act upon? Maybe there is a way around using BroadcastReceivers that can suit your needs better, if not, you may have to use the cumbersome Accessibility Service and ask the user for permission to use it, which is always suspicious. – ravemir Apr 25 '13 at 18:26
-
Alread read this or what kind of notifications do you mean? https://developer.android.com/guide/topics/ui/notifiers/notifications.html – luxer Apr 25 '13 at 18:27
-
for example when i receive a message i want to light on the led. – red-eno Apr 25 '13 at 18:48
-
I couldn't find an associated intent-filter for notifications. I initially thought this would be possible but now looking at all of my devices, the notifications seem firmly routed in the OS and it might not be possible to intercept/listen. – Pork 'n' Bunny Apr 25 '13 at 18:56
-
You should add a couple "for example" to your question so other people will have a better idea what you are asking. – StoneBird Apr 25 '13 at 19:07
-
so what i wanna do is to use the led to notify when any notification appears. i'll implement it on a service. – red-eno Apr 25 '13 at 20:06
-
For any notification? Then you need to use a TTS service to read notifications. – ravemir Apr 25 '13 at 21:07
-
what did you mean by TTS service? if you can give explanation :) – red-eno Apr 25 '13 at 21:32
1 Answers
0
As mentioned in this answer, you should use a BroadcastReceiver to capture the appropriate intents and perform your intended action.
You mentioned as an example that receiving an SMS message should trigger the LED light. As mentioned here, you should register your 'SMSReceiver' BroadcastReceiver like so:
// SMS Receiver
<receiver android:name="com.myexample.receivers.SMSReceiver" >
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
...and define its code somewhat like so:
public class SMSReceiver extends BroadcastReceiver {
private final String DEBUG_TAG = getClass().getSimpleName().toString();
private static final String ACTION_SMS_RECEIVED = "android.provider.Telephony.SMS_RECEIVED";
private Context mContext;
private Intent mIntent;
// Retrieve SMS
public void onReceive(Context context, Intent intent) {
mContext = context;
mIntent = intent;
String action = intent.getAction();
if(action.equals(ACTION_SMS_RECEIVED)){
String address, str = "";
int contactId = -1;
SmsMessage[] msgs = getMessagesFromIntent(mIntent);
// Iterate over the received messages with a loop,
// performing the desired actions during the process
}
}
public static SmsMessage[] getMessagesFromIntent(Intent intent) {
Object[] messages = (Object[]) intent.getSerializableExtra("pdus");
byte[][] pduObjs = new byte[messages.length][];
for (int i = 0; i < messages.length; i++) {
pduObjs[i] = (byte[]) messages[i];
}
byte[][] pdus = new byte[pduObjs.length][];
int pduCount = pdus.length;
SmsMessage[] msgs = new SmsMessage[pduCount];
for (int i = 0; i < pduCount; i++) {
pdus[i] = pduObjs[i];
msgs[i] = SmsMessage.createFromPdu(pdus[i]);
}
return msgs;
}
}
...as for the LED matter, most reports seem sketchy, but this answer has a dodgy workaround:
android.provider.Settings.System.putInt(context.getContentResolver(),
"notification_light_pulse", on ? 1 : 0);