下面列出了android.content.Intent#hasCategory ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if ((getIntent().getFlags() & 4194304) != 0) {
if (getIntent().getAction().equals("android.hardware.usb.action.USB_ACCESSORY_ATTACHED")) {
X8Application.isAoaTopActivity = true;
ConnectRcManager.getInstance().connectRC(this);
}
finish();
return;
}
if (!isTaskRoot()) {
Intent intent = getIntent();
String action = intent.getAction();
if (intent.hasCategory("android.intent.category.LAUNCHER") && action != null && action.equals("android.intent.action.MAIN")) {
finish();
return;
} else if (action.equals("android.hardware.usb.action.USB_ACCESSORY_ATTACHED")) {
X8Application.isAoaTopActivity = true;
ConnectRcManager.getInstance().connectRC(this);
finish();
return;
}
}
startActivity(new Intent(this, SplashActivity.class));
finish();
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setTheme(R.style.AppTheme);
if (!this.isTaskRoot()) {// 判断此activity是不是任务控件的源Activity,“非”也就是说是被系统重新实例化出来的
Intent mainIntent = getIntent();
String action = mainIntent.getAction();
if (mainIntent.hasCategory(Intent.CATEGORY_LAUNCHER) && action.equals(Intent.ACTION_MAIN)) {
finish();
return;
}
}
if (GosApplication.flag != 0) {
GosBaseActivity.noIDAlert(this, R.string.AppID_Toast);
}
// 在配置文件中选择推送类型(0:不开启推送,1:极光推送,2:百度推送。默认为0)
gosPushManager = new GosPushManager(GosDeploy.setPushType(), this);
setContentView(R.layout.activity_gos_user_login);
// 设置actionBar
setActionBar(false, false, R.string.app_company);
initView();
initEvent();
}
@Override
public void initData(Activity activity) {
final Intent intent = activity.getIntent();
if (intent.getData() != null) {
mView.onStartFromUri();
} else {
// 避免从桌面启动程序后,会重新实例化入口类的activity
if (!activity.isTaskRoot()) {
final String intentAction = intent.getAction();
if (intent.hasCategory(Intent.CATEGORY_LAUNCHER) && intentAction != null && intentAction.equals(Intent.ACTION_MAIN)) {
mView.finish();
return;
}
}
if (mView.getPreferences().getBoolean(mView.getContext().getString(R.string.pk_default_read), false)) {
openBookFromRecent();
} else {
mView.onStartNormal(START_DELAY);
}
}
}
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ThreadUtils.execute(new Runnable() {
public void run() {
X8SplashActivity.this.syncServerFwInfo();
}
});
if ((getIntent().getFlags() & 4194304) != 0) {
if (getIntent().getAction().equals("android.hardware.usb.action.USB_ACCESSORY_ATTACHED")) {
TcpClient.getIntance().sendLog(" connect --》home--》usb out2in--->");
X8Application.isAoaTopActivity = true;
ConnectRcManager.getInstance().connectRC(this);
}
finish();
return;
}
if (!isTaskRoot()) {
Intent intent = getIntent();
String action = intent.getAction();
if (intent.hasCategory("android.intent.category.LAUNCHER") && action != null && action.equals("android.intent.action.MAIN")) {
finish();
return;
} else if (action.equals("android.hardware.usb.action.USB_ACCESSORY_ATTACHED")) {
TcpClient.getIntance().sendLog("main runing ---> usb is in--->");
X8Application.isAoaTopActivity = true;
ConnectRcManager.getInstance().connectRC(this);
finish();
return;
}
}
startActivity(new Intent(this, X8DeviceSelectActivity.class));
finish();
}
static void tryReplaceActivityInfo(Intent intent, ActivityInfoReplacer replacer) {
if (intent == null) return;
String targetClass = unwrapIntent(intent);
boolean hasSetUp = Small.hasSetUp();
if (targetClass == null) {
// The activity was register in the host.
if (hasSetUp) return; // nothing to do
if (intent.hasCategory(Intent.CATEGORY_LAUNCHER)) {
// The launcher activity will setup Small.
return;
}
// Launching an activity in remote process. Set up Small for it.
Small.setUpOnDemand();
return;
}
if (!hasSetUp) {
// Restarting an activity after application recreated,
// maybe upgrading or somehow the application was killed in background.
Small.setUp();
}
// Replace with the REAL activityInfo
ActivityInfo targetInfo = sLoadedActivities.get(targetClass);
replacer.replace(targetInfo);
// Ensure the merged application-scope resource has been cached so that
// the incoming activity can attach to it without creating a new(unmerged) one.
ReflectAccelerator.ensureCacheResources();
}
private boolean isHomeIntent(Intent intent) {
return ACTION_MAIN.equals(intent.getAction())
&& intent.hasCategory(CATEGORY_HOME)
&& intent.getCategories().size() == 1
&& intent.getData() == null
&& intent.getType() == null;
}
static boolean isMainIntent(Intent intent) {
return ACTION_MAIN.equals(intent.getAction())
&& intent.hasCategory(CATEGORY_LAUNCHER)
&& intent.getCategories().size() == 1
&& intent.getData() == null
&& intent.getType() == null;
}
/**
* Returns true if the intent is a valid launch intent for a launcher activity of an app.
* This is used to identify shortcuts which are different from the ones exposed by the
* applications' manifest file.
*
* @param launchIntent The intent that will be launched when the shortcut is clicked.
*/
public static boolean isLauncherAppTarget(Intent launchIntent) {
if (launchIntent != null
&& Intent.ACTION_MAIN.equals(launchIntent.getAction())
&& launchIntent.getComponent() != null
&& launchIntent.getCategories() != null
&& launchIntent.getCategories().size() == 1
&& launchIntent.hasCategory(Intent.CATEGORY_LAUNCHER)
&& TextUtils.isEmpty(launchIntent.getDataString())) {
// An app target can either have no extra or have ItemInfo.EXTRA_PROFILE.
Bundle extras = launchIntent.getExtras();
return extras == null || extras.keySet().isEmpty();
}
return false;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/*
Recommended solution to avoid opening of multiple tasks of our app's launcher activity.
For more info:
- https://issuetracker.google.com/issues/36907463
- https://stackoverflow.com/questions/4341600/how-to-prevent-multiple-instances-of-an-activity-when-it-is-launched-with-differ/
- https://stackoverflow.com/questions/16283079/re-launch-of-activity-on-home-button-but-only-the-first-time/16447508#16447508
*/
if (!isTaskRoot()) {
final Intent intent = getIntent();
if (intent.hasCategory(Intent.CATEGORY_LAUNCHER) && Intent.ACTION_MAIN.equals(intent.getAction())) {
return;
}
}
final IEdxEnvironment environment = MainApplication.getEnvironment(this);
if (environment.getUserPrefs().getProfile() != null) {
environment.getRouter().showMainDashboard(SplashActivity.this);
} else if (!environment.getConfig().isRegistrationEnabled()) {
startActivity(environment.getRouter().getLogInIntent());
} else {
environment.getRouter().showLaunchScreen(SplashActivity.this);
}
}
public static int onStartActivity(int res, IApplicationThread caller, String callingPackage, Intent intent) {
if (res >= 0 && intent != null && (intent.hasCategory(Intent.CATEGORY_HOME) || intent.hasCategory(Intent.CATEGORY_LAUNCHER))) {
ProcessRecord callerApp = getAms().getRecordForAppLocked(caller);
if (callerApp != null) {
mPreventRunning.onStartHomeActivity(callerApp.info.packageName);
}
}
return res;
}
@Override
public boolean onControlRequest(Intent intent, android.support.v7.media.MediaRouter.ControlRequestCallback callback) {
if (intent.hasCategory(CATEGORY_DLNA)) {
return true;
} else {
return false;
}
}
@Override
protected void onCreateWithService(final Bundle savedInstanceState) {
setContentView(R.layout.activity_sell);
if (mService.isElements())
setTitle(R.string.cash_in);
final Intent intent = getIntent();
final boolean isBitcoinUri = TabbedMainActivity.isBitcoinScheme(intent) ||
intent.hasCategory(Intent.CATEGORY_BROWSABLE) ||
NfcAdapter.ACTION_NDEF_DISCOVERED.equals(intent.getAction());
final View v = UI.find(this, R.id.sell_fragment);
if (isBitcoinUri) {
v.setTag(R.id.tag_bitcoin_uri, getIntent().getData());
}
if (intent.getStringExtra("sendAmount") != null) {
v.setTag(R.id.tag_amount, intent.getStringExtra("sendAmount"));
}
final SendFragment sellFragment = new SendFragment();
sellFragment.setIsExchanger(true);
sellFragment.setPageSelected(true);
getSupportFragmentManager()
.beginTransaction()
.add(R.id.sell_fragment, sellFragment, "tag2")
.disallowAddToBackStack()
.commit();
}
/**
* Returns true if the intent is a valid launch intent for a shortcut.
* This is used to identify shortcuts which are different from the ones exposed by the
* applications' manifest file.
*
* When DISABLE_ALL_APPS is true, shortcuts exposed via the app's manifest should never be
* duplicated or removed(unless the app is un-installed).
*
* @param launchIntent The intent that will be launched when the shortcut is clicked.
*/
static boolean isValidShortcutLaunchIntent(Intent launchIntent) {
if (launchIntent != null
&& Intent.ACTION_MAIN.equals(launchIntent.getAction())
&& launchIntent.getComponent() != null
&& launchIntent.getCategories() != null
&& launchIntent.getCategories().size() == 1
&& launchIntent.hasCategory(Intent.CATEGORY_LAUNCHER)
&& launchIntent.getExtras() == null
&& TextUtils.isEmpty(launchIntent.getDataString())) {
return false;
}
return true;
}
@Override
public void onReceive(Context context, Intent intent) {
Log.d(TAG, "onReceive");
super.onReceive(context, intent);
SharedPreferences mPreferences = PreferenceManager.getDefaultSharedPreferences(context);
if (intent.hasCategory(Intent.CATEGORY_ALTERNATIVE)) {
Uri mData = intent.getData();
int mButtonId;
int mWidgetId;
mWidgetId = Integer.parseInt(mData.getSchemeSpecificPart().split("/")[0]);
mButtonId = Integer.parseInt(mData.getSchemeSpecificPart().split("/")[1]);
if (mButtonId == 0) {
Intent mPendingIntent = new Intent(TorchSwitch.TOGGLE_FLASHLIGHT);
mPendingIntent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND);
mPendingIntent.putExtra("sos",
mPreferences.getBoolean("widget_sos" + mWidgetId, false));
context.sendBroadcast(mPendingIntent);
}
try {
Thread.sleep(50);
} catch (InterruptedException e) {
// TODO auto-generated catch block
e.printStackTrace();
}
this.updateAppWidget(context);
} else if (intent.getAction().equals(TorchSwitch.TORCH_STATE_CHANGED)) {
this.updateAppWidget(context);
}
}
@Override
protected void beforeOnCreate(Bundle savedInstanceState)
{
super.beforeOnCreate(savedInstanceState);
//app首次安装完成后在安装界面直接“打开”应用再按home键返回桌面,重新进入app重复实例化launcher activity的问题解决方案
//判断该Activity是不是任务空间的源Activity,“非”也就是说是被系统重新实例化出来
if (!this.isTaskRoot())
{
Intent mainIntent = getIntent();
String action = mainIntent.getAction();
if (mainIntent.hasCategory(Intent.CATEGORY_LAUNCHER) && action.equals(Intent.ACTION_MAIN))
mIsReLaunch = true;
}
}
/**
* Returns true if the app should ignore a given intent.
*
* @param intent Intent to check.
* @return true if the intent should be ignored.
*/
public boolean shouldIgnoreIntent(Intent intent) {
// Although not documented to, many/most methods that retrieve values from an Intent may
// throw. Because we can't control what packages might send to us, we should catch any
// Throwable and then fail closed (safe). This is ugly, but resolves top crashers in the
// wild.
try {
// Ignore all invalid URLs, regardless of what the intent was.
if (!intentHasValidUrl(intent)) {
return true;
}
// Determine if this intent came from a trustworthy source (either Chrome or Google
// first party applications).
boolean isInternal = isIntentChromeOrFirstParty(intent);
boolean isFromChrome = wasIntentSenderChrome(intent);
// "Open new incognito tab" is currently limited to Chrome.
//
// The pending incognito URL check is to handle the case where the user is shown an
// Android intent picker while in incognito and they select the current Chrome instance
// from the list. In this case, we do not apply our Chrome token as the user has the
// option to select apps outside of our control, so we rely on this in memory check
// instead.
if (!isFromChrome
&& IntentUtils.safeGetBooleanExtra(
intent, EXTRA_OPEN_NEW_INCOGNITO_TAB, false)
&& (getPendingIncognitoUrl() == null
|| !getPendingIncognitoUrl().equals(intent.getDataString()))) {
return true;
}
// Now if we have an empty URL and the intent was ACTION_MAIN,
// we are pretty sure it is the launcher calling us to show up.
// We can safely ignore the screen state.
String url = getUrlFromIntent(intent);
if (url == null && Intent.ACTION_MAIN.equals(intent.getAction())) {
return false;
}
// Ignore all intents that specify a Chrome internal scheme if they did not come from
// a trustworthy source.
String scheme = getSanitizedUrlScheme(url);
if (!isInternal && scheme != null
&& (intent.hasCategory(Intent.CATEGORY_BROWSABLE)
|| intent.hasCategory(Intent.CATEGORY_DEFAULT)
|| intent.getCategories() == null)) {
String lowerCaseScheme = scheme.toLowerCase(Locale.US);
if (UrlConstants.CHROME_SCHEME.equals(lowerCaseScheme)
|| UrlConstants.CHROME_NATIVE_SCHEME.equals(lowerCaseScheme)
|| ContentUrlConstants.ABOUT_SCHEME.equals(lowerCaseScheme)) {
// Allow certain "safe" internal URLs to be launched by external
// applications.
String lowerCaseUrl = url.toLowerCase(Locale.US);
if (ContentUrlConstants.ABOUT_BLANK_DISPLAY_URL.equals(lowerCaseUrl)
|| ContentUrlConstants.ABOUT_BLANK_URL.equals(lowerCaseUrl)) {
return false;
}
Log.w(TAG, "Ignoring internal Chrome URL from untrustworthy source.");
return true;
}
}
// We must check for screen state at this point.
// These might be slow.
boolean internalOrVisible = isInternal || isIntentUserVisible();
if (!internalOrVisible) {
updateDeferredIntent(intent);
return true;
}
return false;
} catch (Throwable t) {
return true;
}
}
private boolean isMainIntentFromLauncher(Intent intent) {
return intent != null && TextUtils.equals(intent.getAction(), Intent.ACTION_MAIN)
&& intent.hasCategory(Intent.CATEGORY_LAUNCHER);
}
@Override
public void initSplash(ConfigSplash configSplash) {
if (!isTaskRoot()) {
final Intent intent = getIntent();
if (intent.hasCategory(Intent.CATEGORY_LAUNCHER) && Intent.ACTION_MAIN.equals(intent.getAction())) {
Log.w(LOG_TAG, "Main Activity is not the root. Finishing Main Activity instead of launching.");
finish();
return;
}
}
/* you don't have to override every property */
//Customize Circular Reveal
configSplash.setBackgroundColor(R.color.lightbluesplash); //any color you want form colors.xml
configSplash.setAnimCircularRevealDuration(1000); //int ms
configSplash.setRevealFlagX(Flags.REVEAL_RIGHT); //or Flags.REVEAL_LEFT
configSplash.setRevealFlagY(Flags.REVEAL_BOTTOM); //or Flags.REVEAL_TOP
//Choose LOGO OR PATH; if you don't provide String value for path it's logo by default
//Customize Logo
configSplash.setLogoSplash(R.drawable.cglogo); //or any other drawable
configSplash.setAnimLogoSplashDuration(1000); //int ms
configSplash.setAnimLogoSplashTechnique(Techniques.FadeIn); //choose one form Techniques (ref: https://github.com/daimajia/AndroidViewAnimations)
//Customize Path
// configSplash.setPathSplash(Constants.DROID_LOGO); //set path String
configSplash.setOriginalHeight(400); //in relation to your svg (path) resource
configSplash.setOriginalWidth(400); //in relation to your svg (path) resource
configSplash.setAnimPathStrokeDrawingDuration(1000);
configSplash.setPathSplashStrokeSize(3); //I advise value be <5
configSplash.setPathSplashStrokeColor(R.color.lightbluesplash); //any color you want form colors.xml
configSplash.setAnimPathFillingDuration(1000);
configSplash.setPathSplashFillColor(R.color.white); //path object filling color
//Customize Title
configSplash.setTitleSplash(getResources().getString(R.string.charetakergames));
configSplash.setTitleTextColor(R.color.white);
configSplash.setTitleTextSize(20f); //float value
configSplash.setAnimTitleDuration(0);
configSplash.setAnimTitleTechnique(Techniques.FadeIn);
configSplash.setTitleFont("fonts/grobold.ttf"); //provide string to your font located in assets/fonts/
}
/**
* Returns true if the app should ignore a given intent.
*
* @param context Android Context.
* @param intent Intent to check.
* @return true if the intent should be ignored.
*/
public boolean shouldIgnoreIntent(Context context, Intent intent) {
// Although not documented to, many/most methods that retrieve values from an Intent may
// throw. Because we can't control what packages might send to us, we should catch any
// Throwable and then fail closed (safe). This is ugly, but resolves top crashers in the
// wild.
try {
// Ignore all invalid URLs, regardless of what the intent was.
if (!intentHasValidUrl(intent)) {
return true;
}
// Determine if this intent came from a trustworthy source (either Chrome or Google
// first party applications).
boolean isInternal = isIntentChromeOrFirstParty(intent, context);
// "Open new incognito tab" is currently limited to Chrome or first parties.
if (!isInternal
&& IntentUtils.safeGetBooleanExtra(
intent, EXTRA_OPEN_NEW_INCOGNITO_TAB, false)) {
return true;
}
// Now if we have an empty URL and the intent was ACTION_MAIN,
// we are pretty sure it is the launcher calling us to show up.
// We can safely ignore the screen state.
String url = getUrlFromIntent(intent);
if (url == null && Intent.ACTION_MAIN.equals(intent.getAction())) {
return false;
}
// Ignore all intents that specify a Chrome internal scheme if they did not come from
// a trustworthy source.
String scheme = getSanitizedUrlScheme(url);
if (!isInternal && scheme != null
&& (intent.hasCategory(Intent.CATEGORY_BROWSABLE)
|| intent.hasCategory(Intent.CATEGORY_DEFAULT)
|| intent.getCategories() == null)) {
String lowerCaseScheme = scheme.toLowerCase(Locale.US);
if ("chrome".equals(lowerCaseScheme) || "chrome-native".equals(lowerCaseScheme)
|| "about".equals(lowerCaseScheme)) {
// Allow certain "safe" internal URLs to be launched by external
// applications.
String lowerCaseUrl = url.toLowerCase(Locale.US);
if ("about:blank".equals(lowerCaseUrl)
|| "about://blank".equals(lowerCaseUrl)) {
return false;
}
Log.w(TAG, "Ignoring internal Chrome URL from untrustworthy source.");
return true;
}
}
// We must check for screen state at this point.
// These might be slow.
boolean internalOrVisible = isInternal || isIntentUserVisible(context);
return !internalOrVisible;
} catch (Throwable t) {
return true;
}
}
/**
* Returns true if the app should ignore a given intent.
*
* @param context Android Context.
* @param intent Intent to check.
* @return true if the intent should be ignored.
*/
public boolean shouldIgnoreIntent(Context context, Intent intent) {
// Although not documented to, many/most methods that retrieve values from an Intent may
// throw. Because we can't control what packages might send to us, we should catch any
// Throwable and then fail closed (safe). This is ugly, but resolves top crashers in the
// wild.
try {
// Ignore all invalid URLs, regardless of what the intent was.
if (!intentHasValidUrl(intent)) {
return true;
}
// Determine if this intent came from a trustworthy source (either Chrome or Google
// first party applications).
boolean isInternal = isIntentChromeOrFirstParty(intent, context);
// "Open new incognito tab" is currently limited to Chrome or first parties.
if (!isInternal
&& IntentUtils.safeGetBooleanExtra(
intent, EXTRA_OPEN_NEW_INCOGNITO_TAB, false)
&& (getPendingIncognitoUrl() == null
|| !getPendingIncognitoUrl().equals(intent.getDataString()))) {
return true;
}
// Now if we have an empty URL and the intent was ACTION_MAIN,
// we are pretty sure it is the launcher calling us to show up.
// We can safely ignore the screen state.
String url = getUrlFromIntent(intent);
if (url == null && Intent.ACTION_MAIN.equals(intent.getAction())) {
return false;
}
// Ignore all intents that specify a Chrome internal scheme if they did not come from
// a trustworthy source.
String scheme = getSanitizedUrlScheme(url);
if (!isInternal && scheme != null
&& (intent.hasCategory(Intent.CATEGORY_BROWSABLE)
|| intent.hasCategory(Intent.CATEGORY_DEFAULT)
|| intent.getCategories() == null)) {
String lowerCaseScheme = scheme.toLowerCase(Locale.US);
if ("chrome".equals(lowerCaseScheme) || "chrome-native".equals(lowerCaseScheme)
|| "about".equals(lowerCaseScheme)) {
// Allow certain "safe" internal URLs to be launched by external
// applications.
String lowerCaseUrl = url.toLowerCase(Locale.US);
if ("about:blank".equals(lowerCaseUrl)
|| "about://blank".equals(lowerCaseUrl)) {
return false;
}
Log.w(TAG, "Ignoring internal Chrome URL from untrustworthy source.");
return true;
}
}
// We must check for screen state at this point.
// These might be slow.
boolean internalOrVisible = isInternal || isIntentUserVisible(context);
return !internalOrVisible;
} catch (Throwable t) {
return true;
}
}