Broadcast intent for On receiving audio from mic? - java

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?

Related

startActivity() in BroadcastReceiver

I am trying to create an application that calls the sender of an SMS as soon as the smartphone receives an SMS.
This is my code:
public class SmsReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals("android.provider.Telephony.SMS_RECEIVED")){
// ... (Managing SMS)
Intent intent = new Intent(Intent.ACTION_CALL);
intent.setData(Uri.parse("tel:" + sender));
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
}
}
But it dials and calls the sender only when the application is in foreground, while I'd like it to work always. Using the debugger, the execution flows, but it is unable to start the ACTION_CALL activity somehow. Am I missing anything? Thank you very much in advance
Since Android 10, there are limitations on when/how background processes (Service, BroadcastReceiver) can launch activities. This is your problem.
See this guide for more details.
while I'd like it to work always
That is not an option on modern versions of Android. You cannot start an activity from the background, because you do not know what is going on in the foreground at the time. For example, if the user is relying on a navigation app for driving, taking over the foreground could cause the user to crash.
You could raise a high-priority Notification instead.

How to securely send one intent extra to multiple receivers within an Android application

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

How to send an intent to only 1 app

I broadcast an intent like this
Intent intent= new Intent();
intent.setAction("com.my_app_two");
sendBroadcast(intent);
I only want com.my_app_two to get it.
I tried adding an extra to check but there has to be an easier way.
have you looked into using a Pendingintent instead of a broadcast as suggested in this answer as well ?
send broadcast intent to only one application without using explicit intent
The solution was to use setPackage
There I can specify which package should receive the intent.

Sharing a video via intent to messaging apps like Viber

With this code i want to share an MP4 video to messaging apps like WhatsApp, Viber, Line etc. but only WhatsApp can do it successfully, But for Example if i choose Viber, After selecting recipient nothing happens, Screen flashes and nothing happens. how i can fix it?
Intent intent = new Intent(Intent.ACTION_SEND, Uri.parse(String.valueOf(path)));
intent.setDataAndType(Uri.parse(String.valueOf(path)), "video/*");
intent.putExtra(Intent.EXTRA_STREAM, Uri.parse(String.valueOf(path)));
context.startActivity(Intent.createChooser(intent, "Share video using"));

WIFI_P2P_DISCOVERY_CHANGED_ACTION intent action is never sent

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.

Categories

Resources