How to set default mode headset in android - java

I am working on a VoIP app, I get an issue when user unplugs headphone then device automatically switches to loud speaker but I want device to switch to normal speaker. Is there any way I can change it to normal speaker?

Tricky stuff, esp multiple devices, different scenarios.
I have worked on a VoIP app that is how i know this. What you are facing is probably because the audio manager mode and-or stream is being handled manually after requesting focus. What you need to do is to transfer the control to the call stream so while your call is on,
connection and disconnection of headset, transfer to the phone earpiece etc is handled automatically by the o.s.
You could probably be doing all of this or a combination...
Use AudioManager {.setMode(int mode)} - use MODE_IN_COMMUNICATION, recommended for VoIP or MODE_IN_CALL
Also, check STREAM_VOICE_CALL
Are you usinng
- isSpeakerphoneOn() and setSpeakerphoneOn(boolean)
- isBluetoothScoOn(), setBluetoothScoOn(boolean) ?
Will need to play around with all of those for device or headset model specific issues.
Useful stuff:
Even if a SCO connection is established, the following restrictions
apply on audio output streams so that they can be routed to SCO
headset:
the stream type must be STREAM_VOICE_CALL
the format must be mono
the sampling must be 16kHz or 8kHz

To turn on earpiece programmatically try this:
audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
private void useEarpiece() {
audioManager.setMode(AudioManager.MODE_IN_CALL);
audioManager.setSpeakerphoneOn(false);
}
Check your permission:
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
To catch event:
private class HeadSetReceiver extends BroadcastReceiver {
#Override public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Intent.ACTION_HEADSET_PLUG)) {
int state = intent.getIntExtra("state", -1);
switch (state) {
case 0:
// Headset unplugged
break;
case 1:
// Headset plugged in
break;
}
}
}
}

Related

Foreground service killed in Doze mode for some devices like oppo,vivo,MI, etc

I am using a foreground service to track the live location of the user. it is working fine in stock android devices, but in brands like oppo, vivo, Mi etc, the app is killed when the device comes into doze mode. I also tried to use FCM notifications still of no use. I am just wondering has Uber or Ola been able to crack this, bcuz i have seen most of the drivers have been using these brands. How are the able to keep their app alive in doze mode?
you need enable auto start permission for apps in oppo , vivo and mi
try below code worked for me
private void keepServicesInChineseDevices() {
Intent intent = new Intent();
String manufacturer = android.os.Build.MANUFACTURER;
switch (manufacturer) {
case "xiaomi":
intent.setComponent(new ComponentName("com.miui.securitycenter",
"com.miui.permcenter.autostart.AutoStartManagementActivity"));
break;
case "oppo":
intent.setComponent(new ComponentName("com.coloros.safecenter",
"com.coloros.safecenter.permission.startup.StartupAppListActivity"));
break;
case "vivo":
intent.setComponent(new ComponentName("com.vivo.permissionmanager",
"com.vivo.permissionmanager.activity.BgStartUpManagerActivity"));
break;
}
List<ResolveInfo> arrayList = getPackageManager().queryIntentActivities(intent,
PackageManager.MATCH_DEFAULT_ONLY);
if (arrayList.size() > 0) {
AppDataHolder.getSession(MyApplication.getAppContext()).setPermissionForChineseDevices(true);
startActivity(intent);
}
}
this article is also helpful
Thank you so much guys, for your responses. I would like to tell you that I wrote an email to google support and highlighted the issue to google support, in reply they mentioned below reply:
"Thanks for reaching out.
It seems that you can’t receive a push notification on some Android devices. Upon checking the affected device, it is affected by a device specific (known) issue, and it's caused by OEM features for battery optimization. When the app is swiped away, in some of OEMs which has implemented such a feature, the application is treated similar to "force-stopped" mechanism and services registered with the app that's swiped, is stopped.
For now, I strongly recommend contacting the support team of those affected OEMs to help get it resolved from their end.
You can read more about this issue and a possible way of solving it in this blog post(https://www.freecodecamp.org/news/why-your-push-notifications-never-see-the-light-of-day-3fa297520793/).

How to make phone vibrate when profile is on vibration mode or never ring when profile is on silent mode and will ring on ringing mode

As I am developing calling app in Android, I have achieved to play native ringing on call incoming by:
Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALL);
ringtone = RingtoneManager.getRingtone(getApplicationContext(), notification);
ringtone.play();
so what I want is to achieve native whole behavior as when we receive a phone call or any other application's call.
(As I am beginner on this site and on android development so please guide me on my mistakes and thank you in advance)
try this
RINGER_MODE_NORMAL This mode is used to set ringing mode in device.
RINGER_MODE_SILENT This mode is used to set silent mode in device.
RINGER_MODE_VIBRATE This mode is used to set vibration mode in device.
try this code...
create an instance of AudioManager class by calling the getSystemService() method with an argument of Context.AUDIO_SERVICE.Once we create an instance of AudioManager class, we can use setRingerMode() method to set the volume or ringing modes of our device based on our requirements.
CODE:
AudioManager aManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
aManager.setRingerMode(AudioManager.RINGER_MODE_SILENT);
by using AudioManager class getRingerMode() method we can easily get the device current ringer mode.
AudioManager aManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
int currentMode = aManager.getRingerMode();
if(currentMode == AudioManager.RINGER_MODE_NORMAL){
// Do your code
}

