Sending ordered broadcast to a specific receiver? - java

How can I send an orderedBroadcast to a specific receiver? I am broadcasting the ACTION_MEDIA_BUTTON and only want a specific app (Winamp for example) to receive it. I don't want Pandora to steal it. Is there a way to explicitly define who the receiver will be?
void sendBroadcast() {
Intent i = new Intent(Intent.ACTION_MEDIA_BUTTON);
synchronized (this) {
i.putExtra(Intent.EXTRA_KEY_EVENT, new KeyEvent(KeyEvent.ACTION_DOWN, theKeyEvent));
ctx.sendOrderedBroadcast(i, null);
i.putExtra(Intent.EXTRA_KEY_EVENT, new KeyEvent(KeyEvent.ACTION_UP, theKeyEvent));
ctx.sendOrderedBroadcast(i, null);
}
}

If you know the package of the Winamp application, you can use setPackage to restrict Intent resolution to only a specific package.

Related

Hide push notification if it contains specific elements

I won't show a received push notification from appearing top notifications menu my notification if it has for example key update. For now if I get notification with this key, all notifications are in the notification bar. I want to not present this notifications for user.
I'm using WakefulBroadcastReceiver for handle notifications like below:
public class PusherReceiver extends WakefulBroadcastReceiver {
private boolean isAppOnForeground(Context context) {
ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningAppProcessInfo> appProcesses = activityManager.getRunningAppProcesses();
if (appProcesses == null)
return false;
final String packageName = context.getPackageName();
for (ActivityManager.RunningAppProcessInfo appProcess : appProcesses) {
if (appProcess.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND && appProcess.processName.equals(packageName)) {
return true;
}
}
return false;
}
public void onReceive(final Context context, Intent intent) {
Log.i("SimpleWakefulReceiver", "Starting service # " + SystemClock.elapsedRealtime());
if (!isAppOnForeground((context))) {
String pushNotificationBody = intent.getStringExtra("alert");
try {
JSONObject notificationData = new JSONObject(pushNotificationBody);
// This is the Intent to deliver to our service.
Intent service = new Intent(context, BackgroundService.class);
// Put here your data from the json as extra in in the intent
service.putExtra("notification", pushNotificationBody);
Log.i("PUSH_NOTIFICATION_JSON", "RECEIVED JSON " + notificationData);
// Start the service, keeping the device awake while it is launching.
if (!notificationData.has("update")) {
startWakefulService(context, service);
} else {
// Do nothing
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}
}
UPDATE:
I changed project a little and with Onesignal and his NotificationExtenderService, I did something like below:
public class NotificationNotDisplayingExtender extends NotificationExtenderService {
#Override
protected boolean onNotificationProcessing(OSNotificationReceivedResult receivedResult) {
String notification = receivedResult.toString();
String notificationBody = receivedResult.payload.body;
JSONObject notificationBodyJSON = null;
try {
notificationBodyJSON = new JSONObject(notificationBody);
} catch (JSONException e) {
e.printStackTrace();
}
JSONObject pushNotificationData = notificationBodyJSON;
boolean hidden = false;
if (pushNotificationData.has("update")) {
Log.i("NOTIFICATION MANAGER", "PREVENT DISPLAY NOTIFICATION");
hidden = true;
}
// Return true to stop the notification from displaying.
return hidden;
}
}
And it prevent displaying notifications with update key, but now I don't receive it in my PusherReceiver to start my service. Is there easy way to send data from my NotificationNotDisplayingExtender receivedResult to my PusherReceiver?
For now it looks like my PusherReceiver don't fire his onReceive method.
Many thanks for help in advance.
There are two types of payload.
1. Data
2. Notification
https://developers.google.com/cloud-messaging/concept-options
Use only data payload. Then you always get the call in FirebaseMessagingService onMessageRececived Method
The thing is basically we have two type of notifications.
One which can be called Notification Type, is that the push has a notification object in sent/received bundle, in which you have to handle it when your app is in foreground and the notification is received. In this case, if your app is in foreground, then you can handle it and do whatever you like which is not showing a notification. But if the app is in background, a notification will automatically create by google and it takes predefined title and message objects within the received push bundle to make the notification.
Second type which can be called Data Type, do not have any notification object in the sent/received bundle. In this scenario, your app is in foreground or background, you should handle everything. So, if you put your data in data object of your push notification message, everything will be in your hands.
So, in short, just put your data in data object of your notification and implement your desired logic.
I do not see the JSON data you are referring to. However, I suppose the update key in your JSON is containing null. In your code you are checking if the JSON data has the key update in it. This function will always return true if the key exists in the JSON body. You might have the field with null value which is indicating that you are not supposed to show the notification in the system tray.
In that case, you might consider using isNull function. It returns true if this object has no mapping for update or if it has a mapping whose value is null.
// Start the service, keeping the device awake while it is launching.
if (!notificationData.isNull("update")) {
startWakefulService(context, service);
} else {
// Do nothing
}
And yes, please use the data payload from the notification that you get.
Every time you notify the NotificationManager to show a notification, you provide an ID to be used for the notification to edit or cancel that notification later on. If you show a notification by manager.notify(notificationId, notification), you can cancel it with manager.cancel(notificationId).
If you want to remove all the notifications, you can use NotificationManager.cancelAll().

Communicate between service and activity

I have a class in a service
MyClass m = new MyClass();
and inside my class I check if I have permission to overlay the view; if so, it's ok, otherwise I must start an activity
if (Settings.canDrawOverlays(mContext)) {
// draw over app
} else {
// start the activity
Intent i = new Intent(context,Calls.class);
context.startActivity(i);
}
When I start the activity I have a problem communicating between the class and the activity. I know how to use the interface but how can I register it in activity.
Some time I want to pass an object or data from the class to the activity or from the activity to the class... how can I do that?
I saw many examples in Stack Overflow about how to communicate between service and activity; they suggest to start the class from the activity but this does not work in my app because my class must be running all the time.
Perhaps you could use an event bus like mechanism where you can send or receive events through out your app, Though there are several libraries out there, I would recommend using Otto library for android.
Usage is pretty simple just register in your activity onCreate
Bus bus = new Bus();
bus.register(this);
For sending events
// example data to post
public class TestData {
public String message;
}
// post this data
bus.post(new TestData().message="Hello from the activity");
And subscribe to events like this
#Subscribe public void getMessage(TestData data) {
// TODO: React to the event somehow!
}
More info here
If you want to implement a communication pattern between a Service and an Activity, you should use a LocalBroadcastManager.
It will turn handy because, in case your Service is still on but your Activity
has been destroyed (very common situation), then the 'messagging' between the two will simply have no effect (no NPE or whatsoever will be thrown).
Step 1
Create a BroadcastReceiver in your Activity and define an ID / Filter
this.localBroadcastReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
// Do what you have to do here if you receive data from the Service / Background Task
}
}
public static final IntentFilter SIGNAL_FILTER = new IntentFilter("com.you.yourapp.MY_SIGNAL")
Step 2
In your Activity register the broadcast in onResume() and unregister it in onPause().
#Override
protected void onResume() {
// Listen if a Service send me some data
LocalBroadcastManager.getInstance(getApplicationContext()).registerReceiver(this.localBroadcastReceiver, SIGNAL_FILTER);
}
#Override
protected void onPause() {
// I'm going to the background / or being destroyed: no need to listen to anything anymore...
LocalBroadcastManager.getInstance(getApplicationContext()).unregisterReceiver(this.localBroadcastReceiver);
}
Your Activity is now ready to receive data from any other component in your Application.
If it's in the background, then there is no need to update the UI: in fact the Activity will not respond if in the background.
In the same way, if it's being garbage collected, the Receiver will be unregistered and the Activity will just not respond to anything.
If the Activity is resumed / restarted, onResume() will be triggered and the Receiver will be registered again.
Step 3
All you need to do right now, is send data from the Service.
Simply call
final Intent intent = new Intent();
intent.setAction(SomeActivity.SIGNAL_FILTER);
// put your data in intent
LocalBroadcastManager.getInstance(getApplicationContext()).sendBroadcast(intent);
and your Activity will accordingly respond to the signal.
It's surprising how few people know about the LocalBroadcastManager and instead use some self-implemented callback / singleton pattern, which increases complexity and non-readability.
This pattern is built-in in Android, so you don't need external libraries. As for security, this ensures that your signals stay internal to your application: no data can therefore be read by other apps.
I similarly answered to another question here.

