What is the default browser, find out programmatically on Android - android

I want to find out, programmatically, the package name of the default browser.
(And knowing the package name, I can, for example, place its icon on a button, or launch without a launcher.)
There is resolveActivity() in PackageManager, but the Google docs say: If multiple matching activities are found and there is no default set, returns a ResolveInfo containing something else, such as the activity resolver.

I hope someone can write something more elegant. But this stuff works:
public static ComponentName getDefaultBrowserComponent(Context context) {
Intent i = new Intent()
.setAction(Intent.ACTION_VIEW)
.setData(new Uri.Builder()
.scheme("http")
.authority("x.y.z")
.appendQueryParameter("q", "x")
.build()
);
PackageManager pm = context.getPackageManager();
ResolveInfo default_ri = pm.resolveActivity(i, 0); // may be a chooser
ResolveInfo browser_ri = null;
List<ResolveInfo> rList = pm.queryIntentActivities(i, 0);
for (ResolveInfo ri : rList) {
if (ri.activityInfo.packageName.equals(default_ri.activityInfo.packageName)
&& ri.activityInfo.name.equals(default_ri.activityInfo.name)
) {
return ri2cn(default_ri);
} else if ("com.android.browser".equals(ri.activityInfo.packageName)) {
browser_ri = ri;
}
}
if (browser_ri != null) {
return ri2cn(browser_ri);
} else if (rList.size() > 0) {
return ri2cn(rList.get(0));
} else if (default_ri == null) {
return null;
} else {
return ri2cn(default_ri);
}
}
private static ComponentName ri2cn(ResolveInfo ri) {
return new ComponentName(ri.activityInfo.packageName, ri.activityInfo.name);
}

Related

Why is resolveList for VoiceInteractionService.SERVICE_INTERFACE on Android always empty

Im investigating the use of slices in my android apps shortcuts
the google docs have this snippet of code to grant permission to slices
public class MyApplication extends Application {
private static final String SLICE_AUTHORITY = "...";
#Override
public void onCreate() {
super.onCreate();
grantSlicePermissions();
}
private void grantSlicePermissions() {
Context context = getApplicationContext();
Uri sliceProviderUri =
new Uri.Builder()
.scheme(ContentResolver.SCHEME_CONTENT)
.authority(SLICE_AUTHORITY)
.build();
String assistantPackage = getAssistantPackage(context);
if (assistantPackage == null) {
return;
}
SliceManager.getInstance(context)
.grantSlicePermission(assistantPackage, sliceProviderUri);
}
private String getAssistantPackage(Context context) {
PackageManager packageManager = context.getPackageManager();
List<ResolveInfo> resolveInfoList = packageManager.queryIntentServices(
new Intent(VoiceInteractionService.SERVICE_INTERFACE), 0);
if (resolveInfoList.isEmpty()) {
return null;
}
return resolveInfoList.get(0).serviceInfo.packageName;
}
}
when i execute the above code within my application the following line always returns an empty list
List<ResolveInfo> resolveInfoList = packageManager.queryIntentServices(
new Intent(VoiceInteractionService.SERVICE_INTERFACE), 0);
what is my mistake?
if this list is empty no permissions are granted

How can I make other apps play/pause music?

