Activate speakerphone on Android 10 and above - java

So, I'm working on this app where a phone call is made using the TelecomManager and I want to turn on the speakerphone programmatically when an event is triggered in the app.
The app is primarily not made for making phone calls etc, but there's a function that makes phone calls in certain events.
This works fine on android versions below android 10 using the following code:
AudioManager am = (AudioManager) this.getSystemService(Context.AUDIO_SERVICE);
am.setMode(AudioManager.MODE_IN_CALL);
am.setSpeakerphoneOn(true);
am.setStreamVolume(AudioManager.STREAM_VOICE_CALL, am.getStreamMaxVolume(AudioManager.STREAM_VOICE_CALL), 0);
But this code doesn't seem to work on android 10 and above.
One solution is to have the speaker on by default when making the phone call, but I want to be able to toggle the speaker via events in the app.
Does anyone have a solution for this?
Edit:
The application is only making a call to a specific number in case of an emergency and is not a phone app. And I'd like to be able to start the speaker without having to make the app into the "Default Phone App".

Related

How to make a phone call without switching activities to a default calling app in android?

I'm trying to make a phone call from my app in a way it does not need to switch activities, But every guide I find has the following code snippet,
Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse("tel:XXXXXXXXX"));
startActivity(callIntent);
which switches the activity I start the call from to another activity (in this case to an activity in a different app). Is there a way to stop this from happening? I managed to do it with a third party library called "sinch" but I'm wondering if there is a native way to do it or maybe a better library?
Ps- the app I'm building it for myself, basically, I'm building a voice assistant that can make calls via voice commands, hence I can't let it switch activities. I have no intention of publishing it on the app store and I have no difficulty giving dangerous permissions :) My plan is to run it on a separate piece of hardware in the future.
This link can help you, but as Xavier Rubio Jansana wrote previously, Google hardly accepts applications that do not use intents to make phone calls :
https://google-developer-training.github.io/android-developer-phone-sms-course/Lesson%201/1_c_phone_calls.html
Google wants any programmer to use an intent to make the user view the default phone application handle the phone call.
If your app does not need to be available on Google Play Store then it would be ok for you to make the phone call directly.
To elaborate on what I was talking about earlier, it is talked about in this stack overflow question (perhaps upvote their answers if they are helpful). How to make a phone call using intent in Android?.
From memory, ACTION_DIAL will bring up the dialler and pre-populate the number.. it requires no special permission because it requires the user to actually place the call.
On the other hand, ACTION_CALL will actually initiate the call and requires special permissions.
If returning focus (bringing your app back to the foreground) is a concern, you could try using a scheduled intent to (re) launch your activity (maybe using alarm manager or similar) some time after invoking the dialler. You can retain state with a little care around launch modes in the intent and/or a special "do nothing" activity published in your app manifest which does nothing but re-activate your app.

Controlling media on one android phone from another

I'm building a custom device. It runs android(rooted/configurable).
What I want to do is this: when my device is paired to your phone, it can accept/reject calls, control music(Volume +/-, next song, play/pause etc).
I don't want the audio. The audio plays from your phone itself. I just want to control the calls and the music. I have been reading up a lot on Bluetooth and profiles. And I'm not sure if there is a right one out there for me. Also it would be more helpful, if i can do all these functions without an app. That is i just pair the devices and they work. Hoping someone can help me whit this.

Permanent services on android oreo

Android 8's battery consumption improvements are nice to the user but I am a bit afraid if my service will work as expected.
First of all: Thank you for any suggestions but I cannot just schedule my service. I want to make a OK Google-like keyword listener running in the background all the time. It will be based on the open source pocketsphinx-android library. I know that this will consume much battery power and I will inform the user about this.
Can we create a permanent background service on android 8+ ? I need to target android 8 in gradle because I was expecting some bugs with older targets. I also don't want to annoy a user with a foreground service which permanently shows a notification in the status bar.
[https://developer.android.com/about/versions/oreo/background.html] - Is there really no way of making permanent background services for my use-case (but preferably for all use-cases) possible?
Unfortunately, it's not possible to use a background service and don't show a foreground notification on Android 8.0 and higher.
The only one way that it might work is if you stick your app to Google APIs such as Voice Actions API.
As far as I know there is no a good work around and most apps like WhatsApp are still targetting Android API 24.
For what is worth, I am sharing me experience on that:
It's partially possible to use a background service and not showing a foreground notification on Android 8.0 just made some experiments and ended up with this:
Just create a notification channel with IMPORTANCE_NONE, and the notification will still exist, but will not be displayed in status bar.
Code excerpt:
NotificationChannel channelService = new NotificationChannel(
YOUR_SERVICE_CHANNEL_ID,
YOUR_CHANNEL_HUMAN_READABLE_NAME,
NotificationManager.IMPORTANCE_NONE);
channelService.setSound(null, null); // let's be quiet : no sound
channelService.setLockscreenVisibility(Notification.VISIBILITY_SECRET); // not on lock screen
// Register the channel with the system; you can't change the importance
// or other notification behaviors after this
// changes needs to uninstall app or clean app data, or rename channel id!
notificationManager.createNotificationChannel(channelService));
This way, the notification will not be displayed when your app is running in foreground.
Not a perfect solution:
When your app goes in background (i.e., you open another app), the "Android system" app displays a notification about "(your) app being running in the background".
So, this is not great, but, from my point of view, it's a bit better than before.

Programmatically mute an Android wearable

I programmatically want to mute the entire watch. So I wanted to do these things:
1. Mute to on , so go into non vibration mode. Will manufacturers be able to add sound capabilities to watches? Otherwise I would also have to add muting of sound...
2. Screen always on , to off. It's a system setting. Can I change it programmatically?
I already tried for point one to use the MediaManager:
AudioManager audioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
audioManager.setRingerMode(AudioManager.RINGER_MODE_SILENT);
I tried all other ringer modes also just to be sure and nothing seems to work and change anything in the emulator and on my real watch but nothing is doing the job...
A pebble watch has night or silent mode support... So I won't be that hard to do it on a wearable I thought.. Any ideas on this one?
Wear 5.0.1 has some ability! the SDK sample Watchface has a way to detect it. Look for WatchFaceService.INTERRUPTION_FILTER_NONE - RoundSparrow hilltx
from this question here: Android Wear detect "Mute"
Maybe with requestInterruptionFilter where fitler mode are:
INTERRUPTION_FILTER_ALL
NTERRUPTION_FILTER_NONE
INTERRUPTION_FILTER_PRIORITY

How to turn off Android phone programmatically?

I want to make a lock screen application. When the phone is in the lock mode, it will turn off the phone while the USB is plugged for prevent other users from accessing the phone data. Does anyone know how to turn off Android devices programmatically so other people can't access the data while a device on the lock mode?
here's the code to lock the home button on the lock screen activity:
#Override
public void onAttachedToWindow()
{
this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);
super.onAttachedToWindow();
}
It is impossible with the public Android SDK.
Yes it is possible,
Using making system application(Using firmware) not download application.
You can get the code for power off android device from AOSP.
This is what can provide you an insight of the OS you are working on , and not about developing an app that would run on any device, as you have not made OS's for them.
Just pull a branch of android , customize it according to your wish and in your mainfest remove the filters, permissions that don't allow you to do that, or add your owns. Now , you have to play with the battery if I talk of more depth.
But if you just want to trigger the click on power off then its far
simpler then.

Categories

Resources