Android:在Android 4.0中启动主屏幕

时间:2013-01-22 09:58:40

标签: android homescreen

我正在尝试从服务启动主屏幕..我使用以下代码

           Intent startMain = new Intent(Intent.ACTION_MAIN);
           startMain.addCategory(Intent.CATEGORY_LAUNCHER);
           startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
           startActivity(startMain);

它在Android 2.3中运行良好,但在4.0中没有运行

在4.0中,它显示了一个列表,用于选择默认屏幕。

我需要与2.3

中相同的效果

提前致谢

2 个答案:

答案 0 :(得分:0)

你可以尝试这个添加看看它是否有效。

而不是此类别,startMain.addCategory(Intent.CATEGORY_LAUNCHER);

试试这个,

startMain.addCategory(Intent.CATEGORY_HOME);

答案 1 :(得分:0)

可以从清单触发主屏幕。 开始你的活动,你想拥有如下主屏幕:

<activity
        android:name="com.example.HomeScreenActivity"
        android:screenOrientation="portrait"
        android:configChanges="orientation|keyboardHidden|screenSize"
        android:label="@string/title_activity_home_screen"
        android:theme="@style/FullscreenTheme" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

如果您想在一段毫秒之后切换到下一个屏幕,请创建您的活动,如下所示:

public class HomeScreenActivity extends Activity {

protected boolean _active = true;
protected int _splashTime = 3000; // time to display the splash screen in ms

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setTitle("Your Activity Title");
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.activity_home_screen);

    // thread for displaying the HomeScreen
    Thread homeTread = new Thread() {
        @Override
        public void run() {
            try {
                int waited = 0;
                while(_active && (waited < _splashTime)) {
                    sleep(100);
                    if(_active) {
                        waited += 100;
                    }
                }
            } catch(InterruptedException e) {
                // do nothing
            } finally {
                finish();
                startActivity(new Intent("com.example.SecondViewActivity"));
            }
        }
    };
    homeTread.start();

}

@Override
public boolean onTouchEvent(MotionEvent event) {
    if (event.getAction() == MotionEvent.ACTION_DOWN) {
        _active = false;
    }
    return true;
}

}
相关问题