Intent Action_dial does not function in android 11 - java

I am currently developing an android app and needed a function that starts a phone call so I added this code.
public void dialPhoneNumber(String phoneNumber) {
Intent intent = new Intent(Intent.ACTION_DIAL);
intent.setData(Uri.parse("tel:" + phoneNumber));
if (intent.resolveActivity(getPackageManager()) != null) {
startActivity(intent);
}
}
.. it seems to work perfectly in older android versions but when I test it in android 11 it doesn't function at all I tried action_call and added the permission <uses-permission android:name="android.permission.CALL_PHONE" /> still doesn't work.

Your problem lies in the line of code intent.resolveActivity(getPackageManager()). When you call resolveActivity, you will get a warning like this:
Consider adding a declaration to your manifest when calling this method; see https://g.co/dev/packagevisibility for details
Check the document under PackageManager, you will see this note:
Note: If your app targets Android 11 (API level 30) or higher, the methods in this class each return a filtered list of apps. Learn more about how to manage package visibility.
So what does that mean?
In android 11, Google added package visibility policy. Apps now have tighter control over viewing other apps. Your application will not be able to view or access applications outside of your application.
What do you need to do?
All you need to do is add below line of code to AndroidManifest.xml:
<manifest>
<queries>
<!-- Specific intents you query for -->
<intent>
<action android:name="android.intent.action.DIAL" />
</intent>
</queries>
</manifest>
More information:
Package visibility in Android 11
Package visibility filtering on Android

Related

Android in-app language switcher works with emulator and local device, but not when published in Google Play (signed bundle)

I have an in-app language switcher in a Kotlin app (two buttons, one button for English, one button for Romanian).
It does not work on devices with Android 12 (API level 32) and lower. On the newer ones, it works perfectly.
I'm following the official guide here. I have a class LocaleHelper, that has this method:
fun setLanguage(language: String) {
App.preferences.edit().putString(App.LANGUAGE_SELECTION, language).apply()
val tag = "$language-RO"
val appLocale: LocaleListCompat = LocaleListCompat.forLanguageTags(tag)
AppCompatDelegate.setApplicationLocales(appLocale)
}
which I'm calling when a language button is clicked:
when (item?.itemId) {
R.id.romanian_button -> {
LocaleHelper().setLanguage("ro")
}
R.id.english_button_button -> {
LocaleHelper().setLanguage("en")
}
}
The Romanian language IS available on users devices, because in the MainActivity.kt, I'm already downloading the additional language (Romanian) according to the official Google code sample from github.
Also, in AndroidManifest.xml I've put this, according to the official guide for older devices:
<application>
...
<service
android:name="androidx.appcompat.app.AppLocalesMetadataHolderService"
android:enabled="true"
android:exported="false">
<meta-data
android:name="autoStoreLocales"
android:value="true" />
</service>
...
</application>
The problem is that on devices with Android 12 (API level 32) and lower, this does nothing. When I tap the flags, nothing happens, the language remains English, even if the activity is recreating itself. On newer devices, this code works. What did I do wrong?

Android Email intent not Working on Android 11 java [duplicate]

