I am using AudioManager and RingtoneManager to mute the background music and start ringing as soon as i receive a call. Which works quite good without issue. But once i take a call. As in below code , i am trying to end ringtone and start the call. But Some how it play muted music streaming long with voice call. How can i differentiate between music streaming and voice call and handle them.
Uri defaultRingtoneUri = RingtoneManager.getActualDefaultRingtoneUri(getApplicationContext(), RingtoneManager.TYPE_RINGTONE);
RingtoneManager.setActualDefaultRingtoneUri(getApplicationContext(), RingtoneManager.TYPE_RINGTONE, defaultRingtoneUri);
AudioManager am = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
am.setStreamVolume(AudioManager.STREAM_MUSIC, am.getStreamMaxVolume(AudioManager.STREAM_MUSIC), 0);
r = RingtoneManager.getRingtone(getApplicationContext(), defaultRingtoneUri);
am.setMode(AudioManager.MODE_IN_CALL);
r.stop();
Related
I am using blescan with scanfilters to detect beacons it's working very fine in foreground and background up to oreo version but when it comes to android pie it's not able to send pending broadcast in background.
ScanSettings settings = (new ScanSettings.Builder().setScanMode(ScanSettings.SCAN_MODE_LOW_POWER)).build();
final List<ScanFilter> scanFilters = new ArrayList<>();
scanFilters.add(getScanFilter());
BluetoothAdapter bluetoothAdapter;
final BluetoothManager bluetoothManager =
(BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
bluetoothAdapter = bluetoothManager.getAdapter();
Intent intent = new Intent(this.getApplicationContext(), MyBroadcastReceiver.class);
intent.putExtra("o-scan", true);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this.getApplicationContext(), 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
bluetoothAdapter.getBluetoothLeScanner().startScan(scanFilters, settings, pendingIntent);
public class MyBroadcastReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
int bleCallbackType = intent.getIntExtra(BluetoothLeScanner.EXTRA_CALLBACK_TYPE, -1);
if (bleCallbackType != -1) {
Log.d(TAG, "Passive background scan callback type: "+bleCallbackType);
ArrayList<ScanResult> scanResults = intent.getParcelableArrayListExtra(
BluetoothLeScanner.EXTRA_LIST_SCAN_RESULT);
// Do something with your ScanResult list here.
// These contain the data of your matching BLE advertising packets
}
}
}
Android 9 introduces several behavior changes, such as limiting background apps' access to device sensors and Wi-Fi scans.
These changes affect all apps running on Android 9, regardless of target SDK version.
Sensors that use the continuous reporting mode, such as accelerometers and gyroscopes, don't receive events.
Android 9 Limited access to sensors in background:
Android 9 limits the ability for background apps to access user input and sensor data. If your app is running in the background on a device running Android 9, the system applies the following restrictions to your app:
Sensors that use the continuous reporting mode, such as accelerometers and gyroscopes, don't receive events.
Sensors that use the on-change or one-shot reporting modes don't receive events.
Solution:
If your app needs to detect sensor events on devices running Android 9 while the app is in the background, use a foreground service.
I an example test Android app using Oreo (API 26) and the the code above (slightly modified) to detect beacons. I am using the Pixel 3 XL (with Pie).
I think that the hard part about this is to know for sure if the code in onRecieve() in MyBroadcastReceiver is actually being run upon detection of a beacon when the device is running on battery only (disconnected from Android-studio and Logcat (USB)).
Using Volley (com.android.volley) to submit a HTTP request to a local http server, I was able to demonstrate that it works as documented - ie. I am able to receive the http request when beacon(s) are detected. However, Volley only sends these these requests when Android is awake or when it periodically wakes up and connects to the network - which in my simple tests was about every 15 minutes (plus some variation), but I did get all the beacon ScanResults on my HTTP server, just in delayed up to 15 minutes. I was even able to remove the app from the list of running apps (you know; swiping up to remove the app) and still see that the onRecieve() in MyBroadcastReceiver was receiving BLE ScanResults.
How do you know that the onRecieve() in MyBroadcastReceiver is being killed? I am very interested to know how you know this.
I have the following code which plays some audio after some time in a service. The issue is, after exiting the app, the media player continues to run; I want to stop the audio from playing after the app has has been exited or closed.
Here is my audio:
By the way, ten and three were declared at the top:
MediaPlayer ten;
MediaPlayer three;
I would really appreciate any feedback (positive or negative)! Thank you so much for all of your help, let me know if you need any more code.
Option1 : You can use a foreground service so that your service runs only when the app is running.
See more about foreground services here.
http://developer.android.com/guide/components/services.html#Foreground
Option2 : Stopping your service from onDestroy method in Activity. Using this code
#Override
protected void onDestroy() {
super.onDestroy();
Intent in = new Intent(YourActivity.this, MyService.class);
stopService(in);
}
Recommendation :
Also you should always try to use only one MediaPlayer object and do something like this :
stopPlaying(); // To stop whatever is currently playing
mp2 = MediaPlayer.create(YourActivity.this, R.raw.second);
mp2.start();
In your case I would probably go with Option2.
All the Best :)
I want to disable ALL sound and vibration from the android device. As per the answers to similar questions, I'm currently using the following code to mute all the audio streams, set the ringer mode to silent, and to fake a voice call scenario:
AudioManager amanager=(AudioManager)getSystemService(Context.AUDIO_SERVICE);
//DOESNT DISABLE ALARM CLOCK
amanager.setStreamMute(AudioManager.STREAM_NOTIFICATION, true);
amanager.setStreamMute(AudioManager.STREAM_ALARM, true);
amanager.setStreamMute(AudioManager.STREAM_RING, true);
amanager.setStreamMute(AudioManager.STREAM_SYSTEM, true);
amanager.setStreamMute(AudioManager.STREAM_DTMF, true);
amanager.setStreamMute(AudioManager.STREAM_MUSIC, true);
amanager.setStreamMute(AudioManager.STREAM_VOICE_CALL, true);
//disables vibrate and sound of ringer
amanager.setRingerMode(AudioManager.RINGER_MODE_SILENT);
//fakes voice call...changes alarm to single tone+vibrate
amanager.setMode(AudioManager.MODE_IN_CALL);
amanager.setStreamSolo(AudioManager.STREAM_VOICE_CALL, true);
This works for disabling music and incoming calls, but, as noted in the comments, the android's built-in alarm clock app is still able to produce sound and vibration.
Does anyone know how to truly disable ALL sound and vibrations? Or venture a guess as to why the alarm clock app seems to by bypassing the audio streams?
setStreamMute did not work for me to mute the alarm, but using setStreamVolume for STREAM_ALARM setting volume to zero did. It is necessary to restore the alarm volume afterwards.
For example like this:
AudioManager manager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
manager.setStreamVolume(AudioManager.STREAM_ALARM, 0, AudioManager.FLAG_REMOVE_SOUND_AND_VIBRATE);
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 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...
}