Android显式意图、隐式意图、意图过滤器(intent-filter)、意图间传值
显式意图:调用Intent.setComponent()、Intent.setClass()、Intent.setClassName()方法明确指定了组件名的Intent为显式意图,显式意图明确指定了Intent应该传递给哪个组件。
隐式意图:没有明确指定组件名的Intent为隐式意图。 Android系统会根据隐式意图中设置的动作(action)、类别(category)、数据(URI和数据类型)找到最合适的组件来处理这个意图。
MainActivity.java
package cn.android.intent; import java.util.Date; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.KeyEvent; import android.view.View; import android.widget.TextView; public class NewActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.other); Intent intent = getIntent(); //Log.i("NewActivity", intent.getData().getPath()); String title = intent.getStringExtra("title"); double since = intent.getDoubleExtra("since", 1995.5); TextView textView = (TextView) this.findViewById(R.id.textView); textView.setText("语言:" + title + ",发布时间:" + since + "。"); } public void closeActivity(View v) { Intent intent = new Intent(); intent.putExtra("result", new Date().toString()); setResult(501, intent); this.finish(); } @Override public boolean onKeyDown(int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_BACK) { closeActivity(null); } return true; } }
NewActivity.java
package cn.android.intent; import java.util.Date; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.KeyEvent; import android.view.View; import android.widget.TextView; public class NewActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.other); Intent intent = getIntent(); //Log.i("NewActivity", intent.getData().getPath()); String title = intent.getStringExtra("title"); double since = intent.getDoubleExtra("since", 1995.5); TextView textView = (TextView) this.findViewById(R.id.textView); textView.setText("语言:" + title + ",发布时间:" + since + "。"); } public void closeActivity(View v) { Intent intent = new Intent(); intent.putExtra("result", new Date().toString()); setResult(501, intent); this.finish(); } @Override public boolean onKeyDown(int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_BACK) { closeActivity(null); } return true; } }
main.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent"> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" android:id="@+id/textView" /> <LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="horizontal" android:gravity="center_horizontal" android:layout_below="@id/textView"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/button_display" android:id="@+id/buttonDisplay" android:onClick="openActivityDisplay" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/button_hidden" android:onClick="openActivityHidden" /> </LinearLayout> </RelativeLayout>
other.xml
<?xml version="1.0" encoding="utf-8"?> <TableLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent"> <TableRow> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/newActivity" android:id="@+id/textView" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/button_close" android:onClick="closeActivity" /> </TableRow> </TableLayout>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="cn.android.intent" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" /> <application android:icon="@drawable/ic_launcher" android:label="@string/app_name" > <activity android:label="@string/app_name" android:name=".MainActivity" > <intent-filter > <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name=".NewActivity" android:label="@string/newActivity"> <intent-filter> <action android:name="cn.java.action.android" /> <action android:name="cn.java.action.windowsphone" /> <category android:name="android.intent.category.DEFAULT" /> <category android:name="cn.java.category.intent" /> <category android:name="cn.java.category.action" /> <data android:scheme="intent" android:host="www.android.com" /> <data android:mimeType="image/*" /> </intent-filter> </activity> </application> </manifest>
strings.xml
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="hello">Hello World, MainActivity!</string> <string name="newActivity">New Activity!</string> <string name="app_name">Android意图</string> <string name="button_display">打开显示意图</string> <string name="button_hidden">打开隐式意图</string> <string name="button_close">关闭窗口</string> </resources>
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。