Android: BroadcastReceiver: UNINSTALL_PACKAGE intent

I need to detect when my app is being uninstalled. For that, I've seen logcat sends out UNINSTALL_PACKAGE intent, and I simply added it to existing Broadcast Receiver I have. But it just doesn't catch it, while other intent I'm listening to, TIME_TICK, works perfectly. Why?
Code currently used:
private IntentFilter intentFilter;
static {
intentFilter = new IntentFilter();
intentFilter.addAction(Intent.ACTION_TIME_TICK);
intentFilter.addAction(Intent.ACTION_UNINSTALL_PACKAGE);
}
private final BroadcastReceiver receiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
final String action = intent.getAction();
if (action.equals(Intent.ACTION_UNINSTALL_PACKAGE) {
Log.e("App", "Uninstalling");
} else if (action.equals(Intent.ACTION_TIME_TICK){
// this works
}
}
};
I need to detect when my app is being uninstalled
There is no supported way to do this, for obvious security reasons.
But it just doesn't catch it
ACTION_UNINSTALL_PACKAGE is an activity action. Nothing on an Android device should be sending it as a broadcast. Hence, you cannot listen to it via a BroadcastReceiver.
I 've seen logcat sends out UNINSTALL_PACKAGE intent, and I simply added it to existing Broadcast Reciever I have. But it just doesn't catch it, while other intent I'm listening to, TIME_TICK, works perfectly. Why?
Whenever your application is being uninstalled, Android OS kills all the components associated with that application as well free memory & resources. Uninstall broadcast receiver intent was to listen uninstall event of others app not for your app.
If you want to capture uninstall event of your app then there is no concept google provides, however you can do by observing data change in your file system where you need to keep monitoring changes in file system.
This solution is not going to work for all phones & different versions of Android.
Can't you just listen for android.intent.action.PACKAGE_REMOVED with your IntentFilter when you uninstall using ACTION_DELETE or ACTION_UNINSTALL_PACKAGE?

How to detect if bluetooth device out of range, or we lost it?

For bluetooth device found we receive Brodcast from Android with action :
if (BluetoothDevice.ACTION_FOUND.equals(action)) {
// New bluetooth device found
}
When we set device to discover able then this happens.
My question is what are the intents that are fired when device discoverable is turned off or bluetooth of other device turned off.
In my list view I am showing devices that are "FOUND" I am able to do this using above code. But I want to remove entry of the device which is no longer in range, no longer discoverable or has turned off the bluetooth are there any specific intent that Android platform fires ?
I have looked through BluetoothDevice, BluetoothAdapter reference APIs. But did n't found any useful broadcast action.
The intent you are searching for is BluetoothAdapter.ACTION_CONNECTION_STATE_CHANGED
Intent used to broadcast the change in connection state of the local Bluetooth adapter to a profile of the remote device.
This intent will have 3 extras:
EXTRA_CONNECTION_STATE - The current connection state.
EXTRA_PREVIOUS_CONNECTION_STATE - The previous connection state.
EXTRA_DEVICE - The remote device.
EXTRA_CONNECTION_STATE or EXTRA_PREVIOUS_CONNECTION_STATE can be:
STATE_DISCONNECTED
STATE_CONNECTING
STATE_CONNECTED
STATE_DISCONNECTING.

Doing an activity when mobile is silent

I am trying to make an android app which sends a message automatically when you get a miss call but only when the mobile is silent . I know how to send message when there is a miss call but i don't know to check whether the state of mobile is Silent or not .
Is it even possible to check the state of the mobile and if so how ??
Thank you in advance .
You can do that with AudioManager like this:
AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
switch(audioManager.getRingerMode()){
case AudioManager.RINGER_MODE_NORMAL:
// Phone is loud
break;
case AudioManager.RINGER_MODE_SILENT:
// Phone is silent
break;
case AudioManager.RINGER_MODE_VIBRATE:
// Phone is vibrating
break;
}

Categories

Resources