setClassName (String , String )与setClassName (Context,String )

本文介绍如何使用setClassName方法在不同应用间及同一应用内进行Activity跳转。跨应用跳转需指定目标应用的包名及Activity名称;而同一应用内的跳转仅需指定Activity名称。

setClassName (Context packageContext, String className)可以跳转到其他app中的activity


setClassName (String , String ) 跳转到的activity与当前activity在同一个app中


object WrapJumpUtils { private const val TAG = "HbydWrapJumpUtils" //行业应用action private const val INDUSTRY_CLASSNAME = "com.cndatacom.industry.MainActivity" private const val INDUSTRY_PACKAGE = "com.cndatacom.industry" fun getDefaultLauncherConfig(): Pair<String, String> { val profile = AuthenProxy.getInstance().userProfile ?: return Pair<String, String>("", "") var action: String = profile.defaultLauncherAction var param: String = profile.defaultLauncherParam //内部跳转示例:电视精选 // action = "bestv.ott.action.web" // param = "https%3a//hbydflash.bestv.com.cn/the-flash-zbhk/current/?action\u003dPlayByParam\u0026showCover\u003d0\u0026showMenu\u003d0\u0026categoryCode\u003dlive_new\u0026type\u003d1\u0026backEpg\u003d1\u0026data\u003dUmai%3aCHAN/111128@BESTV.SMG.SMG"; //未配置示例 // action = "" // param = "" //行业应用置示例 // action = "com.cndatacom.industry.MainActivity" // param = "" LogUtils.debug(TAG, "defaultLauncherAction = $action") LogUtils.debug(TAG, "defaultLauncherParam = $param") return Pair<String, String>(action, param) } fun isSwitchValueForceQuitApp(): Boolean { var isForceQuitApp = true val proxy = AuthenProxy.getInstance() val swichValue = proxy.getLocalModuleService(ConfigName.TM_QUIT_APP_FINISH_SWITCH) LogUtils.debug(TAG, "isSwitchValueForceQuitApp swichValue:$swichValue") if ("0" == swichValue) { isForceQuitApp = false } return isForceQuitApp } fun tryGotoWrapActivity(context: Context?) { if (context == null) { LogUtils.debug(TAG, "tryGotoWrapActivity context null") return } try { var defaultLauncherConfig = getDefaultLauncherConfig() ?: return val action: String = defaultLauncherConfig.first val param: String = defaultLauncherConfig.second LogUtils.debug(TAG, "tryGotoWrapActivity: $action, param: $param") if(action.isNullOrEmpty()){ return } if(isIndustryAction(action)) { //行业应用 LogUtils.debug(TAG, "goToIndustryActivity") goToIndustryActivity(context, param) } else { LogUtils.debug(TAG, "gotoOtherActivity") gotoOtherActivity(context, action, param) } } catch (e: Throwable) { e.printStackTrace() } } fun gotoRealLauncher(context: Context, isHomeKeyPressed: Boolean) { LogUtils.debug(TAG, "gotoRealLauncher") try { val intent = Intent() intent.setClassName(context, "com.bestv.ott.launcher.RealLauncher") intent.putExtra("isHomePressed", isHomeKeyPressed) intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK CommonUtils.startActivitySafely(context, intent) //HbydWrapActivity跳转之后,需要关闭页面 if (context is HbydWrapActivity) { LogUtils.debug(TAG, "finish HbydWrapActivity") context.finish() } } catch (e: Throwable) { e.printStackTrace() } } private fun goToIndustryActivity(context: Context, param: String?): Boolean { return CommonUtils.startActivitySafely(context, Intent().apply { setClassName(INDUSTRY_PACKAGE, INDUSTRY_CLASSNAME) flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED putExtra("param", param ?: "") }) } private fun isIndustryAction(action: String): Boolean { return INDUSTRY_CLASSNAME.contentEquals(action) } private fun gotoOtherActivity( context: Context, action: String?, param: String? ): Boolean { val intent = Intent(action) intent.putExtra("isHomePressed", false) intent.putExtra("param", param) intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK return CommonUtils.startActivitySafely(context, intent) } }逐句解析代码
06-07
/** * Preference controller for "Open network select" */ public class OpenNetworkSelectPagePreferenceController extends TelephonyBasePreferenceController implements AutoSelectPreferenceController.OnNetworkSelectModeListener, LifecycleObserver { private TelephonyManager mTelephonyManager; private Preference mPreference; private PreferenceScreen mPreferenceScreen; private AllowedNetworkTypesListener mAllowedNetworkTypesListener; private int mCacheOfModeStatus; public OpenNetworkSelectPagePreferenceController(Context context, String key) { super(context, key); mTelephonyManager = context.getSystemService(TelephonyManager.class); mSubId = SubscriptionManager.INVALID_SUBSCRIPTION_ID; mCacheOfModeStatus = TelephonyManager.NETWORK_SELECTION_MODE_UNKNOWN; mAllowedNetworkTypesListener = new AllowedNetworkTypesListener( context.getMainExecutor()); mAllowedNetworkTypesListener.setAllowedNetworkTypesListener( () -> updatePreference()); } private void updatePreference() { if (mPreferenceScreen != null) { displayPreference(mPreferenceScreen); } if (mPreference != null) { updateState(mPreference); } } @Override public int getAvailabilityStatus(int subId) { return MobileNetworkUtils.shouldDisplayNetworkSelectOptions(mContext, subId) ? AVAILABLE : CONDITIONALLY_UNAVAILABLE; } @OnLifecycleEvent(ON_START) public void onStart() { mAllowedNetworkTypesListener.register(mContext, mSubId); } @OnLifecycleEvent(ON_STOP) public void onStop() { mAllowedNetworkTypesListener.unregister(mContext, mSubId); } @Override public void displayPreference(PreferenceScreen screen) { super.displayPreference(screen); mPreferenceScreen = screen; mPreference = screen.findPreference(getPreferenceKey()); } @Override public void updateState(Preference preference) { super.updateState(preference); preference.setEnabled(mCacheOfModeStatus != TelephonyManager.NETWORK_SELECTION_MODE_AUTO); Intent intent = new Intent(); intent.setClassName(SETTINGS_PACKAGE_NAME, SETTINGS_PACKAGE_NAME + ".Settings$NetworkSelectActivity"); intent.putExtra(Settings.EXTRA_SUB_ID, mSubId); preference.setIntent(intent); } @Override public CharSequence getSummary() { final ServiceState ss = mTelephonyManager.getServiceState(); if (ss != null && ss.getState() == ServiceState.STATE_IN_SERVICE) { return MobileNetworkUtils.getCurrentCarrierNameForDisplay(mContext, mSubId); } else { return mContext.getString(R.string.network_disconnected); } } /** * Initialization based on given subscription id. **/ public OpenNetworkSelectPagePreferenceController init(int subId) { mSubId = subId; mTelephonyManager = mContext.getSystemService(TelephonyManager.class) .createForSubscriptionId(mSubId); return this; } @Override public void onNetworkSelectModeUpdated(int mode) { mCacheOfModeStatus = mode; if (mPreference != null) { updateState(mPreference); } } }代码分析
03-08
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值