I've an extension function for opening an intent for my activities:
fun Activity.openIntent(action: String?, type: String?, uri: Uri?) {
Intent()
.apply {
action?.let { this.action = it }
uri?.let { this.data = it }
type?.let { this.type = it }
}
.also { intent ->
packageManager?.let {
if (intent.resolveActivity(it) != null)
startActivity(intent)
else
showToast(R.string.application_not_found)
}
}
}
My targetSdkVersion is 30. It gives me a warning in intent.resolveActivity(it):
Consider adding a queries declaration to your manifest when calling this method.
So What should I do to solve this warning?
The simplest solution is to get rid of resolveActivity(). Replace:
packageManager?.let {
if (intent.resolveActivity(it) != null)
startActivity(intent)
else
showToast(R.string.application_not_found)
}
with:
try {
startActivity(intent)
} catch (ex: ActivityNotFoundException) {
showToast(R.string.application_not_found)
}
This gives you the same result with a bit better performance, and it will get rid of the warning.
Another option would be to add the QUERY_ALL_PACKAGES permission. This may get you banned from the Play Store.
Otherwise, you will need to:
Build a list of every possible openIntent() call that your app may make
Add a <queries> element to your manifest that lists all of those possible Intent structures that you want to be able to use with resolveActivity()
Repeat this process periodically, in case you add new scenarios for openIntent()
So starting Android 11 (i.e, if your app targets Android 11) not all applications will be visible to your application. Some apps are visible by default but in order to access other applications through your application, you will have to declare queries in your manifest else your application will not be able to access them. You can read about that here.
So if your application targets Android 11 and is to access an application that may not be visible by default you will want to add queries for them in the manifest file.
In your case, this warning is not applicable as I believe you are using implicit intents to open other applications. Using implicit intents, other applications can be accessed irrespective of app visibility. If your app target Android 10 or lower you can suppress the warning as all apps are visible by default.
To suppress the lint warning you can either:
Add the suppress annotation, like so:
#SuppressLint("QueryPermissionsNeeded")
fun Activity.openIntent(action: String?, type: String?, uri: Uri?): Activity {
Add the following to your android block in your app module build gradle file:
lintOptions {
ignore "QueryPermissionsNeeded"
}
Replace
if (intent.resolveActivity(it) != null)
with
if (it.resolveActivity(intent, 0) != null)
and the warning will be gone.
From API level 30 package visibility is restricted. So add appropriate query in your AndroidManifest file outside <application> tag.
<queries>
<intent>
<action android:name="android.intent.action.VIEW" />
<data android:scheme="http" />
</intent>
</queries>

How to launch an Activity from another Application in Android 30

I have been using the following code to open my other app from within my app. This code works upto Android 29 devices but this code does not work in Android 30 devices. Can somebody please help me make it work for Android 30 as well. Thanks
case R.id.btAudio:
Intent launchIntent = getPackageManager().getLaunchIntentForPackage(getString("com.android.audioapp"));
Intent uin = new Intent(MainActivity.this, AudioInstall.class);
if (launchIntent != null) {
startActivity(launchIntent);
} else {
startActivity(uin);
}
this.finish();
break;
Android 11 added restrictions regarding the visibility of other apps. Apps that have targetSdk set to >= 30 can't interact with and open other apps without specifying this in the manifest.
To specify that your app interacts with another specific app, you need to add a <queries> element to your manifest file:
<manifest package="com.example.game">
<queries>
<package android:name="com.android.audioapp" />
</queries>
...
</manifest>
The link below contains other examples in case you need to specify a broader range of apps which you want to interact with.
Source: https://developer.android.com/training/basics/intents/package-visibility

How do I launch a specific (explicit) activity of another app

I want to launch a specific activity of another app from my app. For example, on the onCreate of my app, I want to launch the activity named Rolling (not main) activity of com.pas.webcam.pro. I have heard that you must have control of both apps to do this because you must add an intent filter to the manifest of the second app. This is not true though, because activity launcher apps in the Google Play Store can launch the Rolling Activity of IP Webcam Pro.
The Activity Launcher app is open source, so I tried reviewing the source code here. It was too complicated though, so I could not figure out how this app magically launches this activity. There are many other questions like this on Stack Overflow, and I have read every one. I have also tried lots of the code from the answers too, like this:
Intent intent = new Intent(); intent.setComponent(new ComponentName("com.pas.webcam", "com.pas.webcam.RollingActivity")); startActivity(intent);
I have also tried variants of this code from other posts. My app always crashes and I get variants (depending on the code I use) of the following error:
An error occurred
Invalid intent operation. Unable to find explicit activity class {com.pas.webcam.pro/com.pas.webcam.pro.Rolling}; have you declared this activity in your AndroidManifest.xml?
I have put both of the following in my Android Manifest and the same thing happens:
<uses-permission android:name="android.permission.GET_INSTALLED_PACKAGES" />
<activity android:name="com.pas.webcam.pro.RollingActivity"
Thanks in advance for any answers, I really appreciate it, as I have been working on this problem for a while.
Edit: Here is the activity of the app I want to launch: https://i.stack.imgur.com/Fa7Xq.jpg
Edit: David Wasser helped me solve the problem by giving me the code neccessary to solve the problem. It actually works! To anyone who wants to launch a specific activity of another app with code, please use this:
Intent intent = new Intent(); intent.setClassName("com.pas.webcam.pro", "com.pas.webcam.Rolling"); startActivity(intent);
You may replace com.pas.webcam.pro and Rolling with the app and activity of your choice, but this method truly works. Problem Solved!😀
Try this:
Intent intent = new Intent();
intent.setClassName("com.pas.webcam.pro", "com.pas.webcam.Rolling");
startActivity(intent);
Since you refer to the app as "IP webcam pro", I'm assuming the package name is "com.pas.webcam.pro" (found by Internet research).

Unable to start crop activity (using github.com/lvillani/android-cropimage ) Library

I use the Library:
https://github.com/lvillani/android-cropimage
for adding crop activity in my project.I import this library and add in my project now I am writing this code :
String mPackage = "com.android.camera";
String mClass = ".CropImage";
intent.setComponent(new ComponentName(mPackage,mPackage+mClass));
startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
It gives error :
Unable to find explicit activity class {com.android.camera/com.android.camera.CropImage}; have you declared this activity in your AndroidManifest.xml?
I tried to write in AndroidManifest.xml in my project ass well but still the same error.
Step #1: Add an <activity> element to your manifest pointing to com.android.camera.CropImage
Step #2: Use new Intent(this, com.android.camera.CropImage.class) to create your Intent
Step #3: There should be no step #3 AFAIK
Add activity like this in your android manifest file,
...
....
</activity>
<!-- Declare the bundled CropImage Activity -->
<activity android:name="com.android.camera.CropImage"/>
</application>
</manifest>
Documentation indeed helps :) link

Categories

Resources