How to resolve setWifiEnabled(bool) deprecated method into Android Q? - java

In my case, I have existing code for enabling wifi. Here I am getting code depreciation at CONNECTIVITY_ACTION and setWifiEnabled(true) in Android Q api level 29. Below code is working fine in old version but depreciation happen only on api 29. How to resolve it with help of version condition.
Enable Wifi Code
private void enableWifi(Context context, String ssid) {
if(webWifiStateListener != null) {
context.unregisterReceiver(webWifiStateListener);
}
webWifiStateListener = new WifiStateListener(ssid);
context.registerReceiver(webWifiStateListener,
new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION)); // CONNECTIVITY_ACTION Deprecated Android Q
webWifiManager.setWifiEnabled(true); // Deprecated Android Q
}

Android Q won't let apps turn Wi-Fi on and off. You can use something like this https://developer.android.com/reference/android/provider/Settings.Panel

Use this in Android Q:
Intent panelIntent = new Intent(Android.Provider.Settings.Panel.ActionWifi);
StartActivityForResult(panelIntent,1);
Don't use:
webWifiManager.setWifiEnabled(true); // Deprecated Android Q

Related

Text to Speech Android Studio

I am trying to do a TTS app for Arabic however whenever I want to initialize my TTS it always fails. I have no idea what I'm supposed to do to fix it. I tried to enter the TTS engine on the emulator but it doesn't show me anything. So I'm unsure whether the issue is from the emulator or my code.
TTS = new TextToSpeech(this, new TextToSpeech.OnInitListener() {
#Override
public void onInit(int status) {
if (status==TextToSpeech.SUCCESS){
int res = TTS.setLanguage(Locale.forLanguageTag("ar-XA"));
if (res == TextToSpeech.LANG_MISSING_DATA || res == TextToSpeech.LANG_NOT_SUPPORTED){
Log.e("TTS","Language not Supporter");
}
else{
speak.setEnabled(true);
}
}
else{
Log.e("TTS","Init Failed");
}
}
});
This is some information that may help you get TTS working on an android emulator.
When I first tried your code I got the LANG_NOT_SUPPORTED error but soon realized my particular emulator instance did not have an TTS engine installed.
To install a TTS engine with an emulator I then had to use an emulator with Google Play (only a small subset of the AVD emulators have them). From the Android Virtual Device Manager you can Create Virtual Device and see (under phone for example) a few with Play Store: Pixel 4, Pixel 3a, Pixel 3, Pixel, Nexus 5X, Nexus 5.
I then used the Nexus 5 with API 25. (I already had it created.)
With the the emulator running I can then see the Play Store app listed. Run the Play Store app as you would on a phone (you'll have to authenticate yourself with your google account just as you would on a phone). Use the search bar and search for "speech services by google" - and install this app if not already installed. This is one TTS engine example which worked for me. Since this is the only TTS engine installed it is also the default. So if you choose to load other engines then you would have select the proper engine. (The engine name is a package name such as com.google.android.tts.)
I then ran your code (slightly modified) as in the following. I added some diagnostics to list the available languages and variants. I also reviewed the available languages and regions here: https://cloud.google.com/speech-to-text/docs/languages and for example just chose "ar-iq" (but "ar" also worked).
Note your "ar-XA" is a language tag used for voices and not for the engine language.
TTS = new TextToSpeech(this, new TextToSpeech.OnInitListener() {
#Override
public void onInit(int status) {
if (status==TextToSpeech.SUCCESS){
Set<Locale> avail = TTS.getAvailableLanguages();
for (Locale locale : avail) {
Log.e(TAG,"local: "+locale);
if (locale.getDisplayVariant() != null) {
Log.e(TAG," var: "+locale.getVariant());
}
}
List<TextToSpeech.EngineInfo> engineInfo = TTS.getEngines();
for (TextToSpeech.EngineInfo info : engineInfo) {
Log.e(TAG,"info: "+info);
}
int res = TTS.setLanguage(Locale.forLanguageTag("ar-iq"));
if (res == TextToSpeech.LANG_MISSING_DATA || res == TextToSpeech.LANG_NOT_SUPPORTED){
Log.e("TTS","Language not Supporter");
}
else{
//speak.setEnabled(true);
TTS.speak("اَلْعَرَبِيَّةُ", TextToSpeech.QUEUE_ADD, null);
}
}
else{
Log.e("TTS","Init Failed");
}
}
});
Once you have the TTS engine installed and your code or above code works - I think you can then use Settings to install your voice data:
Settings | Speech | Text-to-speech output | (choose Speech Services by Google) and select the gear icon | and select your voice data.
You could also use the voice data programmatically.
Note you can install another TTS engine (not the default) by using the constructor:
TextToSpeech(Context context, TextToSpeech.OnInitListener listener, String engine)
The Arabic voices which come with the Google TTS engine have the following language tags - I'm guessing that when the language is set a preferred voice is used which is one of these:
ar-xa-x-arz-local
ar-xa-x-arc-local

How to implement CallScreeningService for Andoid<=28

**What I have done so far : **
I have implement it for Android SDK 29
RoleManager roleManager = (RoleManager) getSystemService(ROLE_SERVICE);
Intent intent = roleManager.createRequestRoleIntent(RoleManager.ROLE_CALL_SCREENING);
startActivityForResult(intent, REQUEST_ID1);
**What I am looking for ? : **
I want to implement Call Screening Service for devices < SDK 29.
Kindly help me if anyone have done it before.
Unfortunately, you can't.
It is impossible to access the RoleManager class on Android versions < 29 because this class was introduced on Android 29. Therefore, you can only use this class on devices running Android 29 and up.
For Android <= 28, to bind the CallScreeningService the app must be set as the default dialer app. On runtime, a dialog can be displayed asking the user to set your app as the default dialer app as follows:
if (getSystemService(TelecomManager.class).getDefaultDialerPackage() != getPackageName()) {
Intent changedDialer= new Intent(TelecomManager.ACTION_CHANGE_DEFAULT_DIALER);
changedDialer.putExtra(TelecomManager.EXTRA_CHANGE_DEFAULT_DIALER_PACKAGE_NAME, getPackageName());
startActivity(ChangeDialer);
}

How to detect check or identify app is running on Android Go edition 8.1 Device programatically

I'd like to disable some features and reduce memory consumption on Android Go Devices. I'd like to have one APK for all Android devices.
How do I detect that my app is running on an Android Go 8.1 Device?
Is it sufficient to check for version 8.1 or will 8.1 version be distributed to normal Android Devices as well?
This works for me, based on preinstalled Apps.
If Assistant Go or Google Go versions are installed, definitely is an Android Go device.
In rare cases that these apps didn't come preinstalled we look for Gmail Go and also Youtube Go preinstalled.
Tested on Huawei Y5 Lite with Android 8.1 (Go).
public static boolean isAndroidGoEdition(Context context) {
final String GMAIL_GO = "com.google.android.gm.lite";
final String YOUTUBE_GO = "com.google.android.apps.youtube.mango";
final String GOOGLE_GO = "com.google.android.apps.searchlite";
final String ASSISTANT_GO = "com.google.android.apps.assistant";
boolean isGmailGoPreInstalled = isPreInstalledApp(context, GMAIL_GO);
boolean isYoutubeGoPreInstalled = isPreInstalledApp(context, YOUTUBE_GO);
boolean isGoogleGoPreInstalled = isPreInstalledApp(context, GOOGLE_GO);
boolean isAssistantGoPreInstalled = isPreInstalledApp(context, ASSISTANT_GO);
if(isGoogleGoPreInstalled | isAssistantGoPreInstalled){
return true;
}
if(isGmailGoPreInstalled && isYoutubeGoPreInstalled){
return true;
}
return false;
}
private static boolean isPreInstalledApp(Context context, String packageName){
try {
PackageManager pacMan = context.getPackageManager();
PackageInfo packageInfo = pacMan.getPackageInfo(packageName, PackageManager.GET_ACTIVITIES);
if(packageInfo != null){
//Check if comes with the image OS
int mask = ApplicationInfo.FLAG_SYSTEM | ApplicationInfo.FLAG_UPDATED_SYSTEM_APP;
return (packageInfo.applicationInfo.flags & mask) != 0;
}
} catch (PackageManager.NameNotFoundException e) {
//The app isn't installed
}
return false;
}
There doesn't seems to be direct api for retrieving whether app is running on GO version.
But you may cover the case by combination of following :
based on device memory and deciding on threshold value for your app:
private ActivityManager.MemoryInfo getAvailableMemory() {
ActivityManager activityManager =
(ActivityManager) this.getSystemService(ACTIVITY_SERVICE);
ActivityManager.MemoryInfo memoryInfo = new
ActivityManager.MemoryInfo();
activityManager.getMemoryInfo(memoryInfo);
return memoryInfo;
}
Further similar steps can be take for particular model/manufacturer :
String deviceName = android.os.Build.MODEL;
String deviceMan = android.os.Build.MANUFACTURER;
Hope it helps.
No, as #chrylis pointed out, checking the version number wont help because there is a full Android version with the same number.
According to the documentation, to check whether the device is running Android Go you could call either
ActivityManager.isLowRamDevice();
or
PackageManager.hasSystemFeature("FEATURE_RAM_LOW");
Both will return true if the device is running Android Go edition, and false if it's running the regular Android.
I would rely on these APIs rather than checking for other Android Go edition system apps (like the android Go version of Gmail) because those apps may have been installed or uninstalled by the user, potentially triggering false positives or false negatives.
In my experience, is ActivityManager.isLowRamDevice() is equivalent to being an Android Go phone.
boolean isAndroidGo() {
return ((ActivityManager)context.getSystemService(Context.ACTIVITY_SERVICE))
.isLowRamDevice();
}
}
Although, the last time I looked in the documentation, I don't think it says anywhere, explicitly, that Android Go and isLowRamDevice() are equivalent.

