Get instance of current android.intent.category.LA

2019-04-09 05:36发布

I have created a library project that I am sharing across several apps. I implemented a simple session expiration feature that will kick the user back to the login screen after a certain time period.

The login screen activity is my main activity, so in the manifest it looks like this:

<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>

When the session is expired, I want to kick the user back to the login screen, but I don't want to hardcode the name of the activity because it might be different depending on the specific app that is using the library. Here's what I was doing before:

Intent intent = new Intent(context, LoginScreenActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
context.startActivity(intent);

This doesn't work if the app's main activity is something different than LoginScreenActivity. I don't want to hardcode "LoginScreenActivity.class", I want to programmatically determine the name of the main class and then direct the user to that activity...can someone help me out?

Thanks in advance!

EDIT

I found a way to accomplish the same end result, but it's definitely not great. Since there is a certain amount of configuration necessary for me to deploy a new app using the same library (strings, bools, etc), I added a string to the strings.xml file for the specific app that defines the "main" activity name for that app:

<string name="mainClassName">com.blah.specificapp.activity.SpecificAppLoginScreenActivity</string>

Then I can get a handle on that class by name and redirect the user there:

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);
}

I know this is a god-awful solution, but it works. Like I said, there is a certain amount of configuration I have to do for each new app anyway, so adding one more string isn't a huge deal it's just not very elegant. I'd appreciate any suggestions that can accomplish the same goal without using my hack.

2条回答
Luminary・发光体
2楼-- · 2019-04-09 06:17

How about using a mime-type on your intent filter.

    <activity android:name=".LoginActivity"
              android:exported="true" android:launchMode="singleTop" android:label="@string/MT">
        <intent-filter>
            <category android:name="android.intent.category.DEFAULT"/>
            <action android:name="com.foo.ACTION_LOGIN" />
            <data android:mimeType="application/x.foo.com.mobile.login" /> 
     </intent-filter>
    </activity>

And launch the activity as follows:

Intent intent = new Intent();
intent.setAction("com.foo.ACTION_LOGIN");
intent.setType("application/x.foo.com.mobile.login");
startActivity(myIntent);

Thus the intent will be serviced by whatever activity is registered with this action/mime-type pair. I am not certain, but I think if that activity is hosted in the same app, it may be chosen first.

查看更多
时光不老,我们不散
3楼-- · 2019-04-09 06:37

You can request a launch Intent from the PackageManager, using:

Intent launchIntent = PackageManager.getLaunchIntentForPackage(context.getPackageName());

This will return an Intent that you can use to launch the "main" activity (which I assume is your "login" activity). Just add Intent.FLAG_ACTIVITY_CLEAR_TOP to this and you should be good to go.

查看更多
登录 后发表回答