I'm making a media controller app similar to this example made by google. https://github.com/googlesamples/android-media-controller
However, I want to make a function that can resume playing music or pause given package name. I managed to return a list of package names.
PS. I'm using react native that's why I need a fucntion that I can call from the react side.
public void getMediaApps (Callback callback) {
// = getPackageManager();
ArrayList<MediaAppDetails> mediaApps = new ArrayList<MediaAppDetails>();
Intent mediaBrowserIntent = new Intent(MediaBrowserServiceCompat.SERVICE_INTERFACE);
List<ResolveInfo> services = packageManager.queryIntentServices(
mediaBrowserIntent,
PackageManager.GET_RESOLVED_FILTER
);
if (services != null && !services.isEmpty()) {
for (ResolveInfo info : services) {
mediaApps.add(
new MediaAppDetails(info.serviceInfo, packageManager, resources)
);
}
}
WritableArray waPackagenames = Arguments.createArray();
// ArrayList<String> packagenames = ArrayList<String>()
if(mediaApps != null && !mediaApps.isEmpty()){
for(MediaAppDetails mediaApp : mediaApps){
waPackagenames.pushString(mediaApp.packageName);
}
}
callback.invoke(waPackagenames);
}
I've been trying to do this for 3 days now, but no luck.
Probably won't make such of a difference but this is where I got so far with the play function.
#ReactMethod
public void play (String packageName) {
PackageManager pm = this.packageManager;
Resources res = this.resources;
ServiceInfo serviceInfo = MediaAppDetails.findServiceInfo(packageName, pm);
mMediaAppDetails = new MediaAppDetails(serviceInfo, pm, res);
MediaSessionCompat.Token token = mMediaAppDetails.sessionToken;
if (token == null) {
if (mMediaAppDetails.componentName != null) {
mBrowser = new MediaBrowserCompat(this.reactContext, mMediaAppDetails.componentName,
new MediaBrowserCompat.ConnectionCallback() {
#Override
public void onConnected() {
setupMediaController();
// mBrowseMediaItemsAdapter.setRoot(mBrowser.getRoot());
}
#Override
public void onConnectionSuspended() {
//TODO(rasekh): shut down browser.
// mBrowseMediaItemsAdapter.setRoot(null);
}
#Override
public void onConnectionFailed() {
showToastAndFinish("connection failed .. shit!");
}
}, null);
mBrowser.connect();
} else if (mMediaAppDetails.sessionToken != null) {
setupMediaController();
}
token = mBrowser.getSessionToken();
Toast.makeText(this.reactContext, "no token can't open controller", Toast.LENGTH_SHORT).show();
// toast
}
// Toast.makeText(this.reactContext, "found token", Toast.LENGTH_SHORT).show();
if(mBrowser == null )mBrowser = new MediaBrowserCompat(this.reactContext, new ComponentName(packageName, "MainActivity"), null, null);
MediaControllerCompat.TransportControls transportControls;
try{
mController = new MediaControllerCompat(this.reactContext, token);
if(mController!= null) {
transportControls = mController.getTransportControls();
transportControls.play();
}
}catch(Exception E){
Log.w("Error",E);
Log.w("Error","couldn't create mediaControllerCompat");
// System.out.println(E);
// System.out.println("couldn't create mediaControllerCompat");
}
}

ApplicationInfo for Settings pages in android

