Checking android emulator is in Silent Ringer Mode or not - java

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
}

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

Run Android App Twice To Work, Why?

I'm making an android app that test if certain security features on your phone are enabled. For example, if you have password log in enabled or if your data is encrypted on your phone.
For some reason, the app has to be ran twice to test and see if these security features are enabled on the phone or not, and this is the problem I'm trying to solve. I'd like it to test and see if the security features are enabled when the app is created and the first time the app is run, not the second time it is run.
I test if these features are enabled in the onStart() function in my MainActivity file. I included the functions code below:
#Override
#TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
#SuppressLint("NewApi")
public void onStart()
{
super.onStart();
//determine if phone uses lock pattern
//It returns 1 if pattern lock enabled and 0 if pin/password password enabled
ContentResolver cr = getBaseContext().getContentResolver();
lockPatternEnable = Settings.Secure.getInt(cr, Settings.Secure.LOCK_PATTERN_ENABLED, 0);//Settings.System
//returns 1 if pin/password protected. 0 if not
KeyguardManager keyguardManager = (KeyguardManager) getBaseContext().getSystemService(Context.KEYGUARD_SERVICE);
if( keyguardManager.isKeyguardSecure())
{
//it is pin or password protected
pinPasswordEnable=1;
}
else
{
//it is not pin or password protected
pinPasswordEnable=0;
}//http://stackoverflow.com/questions/6588969/device-password-in-android-is-existing-or-not/18716253#18716253
//determine if adb is enabled. works
adb=Settings.Global.getInt(cr, Settings.Global.ADB_ENABLED, 0);
//determine if bluetooth is enabled.works
bluetooth=Settings.Global.getInt(cr, Settings.Global.BLUETOOTH_ON, 0);
//Settings.System BLUETOOTH_DISCOVERABILITY
//determine if wifi is enabled. works
WifiManager wifi = (WifiManager)getSystemService(Context.WIFI_SERVICE);
if (wifi.isWifiEnabled())
{
//wifi is enabled
wifiInt=1;
}
else
wifiInt=0;
//determine if data is encrypted
getDeviceEncryptionencryption();
//determine if gps enabled
}//end of onStart() function
If any more code needs to be posted to answer this question, just let me know, and thanks for your help. Maybe the issue has something to do with the super.onStart();
Does anyone think that a splash loading screen might help solve the issue?
super.onStart(); is fine. Splash screen will not help.
From your code I do not see how you determine how many times it ran.
You also mention testing - is it manual testing or you use any framework? Maybe your framework has some init method which runs before each run and it makes this extra call for onStart().
Issues is not in this code. Use debugger or logcat and figure out who calls you twice and, as #nasch had asked, what happens at first run.
Still, real question to help you remains - what do you mean "call twice". Is it you manually clicking app icon twice or is it some testing framework calls your app twice. Both cases are clear to solve.

Android use device flash as torch

There is a need to use the built in device camera flash as a torch.
Unfortunately I don't have access to an Android device so I can only use the Android emulator.
How can I validate that my code does work?
I have been trying several times to update my app with different ways to access the camera flash and each time the app was crashed.
My app minimum OS version is 2.2,
At the moment I'm using the following code
private void turnFlashOn() {
camera = Camera.open();
Parameters p = camera.getParameters();
p.setFlashMode(Parameters.FLASH_MODE_TORCH);
camera.setParameters(p);
camera.startPreview();
}
private void turnFlashOff() {
camera = Camera.open();
Parameters p = camera.getParameters();
p.setFlashMode(Parameters.FLASH_MODE_OFF);
camera.setParameters(p);
camera.stopPreview();
}
Haven't tested it yet.
What is your way to use the camera flash?
Thanks!
The first two errors here is that you have called Camera.open() twice and never close it. You need to open it in onResume() and close it in onPause(), and in between operate on the Camera handle you obtained.
You can debug on emulator and mock camera class (with jmockit) , but you will have to be careful - not every camera on real devices supports torch mode ( or have a flash at all) - so you will have to check whether camera object supports this.
Once you got your code working correclty, you will still have to test it on real device
( hint: you can recruit testers on android developers mailing lists if you ask politely and provide signed APK somewhere on the web so people can install it )

Categories

Resources