Android开发入门之Intent深入解剖

来源:互联网 发布:gbdt python实现 编辑:程序博客网 时间:2024/05/17 05:18

第一步:新建一个Android工程命名为Intent目录结构如下图:


第二步:修改activity_main.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=".MainActivity" >    <TextView        android:id="@+id/tv_hello"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="@string/hello_world" />    <Button        android:text="@string/start_activity"        android:onClick="startActivity"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_below="@id/tv_hello" /></RelativeLayout>

strings.xml:

<?xml version="1.0" encoding="utf-8"?><resources>    <string name="app_name">Intent</string>    <string name="action_settings">Settings</string>    <string name="hello_world">Hello world!</string>    <string name="other_activity">我是Other Activity</string>    <string name="start_activity">打开OtherActivity</string></resources>


第三步:创建OtherActivity类 ,该类继承Activity:

第四步:编写MianActivity类:

package cn.leigo.intent;import android.app.Activity;import android.content.Intent;import android.net.Uri;import android.os.Bundle;import android.view.View;public class MainActivity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);}public void startActivity(View v) {// (在没设数据参数的情况下)只要Intent中的Action和Category都出现在intent-filter中,就能与之匹配,否则匹配失败Intent intent = new Intent(); // 隐式意图激活Activityintent.setAction("cn.leigo.intent.action");intent.addCategory("cn.leigo.intent.category");//intent.setData(Uri.parse("http://blog.csdn.net/u011272454"));//intent.setType("image/*");intent.setDataAndType(Uri.parse("http://blog.csdn.net/u011272454"), "image/*");startActivity(intent); // 方法内部为Intent添加了android.intent.category.DEFAULT类别}}