intent详解(二)

来源:互联网 发布:粒子群优化算法的功能 编辑:程序博客网 时间:2024/05/10 06:16

转自:http://blog.csdn.net/harvic880925/article/details/38406421

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

效果图:

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

   

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


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

[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     tools:context="com.example.intenttest1.MainActivity" >  
  6.   
  7.     <Button   
  8.         android:id="@+id/btn"  
  9.         android:layout_width="wrap_content"  
  10.         android:layout_height="wrap_content"  
  11.         android:text="转到action.VIEW"/>  
  12.   
  13. </RelativeLayout>  

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

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

[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     tools:context="com.example.intenttest1.SecondActivity" >  
  6.   
  7.     <TextView  
  8.         android:layout_width="wrap_content"  
  9.         android:layout_height="wrap_content"  
  10.         android:text="第二个activiy" />  
  11.   
  12. </RelativeLayout>  

3、修改AndroidManifest.xml

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

[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. <activity  
  2.     android:name=".SecondActivity"  
  3.     android:label="@string/title_activity_second" >  
  4.     <intent-filter>  
  5.         <action android:name="android.intent.action.VIEW" />  
  6.         <category android:name="android.intent.category.DEFAULT" />  
  7.     </intent-filter>  
  8. </activity>  

4、定义隐式intent跳转

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

[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. public class MainActivity extends Activity {  
  2.   
  3.     @Override  
  4.     protected void onCreate(Bundle savedInstanceState) {  
  5.         super.onCreate(savedInstanceState);  
  6.         setContentView(R.layout.activity_main);  
  7.           
  8.         Button btn = (Button)findViewById(R.id.btn);  
  9.         btn.setOnClickListener(new View.OnClickListener() {  
  10.               
  11.             @Override  
  12.             public void onClick(View v) {  
  13.                 // TODO Auto-generated method stub  
  14.                 Intent intent = new Intent();  
  15.                 intent.setAction(Intent.ACTION_VIEW);  
  16.                 startActivity(intent);  
  17.                   
  18.             }  
  19.         });  
  20.     }  
  21.   
  22. }  

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


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

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

[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. <activity  
  2.     android:name=".SecondActivity"  
  3.     android:label="@string/title_activity_second" >  
  4.     <intent-filter>  
  5.         <action android:name="test_action" />  
  6.   
  7.         <category android:name="android.intent.category.DEFAULT" />  
  8.     </intent-filter>  
  9. </activity>  
2、隐式Intent跳转
[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. public class MainActivity extends Activity {  
  2.   
  3.     @Override  
  4.     protected void onCreate(Bundle savedInstanceState) {  
  5.         super.onCreate(savedInstanceState);  
  6.         setContentView(R.layout.activity_main);  
  7.           
  8.         Button btn = (Button)findViewById(R.id.btn);  
  9.         btn.setOnClickListener(new View.OnClickListener() {  
  10.               
  11.             @Override  
  12.             public void onClick(View v) {  
  13.                 // TODO Auto-generated method stub  
  14.                 Intent intent = new Intent();  
  15.                 intent.setAction("test_action");  
  16.                 startActivity(intent);  
  17.                   
  18.             }  
  19.         });  
  20.     }  
  21.   
  22. }  
在intent.setAction()里直接传入自定义的action name,就直接跳转到指定的activity,因为只有这个activity才符合条件,如若有多个activity都有action name="test_action"的话,那就会像上例一样列出列表供用户选择。
源码在文章最底部给出。

效果图:

             初始化状态                                                        点击跳转

   


三、使用Intent打开网页

效果图:

    初始化状态                                                              打开百度网页

   

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

XML代码 :

[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     tools:context="com.example.intenttest1.MainActivity" >  
  6.   
  7.     <Button   
  8.         android:id="@+id/btn"  
  9.         android:layout_width="wrap_content"  
  10.         android:layout_height="wrap_content"  
  11.         android:text="打开百度"/>  
  12.   
  13. </RelativeLayout>  
2、点击Button打开网页
[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. public class MainActivity extends Activity {  
  2.   
  3.     @Override  
  4.     protected void onCreate(Bundle savedInstanceState) {  
  5.         super.onCreate(savedInstanceState);  
  6.         setContentView(R.layout.activity_main);  
  7.           
  8.         Button btn = (Button)findViewById(R.id.btn);  
  9.         btn.setOnClickListener(new View.OnClickListener() {  
  10.               
  11.             @Override  
  12.             public void onClick(View v) {  
  13.                 // TODO Auto-generated method stub  
  14.                 Intent intent = new Intent();  
  15.                 intent.setAction(Intent.ACTION_VIEW);  
  16.                 intent.setData(Uri.parse("http://www.baidu.com"));  
  17.                 startActivity(intent);  
  18.                   
  19.             }  
  20.         });  
  21.     }  
  22.   
  23. }  
使用隐式Intent,利用执行数据来匹配activity,由于执行数据是网页,所以也就只有浏览器才能匹配,所以如果你手机上有不止一个浏览器的话,同样会以列表形式让你选择用哪一个打开。如下图:



所有源码打包一起下载:

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


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



0 0
原创粉丝点击