I'm doing an application based on wifi direct. According with the documentation I was expecting to receive the WIFI_P2P_DISCOVERY_CHANGED_ACTION intent action when I call discoverPeers(), but for some reason this intent is never sent to my broadcast receiver. Any idea?
Thank you.
Related
I am writing a code that shows some logs when user speaks through mic. For that purpose I'm using AudioManager.ACTION_AUDIO_BECOMING_NOISY intent but it does not work for me. I s there any broadcast intent for that purpose?
I am trying to update my homescreen widget when there is a Do not Disturb change. Because WidgetProvider is a BroadcastReceiver in itself, I thought it would be simple. But I am not seeing any Toast when I switch Dnd on or off. Does it mean that the WidgetProvider can only send broadcasts but not receive them?
Here is my code:
#Override
public void onReceive(Context context, Intent intent) {
super.onReceive(context, intent);
String action = intent.getAction();
assert action != null;
if (action.equals(NotificationManager.ACTION_INTERRUPTION_FILTER_CHANGED)) {
System.out.println("Success!");
}
}
You do not show how you are registering for that broadcast. If it is in the manifest, the broadcast that you are trying to receive is not listed on the implicit broadcast exceptions "whitelist", so you may not receive it on Android 8.0+. The only way that you would be able to receive that broadcast on Android 8.0+ is if you have a foreground service running all the time, and that has costs.
Also:
I am not familiar with that specific broadcast and so I am uncertain if it is sent on a DND change (though it certainly seems plausible)
You are not showing a Toast; instead, you are printing a message to stdout, which should get redirected to Logcat on Android
I'm developing an app where sensor data detected on android wear are being sent to mobile through sendbroadcast. There's a switch in the mobile app to start and stop both the sensor detection on android wear and the sensor data sending process.
The problem is, there's a delay from where the data are being sent from android wear until the the data can be received on mobile using broadcastreceiver. So whenever I press the switch to stop the service and then press it again to start the process again, the mobile will continue to receive the leftover data from the previous session before it receive the data from the new session.
Is there any way I can clear all the broadcast data when clicking the stop switch, so when I press the start switch it will only receive the new data from this session?
You can use ordered boradcast :
void sendOrderedBroadcast (Intent intent,
String receiverPermission)
Broadcast the given intent to all interested BroadcastReceivers,
delivering them one at a time to allow more preferred receivers to
consume the broadcast before it is delivered to less preferred
receivers. This call is asynchronous; it returns immediately, and you
will continue executing while the receivers are run.
And then you can abortBroadcat when you want :
void abortBroadcast ()
Sets the flag indicating that this receiver should abort the current
broadcast; only works with broadcasts sent through
Context.sendOrderedBroadcast. This will prevent any other broadcast
receivers from receiving the broadcast. It will still call
onReceive(Context, Intent) of the BroadcastReceiver that the caller of
Context.sendOrderedBroadcast passed in.
This method does not work with non-ordered broadcasts such as those
sent with Context.sendBroadcast
I have a intent service that receives explicit intents from other activities on what to do, and currently returns an implicit intent with the data as extras, like below:
Intent someImplicitIntent= new Intent(APP_PACKAGE_NAME + ACTION_SOME_DATA);
someImplicitIntent.putExtra(APP_PACKAGE_NAME + EXTRA_SOME_DATA, "HELLO");
sendBroadcast(someImplicitIntent);
Software design wise, I have multiple activities within the app, that when running, would use the identical data for doing different things. So, currently I have setup broadcast receivers in each activity to listen for "APP_PACKAGE_NAME + EXTRA_SOME_DATA" and then act accordingly, like interrupting something. The broadcast receivers are tied to the their respective activities' life-cycles
This setup is fine for testing functionality. But the problem is, I understand that implicit intents in Android broadcasts the intents system wide, so any app which a registered broadcast receiver listening to the action would received the intent and its extra. Implicit intents are great for its intended purposes, but for me the convenience of minimal code and sending just one and multiple recipients are carrying obvious security risks.
I'm thinking of making explicit intents, but my current & limited understanding is I would have to create an explicit intent for each activity in order to keep the data private within the scope of the application, like below:
Intent someExplicitIntent= new Intent(this, activityOne.class);
someExplicitIntent.setAction(APP_PACKAGE_NAME + ACTION_SOME_DATA)
someExplicitIntent.putExtra(APP_PACKAGE_NAME + EXTRA_SOME_DATA, "Hi");
sendBroadcast(someExplicitIntent);
Intent someExplicitIntent2= new Intent(this, activityTwo.class);
someExplicitIntent2.setAction(APP_PACKAGE_NAME + ACTION_SOME_DATA)
someExplicitIntent2.putExtra(APP_PACKAGE_NAME + EXTRA_SOME_DATA, "Hi");
sendBroadcast(someExplicitIntent2); ..... so on, for the same data to N activities
The snippet above look needlessly redundant, and will be wasting resources sending intents that won't be picked up. How can I securely send one intent extra to multiple receivers within an application?
You can make use of LocalBroadcastManager to send broadcasts which can be recieved only by the components of your application, thereby making it secure.
So, you can do as follows :
Intent someImplicitIntent= new Intent(APP_PACKAGE_NAME + ACTION_SOME_DATA);
someImplicitIntent.putExtra(APP_PACKAGE_NAME + EXTRA_SOME_DATA, "HELLO");
LocalBroadcastManager.getInstance(this).sendBroadcast(someImplicitIntent);
Thereafter, just register your broadcast receiver in those components in which you want the intent extra/s.
In this way, you will be able to send intent extra/s through local broadcasts.
For more information about LocalBroadcastManager, you can check out the following link :
https://developer.android.com/reference/android/support/v4/content/LocalBroadcastManager.html
I have an app, where some calls can be made when you press a button.
I call a number with:
Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse("tel:"+o.getTel()));
startActivity(callIntent);
I have a broadcast receiver that detects end of call.
But this broadcast receiver also receives calls started from other apps (e.g. dialer app).
How can i differentiate calls started from other apps from calls started in mine?
Tnx
Before you call sendBroadcast(intent), add an extra to the intent,
e.g i.putExtra("sender", "my identifier")
Then in the onReceive of the receiver
String encodedType = intent.getStringExtra("sender");
Then you can test for this string.