I'm trying to build a broadcast that every time audio output route is changed it would route it to a device of choice instead (both in calls and when playing media/music).
for example: when getting a call with plugged/Bluetooth headset and then the user decides to click on the speaker icon on the phone, I need the app to route it back to the headset.
I succeeded to change the audio output on a WIRED headset with a click of a button with following code:
mAudioManager.setMode(AudioManager.MODE_IN_CALL);
mAudioManager.setSpeakerphoneOn(false);
and on a Bluetooth headset with the following (not in calls, works on media/music only):
mAudioManager.setMode(AudioManager.MODE_IN_COMMUNICATION);
mAudioManager.startBluetoothSco();
mAudioManager.setBluetoothScoOn(true);
mAudioManager.setSpeakerphoneOn(false);
I have two problems:
When I click on the speaker icon in the call screen to stop using my Bluetooth headset and then click the button on my app to redirct the route to my Bluetooth headset with the above code it doesn't work :(
Didn't manage to find a way to broadcast audio output change in order for the above code to work automatically without a need to press my app's button. only came across the following code that only suppose to work on a Bluetooth headset and not plugged headset and it's don't even work for me on the Bluetooth headset.
private final BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(BluetoothHeadset.ACTION_AUDIO_STATE_CHANGED)) {
int state = intent.getIntExtra(BluetoothHeadset.EXTRA_STATE, -1);
switch (state) {
case BluetoothHeadset.STATE_AUDIO_CONNECTED:
Toast.makeText(context, "Bluetooth Connected", Toast.LENGTH_SHORT).show();
case BluetoothHeadset.STATE_AUDIO_DISCONNECTED:
Toast.makeText(context, "Bluetooth Disconnected", Toast.LENGTH_SHORT).show();
mAudioManager.setMode(AudioManager.MODE_IN_CALL);
mAudioManager.startBluetoothSco();
mAudioManager.setBluetoothScoOn(true);
mAudioManager.setSpeakerphoneOn(false);
break;
}
}
}
};
Thanks so much in advance for the help!
any answer would help.
Related
I have to create an app, which detects user inactivity, and then start activity which displays some videos with WebView, and then when displaying with WebView is finished, it has to play videos from SDCard. I've already handled part with WebView and SDCard (with JavaScriptInterface etc.)
This application has to work with API 19 all the way to the newest one.
The question is - Is there a possibility to detect if user is inactive and start my application, or keep the app running in background, and then start activity in the foreground after the user becomes inactive for certain time?
I'm not trying to play ads, when user is not looking at his screen. Application is for my client, who have stores with all kind of electrical equipments, including smartphones. The goal is to play video presentations with hardware details specific for each smartphone (informations about processor, ram, camera, screen etc.).
In short: I have to make an app which is similar to "Demo Apps" created for example by Samsung (playing some kind of presentations on screen).
So far I've read and tested things like:
1) BroadcastReceiver with combination of ACTION_SCREEN_OFF / ACTION_SCREEN_ON events.
Receiver works properly, I can detect this event and then start activity, but... The screen is already off so i can't see the displayed activity - it's visible running in the foreground after unlocking the phone. Is there a way to unlock the phone when the event is received?
That's my code so far.
EventReceiver Class:
class EventReceiver: BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
StringBuilder().apply {
append("Action: ${intent.action}\n")
append("URI: ${intent.toUri(Intent.URI_INTENT_SCHEME)}\n")
toString().also { log ->
Log.d(TAG, log)
Toast.makeText(context, log, Toast.LENGTH_LONG).show()
}
}
if (intent.action == Intent.ACTION_SCREEN_OFF) {
val i = Intent(context, MainActivity::class.java)
context.startActivity(i)
}
}
}
MainActivity Class:
val br : BroadcastReceiver = EventReceiver()
val filter = IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION).apply {
addAction(Intent.ACTION_SCREEN_OFF)
addAction(Intent.ACTION_SCREEN_ON)
addAction(Intent.ACTION_BOOT_COMPLETED)
}
2) Foreground Services - I read that this is a great way to make some asyc stuff in the background and show notifications to user. Is there a way to start the activity with it?
3) Job Scheduler
4) Daydream / Dream Service - it actually works great with almost every API and manufacturer, but.. there's no way to set the app as Screen Saver on Huawei/Honor smartphones, at least from phone settings, I've read that this is possible with ADB etc. but this is not an option that I can use here.
It seems that none of these fullfill my expectations.
I have an Android app with a working push notification set up. It works currently like this:
User gets a push notification
When the user taps on the push notification, he is taken to a screen which has the list of all the notifications. It is basically a list view which has all the notifications.
Now, want to implement the following:
a. When a new notification comes to the user, it has to be displayed on the list view, irrespective of whether a user taps on it.
b. I have to indicate the number of notifications that user has received on the app icon. For instance, if you get a message in whats app, it displays the number of messages on the app icon.
Could anybody let me know what is the best possible way to implement these functionality? If anybody could point me to tutorials/references, it would be very helpful.
Thanks!
a. Launch an activity from the receiver(GCM/C2DM receiver) when the push notification arrives
// Your C2DM receiver (for GCM check the Android Documentation)
public void onReceive(final Context context, final Intent intent)
{
if (intent.getAction().equals(com.google.android.c2dm.intent.RECEIVE))
{
//start your list view activity of notifications
Intent i = new Intent();
i.setClassName("com.test", "com.test.NotificationsActivity");
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
}
}
b. Check this.
I have a ContentObserver registered to android.provider.Settings.System that should observe changes of any audio volume. It is notified as expected when clicking the hardware volume buttons but is not notified when I change the audio volume via AudioManager.setStreamVolume or AudioManager.adjustStreamVolume.
Here's how my ContentObserver looks like:
// this is a Service
this.getApplicationContext().getContentResolver().registerContentObserver(
android.provider.Settings.System.CONTENT_URI,
true, new ContentObserver(new Handler()) {
public void onChange(boolean selfChange) {
Log.d("ContentObserver", "got notified");
}
});
And here my call to AudioManager.adjustStreamVolume:
// this.context is the activities context
this.context.getSystemService(Context.AUDIO_SERVICE).adjustStreamVolume(
AudioManager.STREAM_RING, AudioManager.ADJUST_LOWER,
AudioManager.FLAG_SHOW_UI);
I have read this and that post and the AudioManager and Settings.System documentation and can not find a reason why the Observer is notified when changing the volume with the volume buttons but not when changing it with the AudioManager.
Thanks for the help!
The AudioManager does broadcast an intent
android.media.VOLUME_CHANGED_ACTION`
But this is not part of the official documentation. So this might change in future releases. But you could use this atleast for gingerbread devices.
you can find more about the extras in the intent from here
I am writing an Android app where the user can take pictures and I am using my own camera functionality instead of Androids built in camera software. It all works fine except I want to be able to take a picture when the user presses the hard camera button. I registered a Broadcast receiver, and it works but Android still opens its camera program over my app. Is there a way to block the built in app from receiving the broadcast?
I am posting my code below.
Any help would be greatly appreciated.
Thankyou
//Listen for camera button to be pressed
cameraButtonListener = new BroadcastReceiver(){
#Override
public void onReceive(Context context, Intent intent) {
if(intent.getAction().equals(Intent.ACTION_CAMERA_BUTTON)){
Toast.makeText(getApplicationContext(), "Camera Button Pressed", Toast.LENGTH_SHORT).show();
}
}
};
//register broadcast receiver to listen for camera button
getApplicationContext().registerReceiver(cameraButtonListener,new IntentFilter(Intent.ACTION_CAMERA_BUTTON) );
You can use abortBroadcast() in conjunction with android:priority set to high to "consume" the broadcast. However, this works only if the broadcast is an Ordered broadcast, and I don't know what type is ACTION_CAMERA_BUTTON. More info here.
try this code
if("android.intent.action.ACTION_CAMERA_BUTTON".equals(intent.getAction()))
in register
getApplicationContext().registerReceiver(cameraButtonListener,new IntentFilter(Intent.ACTION_CAMERA_BUTTON) );
I have an app that will speak text messages to the user. I want to make it so that when the user hits a button called "Headphones on" the app will only speak to it when headphones are detected. Is there a command that will allow me to detect if headphones are plugged in or not?
There is a broadcast made when the headphones are plugged in: http://developer.android.com/reference/android/content/Intent.html#ACTION_HEADSET_PLUG
You need to register a BroadcastReceiver to receive this and perform your required actions.
It seems in this case you just want to check if headphone are connected before to start the audio playout, so you should use audioManager.isWiredHeadsetOn() like this:
AudioManager audioManager = (AudioManager) this.getSystemService(Context.AUDIO_SERVICE);
if(audioManager.isWiredHeadsetOn()) {
// Play audio...
}