织梦网站防黑怎么做,网站 集约化建设管理举措,巫山做网站那家好,北京汉邦未来网站建设有限公司上一篇我们讨论了活动的显示跳转#xff0c;现在来学习活动的隐式跳转 相比于显式Intent#xff0c;隐式Intent 则含蓄了许多#xff0c;它并不明确指出我们想要启动哪一个活动#xff0c;而是指定了一系列更为抽象的action 和category 等信息#xff0c;然后交由系统去分… 上一篇我们讨论了活动的显示跳转现在来学习活动的隐式跳转 相比于显式Intent隐式Intent 则含蓄了许多它并不明确指出我们想要启动哪一个活动而是指定了一系列更为抽象的action 和category 等信息然后交由系统去分析这个Intent并帮我们找出合适的活动去启动。 什么叫做合适的活动呢简单来说就是可以响应我们这个隐式Intent 的活动那么目前SecondActivity 可以响应什么样的隐式Intent 呢额现在好像还什么都响应不了不过很快就会有了。 第一步 通过在activity标签下配置intent-filter的内容可以指定当前活动能够响应的action和category打开AndroidManifest.xml添加如下代码 [java] view plaincopy activity android:namecn.com.qiang.buttonjump.SecondActivity intent-filter action android:namejump/ category android:nameandroid.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 plaincopy 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 plaincopy 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 plaincopy ?xml version1.0 encodingutf-8? manifest xmlns:androidhttp://schemas.android.com/apk/res/android packagecn.com.qiang.buttonjump android:versionCode1 android:versionName1.0 uses-sdk android:minSdkVersion8 android:targetSdkVersion17 / application android:allowBackuptrue android:icondrawable/ic_launcher android:labelstring/app_name android:themestyle/AppTheme activity android:namecn.com.qiang.buttonjump.MainActivity android:labelstring/app_name intent-filter action android:nameandroid.intent.action.MAIN / category android:nameandroid.intent.category.LAUNCHER / /intent-filter /activity activity android:namecn.com.qiang.buttonjump.SecondActivity intent-filter action android:namejump/ category android:nameandroid.intent.category.DEFAULT/ /intent-filter /activity /application /manifest