现在的位置: 首页 > 综合 > 正文

intent详解(二)

2017年06月16日 ⁄ 综合 ⁄ 共 3969字 ⁄ 字号 评论关闭

前言:上篇讲解了有关intent的基础知识,现在通过几个实例讲讲intent这几个属性的具体应用,还有,(题外话)我发现不能一直听《一生所爱》太悲凉,整得我一晚上都没劲头了,心情很低落,看来以后还是少听悲伤的歌为好。

相关链接:

《intent详解(一)》

一、使用包含预定义动作的隐式Intent

效果图:

     初始状态(一个按钮)                      跳转(多个activity符合条件,让用户选择一个)    

   

       选择我们自己写义的一个activity

1、新建应用,在布局文件中,添加一个button

<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="com.example.intenttest1.MainActivity" >

    <Button 
        android:id="@+id/btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="转到action.VIEW"/>

</RelativeLayout>

2、新建一个Activity,命名为:SecondActivity

布局如下:(只是改了一下textview的显示值,其它没动)

<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="com.example.intenttest1.SecondActivity" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="第二个activiy" />

</RelativeLayout>

3、修改AndroidManifest.xml

修改SecondActivity的属性,为其添加系统定义的Action,修改如下:

<activity
    android:name=".SecondActivity"
    android:label="@string/title_activity_second" >
    <intent-filter>
        <action android:name="android.intent.action.VIEW" />
        <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>
</activity>

4、定义隐式intent跳转

在MainActivity中,当点击按钮时实现隐式intent跳转。

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        Button btn = (Button)findViewById(R.id.btn);
        btn.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent = new Intent();
				intent.setAction(Intent.ACTION_VIEW);
				startActivity(intent);
				
			}
		});
    }

}


源代码在文章最底部给出。

二、使用自定义动作的隐式Intent

1、在上例的基础上,更改AndroidManifest.xml,为SecondActivity自定义一个action name

 <activity
     android:name=".SecondActivity"
     android:label="@string/title_activity_second" >
     <intent-filter>
         <action android:name="test_action" />

         <category android:name="android.intent.category.DEFAULT" />
     </intent-filter>
 </activity>

2、隐式Intent跳转

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        Button btn = (Button)findViewById(R.id.btn);
        btn.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent = new Intent();
				intent.setAction("test_action");
				startActivity(intent);
				
			}
		});
    }

}

在intent.setAction()里直接传入自定义的action name,就直接跳转到指定的activity,因为只有这个activity才符合条件,如若有多个activity都有action name="test_action"的话,那就会像上例一样列出列表供用户选择。
源码在文章最底部给出。

效果图:

             初始化状态                                                        点击跳转

   

三、使用Intent打开网页

效果图:

    初始化状态                                                              打开百度网页

   

1、新建工程testIntent3,在主页面加一个Button

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"
    tools:context="com.example.intenttest1.MainActivity" >

    <Button 
        android:id="@+id/btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="打开百度"/>

</RelativeLayout>

2、点击Button打开网页

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        Button btn = (Button)findViewById(R.id.btn);
        btn.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent = new Intent();
				intent.setAction(Intent.ACTION_VIEW);
				intent.setData(Uri.parse("http://www.baidu.com"));
				startActivity(intent);
				
			}
		});
    }

}

使用隐式Intent,利用执行数据来匹配activity,由于执行数据是网页,所以也就只有浏览器才能匹配,所以如果你手机上有不止一个浏览器的话,同样会以列表形式让你选择用哪一个打开。如下图:

所有源码打包一起下载:

地址:http://download.csdn.net/detail/harvic880925/7724673


请大家尊重原创者版权,转载请标明出处:http://blog.csdn.net/harvic880925/article/details/38406421   不胜感激!!!!!

抱歉!评论已关闭.