设置与桌面相同的action和category
安卓系统桌面也是一个activity,安卓桌面的action和category是:
<activity
android:name="com.sxt.day04_07_twoaction.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />项目入口acticity <category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
给自己的acticity设置的和桌面一样,会弹出窗口选择是启动安卓系统桌面的activity还是我们自己的activity。
main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"> <Button
android:id="@+id/btnHome"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="启动桌面Activity" /> </LinearLayout>
main.java
package com.sxt.day04_07_twoaction; import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener; public class MainActivity extends Activity implements OnClickListener{ @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setListener();
} private void setListener() {
findViewById(R.id.btnHome).setOnClickListener(this);
} @Override
public void onClick(View v) {
Intent intent=new Intent();
switch (v.getId()) {
case R.id.btnHome:
intent.addCategory(Intent.CATEGORY_HOME);
intent.setAction(Intent.ACTION_MAIN);//这里用常量,也可以写字符串
break;
}
startActivity(intent);
}
}
系统描述文件:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.sxt.day04_07_twoaction"
android:versionCode="1"
android:versionName="1.0" > <uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" /> <application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.sxt.day04_07_twoaction.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>
<activity
android:name="com.sxt.day04_07_twoaction.SecondActivity" //要启动的activity的java类
android:label="@string/title_activity_second" >
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.DEFAULT"/>与桌面activity相同的时候也要加上这个,然后再设置一个category
<category android:name="android.intent.category.HOME"/>
</intent-filter>
</activity>
</application> </manifest>
SecondActivity.java
package com.sxt.day04_07_twoaction; import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu; public class SecondActivity extends Activity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second); //加载xml文件
Log.i("main","SecondActivity");
} }
SecondActivity.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".SecondActivity" > <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" /> </RelativeLayout>
效果: