Android Default application
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 tofinish()
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.
精彩评论