预主活动执行“初始化”

时间:2014-04-23 11:43:52

标签: android

可以通过两种方式提出问题: - 在启动主要活动之前,如何处理变量和数据? - 我如何根据某些逻辑设置主启动器活动?即,在从一组活动中查看活动之前,我应该从偏好中检索数据。这应仅针对第一次使用而不将首选项活动保存在后退按钮堆栈中。

    public static String getProfile(Context context) {
    SharedPreferences mSharedPreferences =    PreferenceManager.getDefaultSharedPreferences(context);
    // String profile = mSharedPreferences.getString("pref_profile_list", "-1");
    String profile = mSharedPreferences.getString("pref_login_list", "-1");
    Log.i(TAG, profile);
    return profile;
}
    void init() {

    String profile = getProfile(this);
    Log.i(TAG, "getProfile " + profile);

    switch (parseInt(profile)){
        case 0: 
            startActivity(new Intent(this, firstActivity.class));
            break;
        case 2: 
            startActivity(new Intent(this, secondActivity.class));
            break;
        default:
            Log.i(TAG, profile);

    }
}

感谢。

4 个答案:

答案 0 :(得分:1)

您可以使用清单文件的android:name="your class name"标记内的<application>

android:name : 为应用程序实现的Application子类的完全限定名称。启动应用程序进程时,将在任何应用程序的组件之前实例化此类。 子类是可选的;大多数应用程序不需要一个。在没有子类的情况下,Android使用基本Application类的实例。

示例:

public class Platform extends Application {

public static String str="";
    @Override
    public void onCreate() {
        super.onCreate();
        str="I am executed first";
    }
}

要在任何其他应用程序的组件之前执行Platform,请将此Platform类添加到项目的清单文件中,如下所示

<application
        android:name="com.example.Platform"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:largeHeap="true"
        android:theme="@android:style/Theme.NoTitleBar.Fullscreen" >
        <activity
            android:name="com.example.HomeActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
  </application>

希望它有所帮助。

参考阅读http://developer.android.com/guide/topics/manifest/application-element.html

答案 1 :(得分:0)

为您的资源初始化

创建一个活动“UILApplication”

并在清单中包括

    **android:name="com.example.UILApplication"**

    android:label="@string/app_name"
    android:theme="@style/Theme.AppCompat.Light.DarkActionBar" >
    <activity
        android:name="com.example.HomePage"
        android:label="@string/app_name"
        >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
 ........

以便它在您的发布活动之前执行..

希望这有助于你

答案 2 :(得分:0)

最好的选择是使用 Splash Screen

启动画面可用于多种用途,包括加载数据,调用服务器,显示应用程序徽标以及应用您想要的任何逻辑。您可以将其视为在MainActivity之前运行并执行预处理的Activity,同时向用户显示进度屏幕。

答案 3 :(得分:0)

您可以通过扩展Application类

创建一个Class

PreAppResources.Java

public class PreAppResources extends Application{

    public static String execute="execute before mail activity"; 
    @Override
    public void onCreate() {
        // TODO Auto-generated method stub
        super.onCreate();

        System.out.println(execute);
    }
}

的AndroidManifest.xml

 <application
        android:allowBackup="true"
         android:name="com.example.listtt.PreAppResources"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
         android:largeHeap="true"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.listtt.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

如果它不起作用请告诉我,我会尽力帮助你。