I'm trying to get the ApplicationInfo for Settings.ACTION_ACCESSIBILITY_SETTINGS or Settings.ACTION_BATTERY_SAVER_SETTINGS, so that I can launch them directly.
I tried following steps but it's return setting's ApplicationInfo.
private static final String[] SETTINGS_ACTION_MENUS = new String[]{
Settings.ACTION_ACCESSIBILITY_SETTINGS,Settings.ACTION_ADD_ACCOUNT};
public void getSettingPages(){
for(String menu : SETTINGS_ACTION_MENUS){
final Intent intent = new Intent(menu);
ComponentName componentName = intent.resolveActivity(packageManager);
if(componentName != null){
try {
ApplicationInfo info = packageManager.getApplicationInfo(componentName.getPackageName(),0);
if(packageManager.getLaunchIntentForPackage(info.packageName) != null){
launchableApps.add(info);
}
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
}
}
}
I tried following steps but it's return setting's ApplicationInfo
That is because those Intent actions are tied to activities in the Settings app. Individual activities do not have their own ApplicationInfo.

How can I find if a particular package exists on my Android device?

How can I find whether a particular package or application, say: com.android.abc, exists on my Android device?
Call any of the below method with the package name.
import android.content.pm.PackageManager;
// ...
public boolean isPackageExisted(String targetPackage){
List<ApplicationInfo> packages;
PackageManager pm;
pm = getPackageManager();
packages = pm.getInstalledApplications(0);
for (ApplicationInfo packageInfo : packages) {
if(packageInfo.packageName.equals(targetPackage))
return true;
}
return false;
}
import android.content.pm.PackageManager;
public boolean isPackageExisted(String targetPackage){
PackageManager pm=getPackageManager();
try {
PackageInfo info=pm.getPackageInfo(targetPackage,PackageManager.GET_META_DATA);
} catch (PackageManager.NameNotFoundException e) {
return false;
}
return true;
}
Without using a try-catch block or iterating through a bunch of packages:
public static boolean isPackageInstalled(Context context, String packageName) {
final PackageManager packageManager = context.getPackageManager();
Intent intent = packageManager.getLaunchIntentForPackage(packageName);
if (intent == null) {
return false;
}
List<ResolveInfo> list = packageManager.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
return list.size() > 0;
}
Kotlin
fun isPackageExist(context: Context, target: String): Boolean {
return context.packageManager.getInstalledApplications(0).find { info -> info.packageName == target } != null
}
Edit: Extension Function
fun Context.isPackageExist(target: String): Boolean {
return packageManager.getInstalledApplications(0).find { info -> info.packageName == target } != null
}
Intent intent = new Intent("com.google.zxing.client.android.SCAN");
PackageManager packageManager = getPackageManager();
List<ResolveInfo> activities = packageManager.queryIntentActivities(intent, 0);
boolean isIntentSafe = activities.size() > 0;
We can check like this:
if(getPackageManager().hasSystemFeature("android.software.webview") == true && isPackageExisted("com.google.android.webview")) {
if (Constant.isNetworkConnected(Activity.this)) {
//Your Intent
} else {
Toast.makeText(getApplicationContext(), resources.getString(R.string.internet_error), Toast.LENGTH_SHORT).show();
}
} else
{
Constant.showDialog(Activity.this,"Please install the webview");
}
}
Make method for package check ! this credit goes to "Kavi" https://stackoverflow.com/a/30708227/6209105
public boolean isPackageExisted(String targetPackage) {
List<ApplicationInfo> packages;
PackageManager pm;
pm = getPackageManager();
packages = pm.getInstalledApplications(0);
for (ApplicationInfo packageInfo : packages) {
if(packageInfo.packageName.equals(targetPackage))
{
return true;
}
}
return false;
}
You should use PackageManager's function called getInstalledPackages() to get the list of all installed packages and the search for the one you are interested in. Note that package name is located in PackageInfo.packageName field.
Since some devices have reported that the "getInstalledPackages" can cause TransactionTooLargeException (check here, here and here), I think you should also have a fallback like I did below.
This issue was supposed to be fixed on Android 5.1 (read here), but some still reported about it.
public static List<String> getInstalledPackages(final Context context) {
List<String> result = new ArrayList<>();
final PackageManager pm = context.getPackageManager();
try {
List<PackageInfo> apps = pm.getInstalledPackages(0);
for (PackageInfo packageInfo : apps)
result.add(packageInfo.packageName);
return result;
} catch (Exception ignored) {
//we don't care why it didn't succeed. We'll do it using an alternative way instead
}
// use fallback:
BufferedReader bufferedReader = null;
try {
Process process = Runtime.getRuntime().exec("pm list packages");
bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line;
while ((line = bufferedReader.readLine()) != null) {
final String packageName = line.substring(line.indexOf(':') + 1);
result.add(packageName);
}
closeQuietly(bufferedReader);
process.waitFor();
} catch (Exception e) {
e.printStackTrace();
} finally {
closeQuietly(bufferedReader);
}
return result;
}
public static void closeQuietly(final Closeable closeable) {
if (closeable == null)
return;
try {
closeable.close();
} catch (final IOException e) {
}
}
If you just want to use adb:
adb shell "pm list packages"|cut -f 2 -d ":"
it will list all installed packages.
You can use pm.getPackageUid() instead of iterating over the pm.getInstalledApplications()
boolean isPackageInstalled;
PackageManager pm = getPackageManager();
int flags = 0;
try
{
pm.getPackageUid(packageName,flags);
isPackageInstalled = true;
}
catch (final PackageManager.NameNotFoundException nnfe)
{
isPackageInstalled = false;
}
return isPackageInstalled;
According to the Package visibility filtering changes in Android 11, you need to add this permission to your manifest to be able to list installed apps:
<uses-permission android:name="android.permission.QUERY_ALL_PACKAGES"/>
but Google doesn't recommend to use this way. You should use <queries> tag instead:
<manifest ...>
<queries>
<package android:name="com.app.package" />
...
</queries>
...
</manifest>
And in your code:
fun isAppInstalled(context: Context, packageId: String): Boolean {
return try {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
context.packageManager
.getApplicationInfo(packageId, PackageManager.ApplicationInfoFlags.of(0))
} else {
context.packageManager.getApplicationInfo(packageId, 0)
}
true
} catch (e: PackageManager.NameNotFoundException) {
false
}
}

get application name from package name

