Doing an activity when mobile is silent - java

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;
}

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
}

How to set default mode headset in android

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;
}
}
}
}

How to set time for silent mode Android

I have an app of school hours and put the option to mute the device during the time stipulated by the user (beginning and end). However there is only the option to enable and disable this feature. How can I do this then?
Use AlarmManager to schedule times where you want mute or turn sound on in your phone, start here
and AudioManager to turn it on or off:
AudioManager am;
am= (AudioManager) getBaseContext().getSystemService(Context.AUDIO_SERVICE);
//For Normal mode
am.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
//For Silent mode
am.setRingerMode(AudioManager.RINGER_MODE_SILENT);
//For Vibrate mode
am.setRingerMode(AudioManager.RINGER_MODE_VIBRATE);

Checking android emulator is in Silent Ringer Mode or not

I am developing an android application for which i want to know whether the android phone is silent or not. I need to know how can I check this thing on android emulator by code.
You can use AudioManager class for your requirement,
AudioManager am = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
if (am.getRingerMode() == AudioManager.RINGER_MODE_SILENT)
{
// silent mode
}

Categories

Resources