Android Default application - android

i would like to know how to show
the check box that makes some application the
default in the createChooser dlg.
thanks in advance.
yes David i would like to know how to clear the app from being default. also i would like to know whether i can call any intent if i know its action, category, package and "class name", can I?
I actually tried to call some activity i previously known its intent contents put some SECURITY EXP. raised. why?

Your question isn't 100% clear to me, so forgive me if I'm answering the wrong question. It sounds like you want to either:
Pop up the dialog that lets the user select your activity as the default to handle some intent; or
Check if your activity is the default for an intent.
I'll answer both.
To pop up the dialog that lets the user select your activity as the default to handle some intent, simply call startActivity() with that intent. If for example you wanted to register yourself as the default home screen you would simply:
Intent intent = new Intent();
intent.setAction(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
startActivity(intent);
1 of 3 things would happen.
Either there is no default specified (and if a user just installed your app, it will clear the default value) and thus the pop up will appear, or you're already the default or someone else is the default.
If you're already the default, then awesome, just figure out how to handle the intent this once (maybe add an EXTRA that explains that this was just a test so your activity knows to finish() immediately).
If someone else is the default you'll need to walk the user through clearing defaults for that app. If no one is the default you may want to throw up a Toast notification that explains what the user should do.
Toast.makeText(this,
"Please check 'Use by default for this action' and then press 'MyActivity'",
Toast.LENGTH_LONG).show();
These are all sort of messy situations to handle and it would be better to check first before calling startActivity(). To do that, use ResolveInfo.
ResolveInfo res = getPackageManager().resolveActivity(intent, 0);
// Got to be a better way to determine if there is no default...
if (res.activityInfo.packageName.equals("android")) {
// No default selected
} else if (res.activityInfo.packageName.equals(getPackageName())) {
// We are default
} else {
// Someone else is default
}
Hope this helps! If you want to know about how to make it easy for the user to clear another app from being default, let me know.

Related

Opening Do Not Disturb preferences in Oreo

I'm having troubles opening system DND preferences from my App so that user can create or edit Automatic Time rule.
Current situation
Our app already has a similar feature which disables App-notification LED, sound and vibration for some specific time period (for example between 10pm-8am) and is applied app-wide. As of Android Oreo, our feature doesn't work anymore because of Notification Channels. The only solution is, as far as I understand, to create in System preferences Automatic Time rule which is then applied system-wide.
What I want to do?
Just to redirect Oreo user from my app to System preferences ie. Do Not Disturb preferences in order to add or edit Time rule.
The problem
There is no specific Intent which opens Do Not Disturb preferences. The closest one I could find was Settings.ACTION_ZEN_MODE_PRIORITY_SETTINGS which leads me to this Preference screen. I also found action which I exactly need, but as you can see, it is hidden by annotation.
Does this mean there is no way to open this Preference screen and I should use another approach?
I also had this question for a long time, now I finally found the solution that works for me:
Java
Intent intent = new Intent();
intent.setComponent(new ComponentName("com.android.settings", "com.android.settings.Settings$ZenModeSettingsActivity"));
startActivity(intent);
Kotlin
val intent = Intent()
intent.component = ComponentName("com.android.settings", "com.android.settings.Settings\$ZenModeSettingsActivity")
startActivity(intent)
If you take a look at the AndroidManifest.xml for the Settings app you can see that there is an Activity Settings$ZenModeSettingsActivity (as mentioned by #cyb3rko in https://stackoverflow.com/a/63713587/467650) already from Android 5.0.
To send the user to the "Do not disturb" screen you can use the action android.settings.ZEN_MODE_SETTINGS like this:
try {
startActivity(new Intent("android.settings.ZEN_MODE_SETTINGS"));
} catch (ActivityNotFoundException e) {
// TODO: Handle activity not found
}
I would expect the intent filter to be even more stable than the class name.

Return back to App after clicking on Accessibility Service

Please Please read the question complete before marking it as duplicate or vague
On clicking of a button I want to redirect the user to accessibility settings of the android mobile. Where user can click on the accessibility settings of the application. Here is the code that I am using for the same:
Intent dummyIntent = new Intent(android.provider.Settings.ACTION_ACCESSIBILITY_SETTINGS);
startActivityForResult(dummyIntent, 1);
Problem: When user clicks on, I want that it should redirect back to my application and should not remain on the accessibility screen itself.
It's quite late but i had to make the same stuff. So my suggestion is to use onServiceConnected() overridden method in class that extends AccessibilityService . When user apply accessibility in the settings you can launch intent to desired activity inside onServiceConnected(). But you should keep in mind that after device reboot onServiceConnected() also called, so use some flag to make sure that is not user action.
try this :
Intent dummyIntent = new Intent(android.provider.Settings.ACTION_ACCESSIBILITY_SETTINGS);
ActivityContext.startActivityForResult(dummyIntent, 1);

Directing to different Intents from a single intent (What is the best practice?)

I'm building an app which has an Intro page with many slides. Once a first time user has gone through the intro, he'll be directed to a login screen. Once he logged in (or registered), he'll be taken into the app home page. As long as the user doesn't sign out, if he clicks on the app icon he'll be directly taken to the home screen.
I'm using the Intro page intent as the LAUNCHER activity and using sharedpreference to save 'first usage' and logged in states. By testing if the user has logged in or a first time user, I'm directing him to different intents.
So my question is, where is the most suitable position to have this intent redirection? Because Intro page has so many fragments and components, setting it as the LAUNCHER activity and having all the if else statements there to decide where the user should go, have I wasted system resources? Because if the user has already logged in, he'll taken into the home page without showing any app intro stuff which are loaded.
Or is it a good practice to create an empty activity and set that as the LAUNCHER activity and put all the if else statements in that. So the app doesn't need to go to the 'heavy' app intro page.
PS: I've declared those intent direction if else statements in the onCreate right after super.onCreate();
#Override
protected void onCreate(Bundle savedInstanceState) {
// Fullscreen
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
super.onCreate(savedInstanceState);
// activity_first_usage is the container for all frames
setContentView(R.layout.activity_first_usage);
logger = new Logger(this);
if (!logger.isFirstUsage()) {
if (logger.hasTOKEN()) {
// If user didn't log out, then he can stay in the app
Intent home = new Intent(getApplicationContext(), Home.class);
startActivity(home);
finish();
} else {
// If this is not the first time user login in, no need to show the intro
Intent directToSignIn = new Intent(getApplicationContext(), SignIn.class);
startActivity(directToSignIn);
finish();
}
} else {
// If not, continue with the Intro and set usage status to used
logger.setFirstUsage(false);
}
...
}
ill tell you the concept
use a splash and there use a condition to check user's state eg: already registered , new one , registered but still did not go through intro like wise
identify it
now you use shared preff
can write a file
can keep a enum value
or get from a server etc..
more : you can think about what happens when user uninstall your app and re install it.Then what you need to do ? up to you.
once you identify the state of the user in the splash
the write different intents to each of them
if a new one - display your intro
if not - load to your main menu
you need to decide cuz you knows the requirement
Hope this helps a bit :)

Clear Default Android Application

Can we know that user has set default application for particular action? i. e. android.intent.action.CALL_PRIVILEGED
Suppose I my application also provide called on action of Call_privilaged. but user has set inbuilt dialer as default launcher for Call_privilaged action.
My question is can I know pro grammatically that user has set dialer as default launcher for Call_privalged action.
Thank You.
Can we know that user has set default application for particular action? i. e. android.intent.action.CALL_PRIVILEGED
I do not think that there is an easy way to do this. Calling getPreferredActivities() on PackageManager, and sifting through the List<IntentFilter> you get back to try to find a match for your Intent might work.
You can use resolveActivity() of Intent or PackageManager.
Intent intent = ...
ComponentName componentName = intent.resolveActivity(getPackageManager());
if (componentName.getPackageName().equals("android")) {
// No default selected
...
} else if (componentName.getPackageName().equals(getPackageName())) {
// We are default
...
} else {
// Someone else is default
...
}
If you don't handle the intent yourself you could also need a null check for the case where there is no app able to handle the intent.
Not sure if this works on all devices and all versions of Android. Tested on Android 4.1-4.3 on Nexus devices.

Android Intent defaults - detect and clear

Is there a way to detect that a phone has a default application chosen for an intent such as android.intent.action.SEND ?
Is there a way to clear the default application via code?
I'd like to give the user an option to remove the default or at least show a screen telling them how to do it manually, if I can detect it.
Take a look at PackageManager. With it, you can determine how an Intent will be handled with resolveActivity(intent). It looks like the method for clearing the preference (clearPackagePreferredActivities) only works on your own package.
Use 2-step detection of defaults:
PackageManager.queryIntentActivities to get all activities for Intent, PackageManager.resolveActivity to get resolved.
If resolved one is in the list returned by queryIntentActivities, then there will be no "Complete action using" dialog, thus "default" activity was set.

Categories

Resources