I want to get the application name from application package name. Somebody please show me how I can get this.
public class AppInstalledListener extends BroadcastReceiver{
#Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
String action = intent.getAction();
if(action.equals("android.intent.action.PACKAGE_ADDED")){
Logger.debug("DATA:"+intent.getData().toString());
}
if(action.equals("android.intent.action.PACKAGE_REMOVED")){
Logger.debug("DATA:"+intent.getData().toString());
}
if(action.equals("android.intent.action.PACKAGE_REPLACED")){
Logger.debug("DATA:"+intent.getData().toString());
}
}
}
You can use PackageManager class to obtain ApplicationInfo:
final PackageManager pm = getApplicationContext().getPackageManager();
ApplicationInfo ai;
try {
ai = pm.getApplicationInfo( this.getPackageName(), 0);
} catch (final NameNotFoundException e) {
ai = null;
}
final String applicationName = (String) (ai != null ? pm.getApplicationLabel(ai) : "(unknown)");
This would return the application name as defined in <application> tag of its manifest.
Try this
final String packageName = "my.application.package"
PackageManager packageManager= getApplicationContext().getPackageManager();
String appName = (String) packageManager.getApplicationLabel(packageManager.getApplicationInfo(packageName, PackageManager.GET_META_DATA));
and replace packageName with your package full name.
you can get packageName using mContext.getPackageName() where mContext = yourActivityName.this for Activity and getActivity() for fragment.
public static String getAppNameFromPkgName(Context context, String Packagename) {
try {
PackageManager packageManager = context.getPackageManager();
ApplicationInfo info = packageManager.getApplicationInfo(Packagename, PackageManager.GET_META_DATA);
String appName = (String) packageManager.getApplicationLabel(info);
return appName;
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
return "";
}
}
It seems that you are able to receive the event of new package added after that its a very simple concept to get the all relevant information about that package like one such information is the application name so here is the concept
-> your device package manager has all the information related to it so just make an object of that it will give you all the information related with the package name.
-> You should also remember that the intent gives you "package: real_package_name" so first you have to get real name first by spilling(I used) or by any other simple implementation of String
-> Here is the code hope you will get what you want I m also giving information about how you can get app name,app icon,app version,app version code etc.....
public class NewAppReciver extends BroadcastReceiver{
#Override
public void onReceive(Context context, Intent intent) {
if(intent.getAction().equals("android.intent.action.PACKAGE_ADDED")){
String[] a=intent.getData().toString().split(":");
String packageName=a[a.length-1];
List<PackageInfo> packageInfoList = context.getPackageManager().getInstalledPackages(0);
for (int i = 0; i < packageInfoList.size(); i++) {
PackageInfo packageInfo = packageInfoList.get(i);
if(packageInfo.packageName.equals(packageName)){
String appName = packageInfo.applicationInfo.loadLabel(context.getPackageManager()).toString();
String appVersion = packageInfo.versionName;
int appVerCode = packageInfo.versionCode;
Drawable app_icon = packageInfo.applicationInfo.loadIcon(context.getPackageManager());
}
}
}
}
}
But at the time of the application Uninstall you can only get the package name as on Un installation all other information gets removed by the system.
Try this below, it worked for me.
public static ArrayList<ApplicationInfo> getLaunchableInstalledApps(Context context){
final PackageManager pm = context.getPackageManager();
Intent intent = new Intent(Intent.ACTION_MAIN, null);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
ArrayList<ApplicationInfo> list = new ArrayList<>();
for (ResolveInfo resolveInfo:pm.queryIntentActivities(intent, PackageManager.GET_META_DATA)){
try {
ApplicationInfo app = context.getPackageManager().getApplicationInfo(resolveInfo.activityInfo.packageName, 0);
list.add(app);
} catch (Exception e) {
e.printStackTrace();
}
}
if (cacheAppsList.isEmpty()){
cacheAppsList.addAll(list);
}
return list;
}
PackageManager pm = getPackageManager();
IntentFilter filter = new IntentFilter();
filter.addAction("android.intent.action.MAIN");
filter.addCategory("android.intent.category.HOME");
filter.addCategory("android.intent.category.DEFAULT");
Context context = getApplicationContext();
ComponentName component = new ComponentName(context.getPackageName(), TestReplaceHomeAppActivity.class.getName());
ComponentName[] components = new ComponentName[] {new ComponentName("com.android.launcher", "com.android.launcher.Launcher"), component};
pm.clearPackagePreferredActivities("com.android.launcher");
pm.addPreferredActivity(filter, IntentFilter.MATCH_CATEGORY_EMPTY, components, component);

Categories

Resources