clearPackagePreferredActivities exception with Android multi accounts - android

I'm trying to unset my app default actions with :
final PackageManager pm = getPackageManager();
pm.clearPackagePreferredActivities( getPackageName() );
It works with the primary account of the device but it throws an exception with secondary accounts:
"Neither user 1010080 nor current process has android.permission.SET_PREFERRED_APPLICATIONS."
Of course I added android.permission.SET_PREFERRED_APPLICATIONS to the manifest (but I think it is not used anymore)
I tried "to change the context" with something like:
final PackageManager pm = getApplicationContext().getPackageManager();
pm.clearPackagePreferredActivities( getPackageName() );
without success.
My App is an Home Application (for kids) that locks almost everything and I want a 'quit' button that restore user home (or the choice of home at least). May be there's another solution.
Thanks.

clearPackagePreferredActivities & addPackageToPreferred are deprecated by android since API level 8.
A modern method to set preferred activity is to start an intent, like this :
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.addCategory(Intent.CATEGORY_DEFAULT);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);

Related

how can i recall a hidden app from phone dialer?

My Final project requires that app must be hidden,and never opened again unless entering some code in phone dialer (ex: *123#) can you help me guys to do that task?
This is a bit tricky and it has its up and downs, but what you need to do basically is:
On app install, you need to programmatically disable the app Icon so you cannot open it manually.
Have a BroadcastReceiver registered with the PROCESS_OUTGOING_CALLS intent filter (don't forget to set the uses-permissions).
In the receiver, listen for every dialed number and when it matches yours you need to activate the App Icon again and then you start the activity with possibly extra data to handle it later.
After processing the data in your activity remember to deactivate the icon again.
To programmatically disable the icon use:
PackageManager packageManager = getPackageManager();
ComponentName componentName = new ComponentName(this, MainActivity.class);
packageManager.setComponentEnabledSetting(
componentName,PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP
);
To enable it:
PackageManager packageManager = context.getPackageManager();
ComponentName componentName = new ComponentName(context, MainActivity.class);
packageManager.setComponentEnabledSetting(
componentName,PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP
);
In your receiver to get the dialed number you need to use:
if (intent.getAction().equals(Intent.ACTION_NEW_OUTGOING_CALL)) {
String number = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
// Validate and start your activity here
// To start an activity from a receiver you need to use the flag FLAG_ACTIVITY_NEW_TASK in your intent
}
Note: After hiding the icon programmatically you might want to finish() the activity so it closes automatically at first run.
P.S I have a working sample of this, so rest assured as I have tested it actually works, sadly I cannot spoon feed you in your final project. Don't hesitate to ask anything tho. Good luck
There is no such functionality in Android. You may be able to do it with a custom home screen, but there is no "hide this app" functionality in the default launcher.

Android Auto - launch third party app from within my app?

Is it possible to launch any third party application from my application on Android Auto
I couldn't find anything mentioned on this anywhere.
Note: Please note "Android Auto" (Car) words here. I am not asking for android mobile application.
Idea of android auto is completely different from what you are trying to do.
Android auto provides a platform where it has done the basic things with a good user interface making sure not to distract user much.
All that you need to do is provide services which this platform can use.
As of now you can provide Music and Messaging services which are compatible with android auto.
You can launch applications code something like
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.setClassName("com.example.youpackage", "com.example.LauchActivity");
startActivity(intent);
And if you want get all possible application list for launch.code :
Declare you intent and add value you want pass
PackageManager packageManager = getPackageManager();
List<ResolveInfo> activities = packageManager.queryIntentActivities(intent, 0);
boolean isIntentSafe = activities.size() > 0;
if (isIntentSafe) {
startActivity(mapIntent);
}
And another way to start you specific application
PackageManager pm = getPackageManager();
Intent intent = pm.getLaunchIntentForPackage("com.example.package");
startActivity(intent);

How to launch a mobile app from another app?

Is there any way to launch for one mobile app to launch another mobile app, e.g. via a button click?
Example: the org.apache.cordova.camera plugin allows direct access to the camera on a button click. In the same way, how can one app launch another app?
You can use this java code:
Intent LaunchIntent = this.cordova.getActivity().getPackageManager().getLaunchIntentForPackage("appPackage");
this.cordova.getActivity().startActivity(LaunchIntent);
or try any of these 2 plugins for launching apps:
https://github.com/lampaa/com.lampa.startapp
https://github.com/dmedvinsky/cordova-startapp
Try this
Intent LaunchIntent = getPackageManager().getLaunchIntentForPackage("package name here");
startActivity( LaunchIntent );
If you don't know the package name of application that you want to launch then try this
PackageManager pm;
pm = getPackageManager();
// get a list of all installed apps then launch by pakagename.
packages = pm.getInstalledApplications(0);
String packagename = packages.get(position).packageName.toString()
Refer this android-package-manager
You need to find out full name of application, and then start it as activity via intent like this:
Intent myIntent = new Intent(getApplicationContext(), "full name of activity you are starting");
startActivity(myIntent);
You can even receive result from that activity check this. Hope this helps!

My homescreen app (which starts on boot) needs to list and link to inbuilt apps like camera, clock etc. How can do that?

I have an activity that starts at boot with 9 icons in it. When user clicks on these icons, the respective apps need to be launched. Some of them are inbuilt like camera, clock, internet etc. This is on Android 3.0 (tablet). How should i achieve this?
First get list all of the available apps:
final PackageManager pm = getPackageManager();
//get a list of installed apps.
List<ApplicationInfo> packages = pm
.getInstalledApplications(PackageManager.GET_META_DATA);
for (ApplicationInfo packageInfo : packages) {
Log.d(TAG, "Installed package :" + packageInfo.packageName);
Log.d(TAG,
"Launch Activity :"
+ pm.getLaunchIntentForPackage(packageInfo.packageName));
}// the getLaunchIntentForPackage returns an intent that you can use with startActivity()
}
Link every app to a drawable(icon) and the intent
Start that intent when the icon is clicked.
I think the launching of inbuilts does not depend on Android version.
Launching camera
Launching browser
There are a lot of tips online. Search deeper.
Have you seen adw-launcher-android ?
This project is open-source and it contains classes which lists the in-built/installed applications and opens them inside this application itself.
I think this might help
final Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
startActivity(mainIntent);
From this List you can take the required apps

How would I go about getting the package names of the applications that can be Home on android

My activity gets set as the default Home by the user, but I want to be able to launch the default Home from within my activity as well. I've tried hard coding the following:
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
PackageManager pm = getPackageManager();
String packageName = "com.android.launcher";
String className = "com.android.launcher.Launcher";
final ComponentName cn = new ComponentName(packageName, >className);
intent.setComponent(cn);
This seems to work on my droid, but force closes on the HTC Ally. I'm thinking there's an easier way to just get a list of the apps that have the category Home and Default.
For those that have used the Home Swittcher app. I essentially want to generate that list of installed default Home activities on the phone.
Step #1: Create an Intent that launches whatever the user has for the default HOME application. In other words, do not try putting in the ComponentName.
Step #2: Call queryIntentActivities() on PackageManager to find out who all can respond to that Intent.
Step #3: If there are only two entries in the list, find out which is yours, and the other one is the platform's. If there are 3+ entries in the list, remove yours and let the user choose among the remaining options.
You could sorta combine #2 and #3 by using queryIntentActivityOptions() if you like, setting it up to filter your own Intent out of the list.

Categories

Resources