使用Intent实现Activity的隐式跳转
2016-12-17 16:01
281 查看
相比于显式Intent,隐式Intent 则含蓄了许多,它并不明确指出我们想要启动哪一个活动,而是指定了一系列更为抽象的action 和category 等信息,然后交由系统去分析这个Intent,并帮我们找出合适的活动去启动。
什么叫做合适的活动呢?简单来说就是可以响应我们这个隐式Intent 的活动,那么目前SecondActivity 可以响应什么样的隐式Intent 呢?额,现在好像还什么都响应不了,不过很快就会有了。
第一步 通过在<activity>标签下配置<intent-filter>的内容,可以指定当前活动能够响应的action和category,打开AndroidManifest.xml,添加如下代码:
[java] view plain copy
<activity
android:name="cn.com.qiang.buttonjump.SecondActivity">
<intent-filter >
<action android:name="jump"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</activity>
在<action> 标签中我们指明了当前活动可以响应com.example.activitytest.ACTION_START 这个action,而<category>标签则包含了一些附加信息,更精确地指明了当前的活动能够响应的Intent 中还可能带有的category。只有<action>和<category>中的内容同时能够匹配上Intent 中指定的action 和category 时,这个活动才能响应该Intent。
第二步 在MainAcivity 中调用setAction 及 addCategory 则可以实现隐式跳转
[java] view plain copy
button1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setAction("jump");
intent.addCategory("android.intent.category.DEFAULT");
startActivity(intent);
}
});
重新运行程序,在MainActivity 的界面点击一下按钮,你同样成功启动SecondActivity了。不同的是,这次你是使用了隐式Intent 的方式来启动的,说明我们在<activity>标签下配置的action 和category 的内容已经生效了!
下面是具体代码:
MainActivity 部分:
[java] view plain copy
package cn.com.qiang.buttonjump;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button1 = (Button)findViewById(R.id.button1);
button1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setAction("jump");
intent.addCategory("android.intent.category.DEFAULT");
startActivity(intent);
}
});
}
}
AndroidManfest.xml 部分
[java] view plain copy
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="cn.com.qiang.buttonjump"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme">
<activity
android:name="cn.com.qiang.buttonjump.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="cn.com.qiang.buttonjump.SecondActivity">
<intent-filter >
<action android:name="jump"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</activity>
</application>
</manifest>
什么叫做合适的活动呢?简单来说就是可以响应我们这个隐式Intent 的活动,那么目前SecondActivity 可以响应什么样的隐式Intent 呢?额,现在好像还什么都响应不了,不过很快就会有了。
第一步 通过在<activity>标签下配置<intent-filter>的内容,可以指定当前活动能够响应的action和category,打开AndroidManifest.xml,添加如下代码:
[java] view plain copy
<activity
android:name="cn.com.qiang.buttonjump.SecondActivity">
<intent-filter >
<action android:name="jump"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</activity>
在<action> 标签中我们指明了当前活动可以响应com.example.activitytest.ACTION_START 这个action,而<category>标签则包含了一些附加信息,更精确地指明了当前的活动能够响应的Intent 中还可能带有的category。只有<action>和<category>中的内容同时能够匹配上Intent 中指定的action 和category 时,这个活动才能响应该Intent。
第二步 在MainAcivity 中调用setAction 及 addCategory 则可以实现隐式跳转
[java] view plain copy
button1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setAction("jump");
intent.addCategory("android.intent.category.DEFAULT");
startActivity(intent);
}
});
重新运行程序,在MainActivity 的界面点击一下按钮,你同样成功启动SecondActivity了。不同的是,这次你是使用了隐式Intent 的方式来启动的,说明我们在<activity>标签下配置的action 和category 的内容已经生效了!
下面是具体代码:
MainActivity 部分:
[java] view plain copy
package cn.com.qiang.buttonjump;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button1 = (Button)findViewById(R.id.button1);
button1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setAction("jump");
intent.addCategory("android.intent.category.DEFAULT");
startActivity(intent);
}
});
}
}
AndroidManfest.xml 部分
[java] view plain copy
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="cn.com.qiang.buttonjump"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme">
<activity
android:name="cn.com.qiang.buttonjump.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="cn.com.qiang.buttonjump.SecondActivity">
<intent-filter >
<action android:name="jump"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</activity>
</application>
</manifest>
相关文章推荐
- Android Activity中使用Intent实现页面跳转与参数传递的方法
- 使用Intent对象来实现两个(或多个)Activity之间的跳转,并实现在Activity之间传递参数。
- Android使用Intent隐式实现页面跳转
- 【Android】使用Intent实现Activity之间的跳转(创建一个Activity的过程)
- 使用Intent实现Activity之间传值与跳转
- Activity中使用Intent实现页面跳转与参数的传递
- Activity中使用Intent实现页面跳转与参数的传递(转)
- 使用Intent实现Activity之间传值与跳转
- Activity中使用Intent实现页面跳转与参数的传递
- android中使用intent来实现Activity带数据跳转
- Android使用Intent实现Activity之间的跳转
- Android 使用Intent实现Activity跳转和数据传递
- 使用Intent实现Activity的显式跳转
- Android 使用ContentProvider 实现多个activity灵活显示,实现Intent隐式传值.
- 使用Intent filter来实现不同应用中Activity的相互跳转
- Android使用Intent实现Activity之间的跳转
- 安卓--使用Intent实现Activity之间传值与跳转
- android中使用intent来实现Activity带数据跳转
- 使用Intent来进行隐式activity跳转时Uri的作用
- 使用Intent实现Activity之间传值与跳转(转)