Android detect incoming calls?

I am trying to develop an app where it will turn on your ringer if someone calls you a certain amount of times in a row in a certain period of time. This is my first real app, so I'm a little stuck.
How would I record whenever a call is received in an internal list? Would this need to be a service to always be running, or could this work in a normal app by just receiving the intent of the dialer app?
I apologize if this question is a little vague.
The best way to do it is, by declaring your broadcast receiver in the manifest, this will cause that the code on your BroadcastReceiver class to get executed everytime the event is fired, without the need of a service running in the background all the time, let the OS handle the observing part for you...
<receiver android:name=".ReceiverExample">
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE" />
</intent-filter>
</receiver>
Now, in your broadcastreceiver class "ReceiverExample", create a SharedPreference to store the number of incomming calls, and based on that, you can validate if is time to do something else or not...
public class ReceiverExample extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
//Logic to listen incoming calls, and keep track of them using Shared Preferences..
}
}
Services are good for long tasks but the OS it self is well suited to Monitor/Observe events (like Telephony events e.g. Incomming calls...), try not to re-do the OS work by creating Services just to monitor already known events...
Regards
use single Tone Class for recording
public class Recording {
private static MediaRecorder recorder;
private File audiofile;
private static Recording mInstance;
public MediaRecorder getRecorder() {
System.out.println("From singleton..!!!");
return recorder;
}
public static Recording getInstance(Context context) {
return mInstance == null ? (mInstance = new Recording(context))
: mInstance;
}
private Recording(Context context) {
System.out.println("Again initiated object");
File sampleDir = Environment.getExternalStorageDirectory();
try {
audiofile = File.createTempFile("" + new Date().getTime(), ".amr",
sampleDir);
} catch (IOException e) {
e.printStackTrace();
return;
}
recorder = new MediaRecorder();
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setOutputFormat(MediaRecorder.OutputFormat.AMR_NB);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
recorder.setOutputFile(audiofile.getAbsolutePath());
}
}