Geocoder returns false on isPresent()

So I wrote some code for maps in my app, and it works on my phone just fine, sadly it does not work on my emulator which is not comfortable enough for me here is the code:
String searchString = mSearchText.getText().toString();
Geocoder geocoder = new Geocoder(this);
List<Address> list = new ArrayList<>();
try{
int i=0;
while(list.size()==0 && i<10) {
boolean a = geocoder.isPresent();
list = geocoder.getFromLocationName(searchString,1);
i++;
}
}catch(IOException e){
Log.d(TAG, "geoLocate: IOException " + e.getMessage());
}
So when I debug it I see that variable "a" is always false on my emulator.
I use android emulator for visual studio since I have an AMD processor (Ryzen 7 1800x) on which I installed google play store and google play services(map works just fine, just geocoder doesn't), now is there some way to fix it?
As I read on https://developer.android.com/reference/android/location/Geocoder.html
website "The Geocoder query methods will return an empty list if there no backend service in the platform." Can I get the service somehow? Download it on my emulator or something like this?
First, isPresent() is a static method so the call should be
Geocoder.isPresent();
IsPresent method "Returns true if the Geocoder methods getFromLocation and getFromLocationName are implemented" and false otherwise. Some emulators don't have the geocoder service installed. Is the method
geocoder.getFromLocationName
returning what you need or an empty array?
Your snippet works just fine on my Nexus 6P Android 7.0 API 24 emulator.

Android 6 Marshmallow Settings.Global.getInt for wifi always returns wrong value?

Reading the WIFI_ON System setting on an Android 6.0(API 23) Marshmallow device seems to always returns 0. This code works well on 19 ( Kit kat ) and older builds. Are there reasons why it is not working on Android Marshmallow?
//api level < 17
int wifiState = Settings.Secure.getInt(getContentResolver(), Settings.Secure.WIFI_ON, -1);
//api levep 17+
int wifiState = Settings.Global.getInt(getContentResolver(), Settings.Global.WIFI_ON, -1);
int mobileDataState = Settings.Global.getInt(getContentResolver(), "mobile_data", -1);
For mobile data state, it was never documented so this doesn't come as a surprise. Are there any documented changes with regards to the access of system settings?

Categories

Resources