實現了一個app跳轉到另一個app 的功能android
功能: 打開一個如:網頁時 會彈出多個 瀏覽器供選擇
如今要 加一個本身定義 相似 供選擇 的瀏覽器瀏覽器
MainActivity 和 IntentInvoke 是不一樣的android 工程app
一、在下個頁面 第二個 android 工程的 AndroidManifest.xml功能清單配置 自定義的 應用ide
代碼佈局
//action 裏是MainActivity 裏setAction裏設置的 動做
//category 的DEFAULT -- 默認 的類型
//data -- MainActivity 裏setType 設置的類型
<intent-filter >
<action android:name="android.intent.action.VIEW"/>
<category android:name="android.intent.category.DEFAULT"/>
<data android:mimeType="text/*"/>
</intent-filter>this
-----------------------------------------xml
二、第一個工程裏的 佈局get
代碼it
<LinearLayout 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:orientation="vertical"
>io
<!--打開 自定義的 應用 -->
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="打開閱讀器"
android:textSize="30sp"
android:onClick="openReader"/>
</LinearLayout>
------------------------
三、第二個 工程 裏的界面 佈局
代碼
<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"
tools:context="${relativePackage}.${activityClass}" >
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="" />
</RelativeLayout>
-------------------------
四、第一個android工程 裏 MainActivity 的 代碼
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
//自定義的 東東
public void openReader(View view){
Intent intent = new Intent();
intent.setAction(intent.ACTION_VIEW);
//設置 應用的類型
intent.setType("text/*");
//第一個參數 -- 一個隨便的 常量
//第二個參數 -- 要在自定義的 運用裏 顯示的內容
intent.putExtra(intent.EXTRA_TEXT, "哈哈哈哈哈哈");
startActivity(intent);
}
}
-------------------------------
五、第二個 工程裏 MainActivity
代碼
public class MainActivity extends Activity {private TextView text; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); text = (TextView) this.findViewById(R.id.text); } @Override protected void onStart() { super.onStart(); //接收 自定義 調用方 的 數據 Intent intent = getIntent(); String data = intent.getStringExtra(intent.EXTRA_TEXT); text.setText(data); }}