Dis*_*Dev 6 android android-manifest android-intent
我创建了一个我在多个应用程序中共享的库项目.我实现了一个简单的会话到期功能,该功能会在一段时间后将用户踢回登录屏幕.
登录屏幕活动是我的主要活动,因此在清单中它看起来像这样:
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/Theme.Sherlock.Light.DarkActionBar"
android:name="com.blah.application.MyApplication" >
<activity
android:name="com.blah.activity.LoginScreenActivity"
android:label="@string/title_activity_main"
android:screenOrientation="portrait"
android:configChanges="orientation|keyboardHidden"
android:windowSoftInputMode="adjustPan">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
Run Code Online (Sandbox Code Playgroud)
当会话过期时,我想将用户踢回登录屏幕,但我不想硬编码活动的名称,因为它可能会有所不同,具体取决于使用该库的特定应用程序.这是我以前做的事情:
Intent intent = new Intent(context, LoginScreenActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
context.startActivity(intent);
Run Code Online (Sandbox Code Playgroud)
如果应用程序的主要活动与LoginScreenActivity不同,则此操作无效.我不想硬编码"LoginScreenActivity.class",我想以编程方式确定主类的名称,然后将用户引导到该活动...有人可以帮助我吗?
提前致谢!
编辑
我找到了一种方法来完成相同的最终结果,但它绝对不是很好.由于我使用相同的库(字符串,bool等)部署新应用程序需要一定的配置,因此我为strings.xml文件添加了一个字符串,用于定义"主"活动名称的特定应用程序对于该应用程序:
<string name="mainClassName">com.blah.specificapp.activity.SpecificAppLoginScreenActivity</string>
Run Code Online (Sandbox Code Playgroud)
然后我可以按名称获取该类的句柄并将用户重定向到那里:
Class<?> clazz = null;
try
{
clazz = Class.forName(context.getString(R.string.mainClassName));
}
catch (ClassNotFoundException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
if(clazz != null)
{
Intent intent = new Intent(context, clazz);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
context.startActivity(intent);
}
Run Code Online (Sandbox Code Playgroud)
我知道这是一个可怕的解决方案,但它确实有效.就像我说的,无论如何我必须为每个新的应用程序做一些配置,所以添加一个字符串并不是一件很大的事情,它不是很优雅.我很感激任何可以在不使用我的黑客的情况下实现相同目标的建议.
Dav*_*ser 15
您可以要求启动意图从PackageManager,使用:
Intent launchIntent = PackageManager.getLaunchIntentForPackage(context.getPackageName());
Run Code Online (Sandbox Code Playgroud)
这将返回你可以用它来启动"主"活动(我以为是你的"登陆"活动)的意图.加上Intent.FLAG_ACTIVITY_CLEAR_TOP这个,你应该好好去.
| 归档时间: |
|
| 查看次数: |
4232 次 |
| 最近记录: |