Asynch启动活动不会出现

时间:2017-05-24 13:40:20

标签: java android android-asynctask

我将SplashActivity设置为第一个在应用程序启动时注册的。然后在我的AsyncTask的postExecute方法中,我启动了一个新的意图,将您带到MainActivity。为什么这不起作用?我不会得到一个例外。该应用程序只是打开我的MainAcitvity ...也许我需要清理重建项目几次......

清单:

    <activity
            android:name=".activity.SplashActivity"
            android:configChanges="keyboardHidden|orientation|screenSize"
            android:label="@string/app_name"
            android:launchMode="singleTask"
            android:screenOrientation="portrait" >
            <intent-filter>
                <action android:name="android.intent.action.SEND" />
                <category android:name="android.intent.category.DEFAULT" />
                <data android:mimeType="image/*" />
            </intent-filter>
        </activity>
    <activity
        android:name=".activity.MainActivity"
        android:configChanges="keyboardHidden|orientation|screenSize"
        android:label="@string/app_name"
        android:launchMode="singleTask"
        android:screenOrientation="portrait" >
    </activity>

代码:

public class SplashActivity extends Activity {

    private static final int SPLASH_SHOW_TIME = 40800;

    @Override protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.splash);
        new BackgroundSplashTask().execute();
    }

    private class BackgroundSplashTask extends AsyncTask<Void, Void, Void> {

        @Override
        protected void onPreExecute(){
            super.onPreExecute();
        }

        @Override
        protected Void doInBackground(Void... arg0) {
            try {
                Thread.sleep(SPLASH_SHOW_TIME);
            } catch (InterruptedException e) {
                e.printStackTrace();
            } return null;
        }

        @Override
        protected void onPostExecute(Void result) {
            super.onPostExecute(result);
            Intent i = new Intent(SplashActivity.this, MainActivity.class);
            startActivity(i); finish();
        }

    }

}

2 个答案:

答案 0 :(得分:1)

我猜您必须将这两行添加到您的清单SplashActivity

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

希望这会有所帮助。

答案 1 :(得分:1)

AndroidManifest.xml文件集 SplashActivity 中,如下所示:

<activity android:name=".activity.SplashActivity"
        android:configChanges="keyboardHidden|orientation|screenSize"
        android:label="@string/app_name"
        android:launchMode="singleTask"
        android:screenOrientation="portrait" >>
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />

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

然后在您的AsycTask中,只需从super.onPostExecute(result);方法中删除onPostExecute(Void result)代码行。同时删除onPreExecute()实现,在您的情况下不需要。

private class BackgroundSplashTask extends AsyncTask<Void, Void, Void> {

    @Override
    protected Void doInBackground(Void... arg0) {
        try {
            Thread.sleep(SPLASH_SHOW_TIME);
        } catch (InterruptedException e) {
            e.printStackTrace();
        } return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        Intent i = new Intent(SplashActivity.this, MainActivity.class);
        startActivity(i); finish();
    }

}