Send Object from Service to Activity (Can't marshal non-Parcelable)

I'm trying to send data from my activity to a service and receive some information back, but i'm getting:
java.lang.RuntimeException: Can't
marshal non-Parcelable objects across
processes.
The code from activity looks like this:
Message msg = Message.obtain(null, 1);
msg.obj=1;
msg.replyTo=new Messenger(new PlanRequestIncomingHandler());
try {
msgService.send(msg);
} catch (RemoteException e) {
Log.i(tag, "Can not send msg to service");
e.printStackTrace();
}
When I set msg.obj = something I get java.lang.RuntimeException, can somebody help me?
You can pass Parcelable type objects via Messenger. Or else if you want to pass primitive data types use Bundle wrapper as below.
In Service End:
//Create a bundle object and put your data in it
Bundle bundle = new Bundle();
bundle.putInt("key", 1);
Message msg = Message.obtain(null, 123);
msg.obj = bundle;
msg.replyTo = new Messenger(new PlanRequestIncomingHandler());
try {
msgService.send(msg);
} catch (RemoteException e) {
Log.i(tag, "Can't send msg to service");
e.printStackTrace();
}
In Activity End:
switch(msg.what) {
case 123:
if(msg.obj != null) {
Bundle bundle = (Bundle) msg.obj;
System.out.println("Got integer "+ bundle.getInt("key"));
}
break;
}
cheers :-)
Old question, but I am answering so it might help someone in the future.
If you are using actual objects, then by all means, please implement Parcelable
Android: How to implement Parcelable to my objects?
However, as the OP stated that he tried using Primitives and it did not work, this is what to do.
The problem lies here msg.obj=1; This expects an actual object that implements Parcelable
Use msg.arg1 = 1;
you can then retrieve the argument on the service side using msg.arg1
For simplicity I use (straight from my code)
Message msg = Message.obtain(null, PlayerService.MSG_ACTION_SEEK, i, -1);
The -1 is just a holder for me.
Hope this helps.
Edit:
Be careful with
Message msg = Message.obtain(null, PlayerService.MSG_ACTION_SEEK, i);
This signature is equivalent to the OP's first attempt and expects a Parcelable and is what actually tripped me and got me searching in the first place. It won't throw an error until runtime.
Other than primitive data, the objects you're juggling between Activities and Services need to implement Parcelable and preferably Serializable.
I hope this helps,
Best
-serkan
You must use the Bundle to pass the conventional type data, otherwise it will be wrong:
Java.lang.RuntimeException: Can't non-Parcelable objects across marshal processes.
Because the Binder transaction data is called Parcel, the Parcelable interface must be implemented, otherwise it is not possible to communicate between the two applications. The reason why Bundle is passed because the class implements the Parcelable interface. Of course, if you want to pass the class must also implement the interface.
you can write like down:
Message msg = Message.obtain(null, 1);
msg.getData().putInt("key",1);
msg.replyTo=new Messenger(new PlanRequestIncomingHandler());
try {
msgService.send(msg);
} catch (RemoteException e) {
Log.i(tag, "Can not send msg to service");
e.printStackTrace();
}
sorry,my english is very bad
I have implemented Actor Model (like Akka) for Android, since Akka requires Java 8, i made my own implementation for it for Android, using RxJava2 , it was very easy to implement ... And once it is there, you can send messages holding any object to any receiver (Activity, Fragment, Service, Pojo, etc.) without worrying about threads or serialization
It is hard to explain my own implementation in details if you dont know what is Actor Model, but if you do, you can make an interface named "Actor" with one method
void onMessageReceived(Message message);
And you can implement this interface by any Actor you have, and to register any Actor, you can create an ActorSystem class that has methods :
static void register(Actor actor, PublishSubject<Message> mailbox);
static void unregister(Actor actor);
static void send(Message message, Class<? extends Actor> ... actors);
And when you register your Actor (Activity or Service), you decide what is your thread / scheduler to receive your messages on, through :
PublishSubject.observeOn(Schedulers.trampoline());
And you register your Actor in onCreate() and unRegister in onDestroy()
Or if you want a library for that (but i did not test it), you can take a look at this :
https://github.com/actorapp/droidkit-actors

