一、使用包含预定义动作的隐式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>
- 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打开网页
效果图:
初始化状态 打开百度网页
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>
- 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);
- }
- });
- }
- }
所有源码打包一起下载:
地址:http://download.youkuaiyun.com/detail/harvic880925/7724673
请大家尊重原创者版权,转载请标明出处:http://blog.youkuaiyun.com/harvic880925/article/details/38406421 不胜感激!!!!!
460

被折叠的 条评论
为什么被折叠?



