Android 安卓:在安卓4.0中启动主屏幕

Android 安卓:在安卓4.0中启动主屏幕,android,homescreen,Android,Homescreen,我正在尝试从服务启动主屏幕。。我使用了以下代码 Intent startMain = new Intent(Intent.ACTION_MAIN); startMain.addCategory(Intent.CATEGORY_LAUNCHER); startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); startActivity(startMain); 它在安卓

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

           Intent startMain = new Intent(Intent.ACTION_MAIN);
           startMain.addCategory(Intent.CATEGORY_LAUNCHER);
           startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
           startActivity(startMain);
它在安卓2.3中运行良好,但在4.0中却不起作用

在4.0中,它显示一个列表来选择默认屏幕

我需要与2.3中相同的效果


提前感谢

您可以试试这个添加,看看它是否有效

而不是这个类别,
startMain.addCategory(Intent.Category\u启动器)

试试这个

startMain.addCategory(Intent.CATEGORY_HOME);

主屏幕可以从您的清单中触发。 开始您的活动,您希望拥有如下主屏幕:

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

}

如果要在毫秒后切换到下一个屏幕,请创建如下活动:

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

}
public class HomeScreenActivity扩展活动{
受保护布尔值_active=true;
protected int _splashTime=3000;//显示启动屏幕的时间(毫秒)
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setTitle(“您的活动标题”);
requestWindowFeature(窗口。功能\u无\u标题);
setContentView(R.layout.activity\u主屏幕);
//用于显示主屏幕的线程
线程homeTread=新线程(){
@凌驾
公开募捐{
试一试{
int=0;
while(_active&&(waiting<_splashTime)){
睡眠(100);
如果(_活动){
平均值+=100;
}
}
}捕捉(中断异常e){
//无所事事
}最后{
完成();
startActivity(新意图(“com.example.SecondViewActivity”);
}
}
};
homeTread.start();
}
@凌驾
公共布尔onTouchEvent(运动事件){
if(event.getAction()==MotionEvent.ACTION\u向下){
_主动=假;
}
返回true;
}
}
谢谢你,先生,它起作用了:)我几分钟后核对一下这个答案