Using an IntentService to do Prioritized Networking

I've been wondering if it is possible to use an IntentService to do some networking while keeping the queue of pending intents prioritized. My goal to be able to download some images in the background, add more if needed (send another Intent) and be able to reset the queue if necessary (preferably using a specific Intent). That is all possible with an IntentServie but when I send that 'stop' Intent it needs to be processed as the next item in the queue, not the last where it is right now.
EDIT
For those interested I have taken the AOSP code for IntentService and modified it to meet my needs. The reason I cannot just subclass IntentHandler is because of the private ServiceHandler class inside of IntentHandler.
Inside of the ServiceHandler I have a new method:
public final boolean sendPriorityMessage(Message msg)
{
int priority = msg.arg2;
//Log.i(GenericList.TAG,"recieved message priority: "+priority);
if(priority>PRIORITY_NORMAL){
return sendMessageAtFrontOfQueue(msg);
}else{
return sendMessage(msg);
}
}
This method is called from onStart instead of just sendMessage
#Override
public void onStart(Intent intent, int startId) {
Message msg = mServiceHandler.obtainMessage();
msg.arg1 = startId;
msg.obj = intent;
try{
msg.arg2 = intent.getExtras().getInt(KEY_PRIORITY);
}catch(Exception e){
msg.arg2 = PRIORITY_NORMAL;
}
mServiceHandler.sendPriorityMessage(msg);
}
Overall the code is still limited but I am able to fast track some messages to the front of the queue, which is what I was after anyway.
you could implement/extend your own PriorityQueue that simply checks every new intent added to the queue. if it's the stop intent, it moves it straight to the front of the line.

Categories

Resources