intent-filter示例:

<activity
android:name=".CustomActivity"
android:label="@string/title_activity_custom" >
<intent-filter>
<action android:name="android.intent.action.VIEW"></action>
<action android:name="com.example.shad_fnst.intentfilterdemo.LAUNCH"></action>
<category android:name="android.intent.category.DEFAULT"></category>
<data android:scheme="http"></data>
</intent-filter>
</activity>
CustomActivity可以过滤接收到action是android.intent.action.VIEW或com.example.shad_fnst.intentfilterdemo.LAUNCH,以及data是http的Intent,
并作出响应。系统中的其他APP也可以过滤接收到MainActivity中的Intent,并作出响应,例如浏览器。
 package com.example.shad_fnst.intentfilterdemo;

 import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button; public class MainActivity extends Activity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); Button startBrowser_a = (Button) findViewById(R.id.start_browser_a);
startBrowser_a.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(android.content.Intent.ACTION_VIEW,
Uri.parse("http://www.baidu.com"));
startActivity(intent);
}
}); Button startBrowser_b = (Button) findViewById(R.id.start_browser_b);
startBrowser_b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent("com.example.shad_fnst.intentfilterdemo.LAUNCH",
Uri.parse("http://www.baidu.com"));
startActivity(intent);
}
}); Button startBrowser_c = (Button) findViewById(R.id.start_browser_c);
startBrowser_c.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent("com.example.shad_fnst.intentfilterdemo.LAUNCH",
Uri.parse("https://www.baidu.com"));
startActivity(intent);
}
});
} @Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
} @Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId(); //noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
} return super.onOptionsItemSelected(item);
}
}

MainActivity.java

 package com.example.shad_fnst.intentfilterdemo;

 import android.app.Activity;
import android.net.Uri;
import android.os.Bundle;
import android.widget.TextView; public class CustomActivity extends Activity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_custom); TextView label = (TextView) findViewById(R.id.show_data);
Uri uri = getIntent().getData();
label.setText(uri.toString());
}
}

CustomActivity.java

 <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.shad_fnst.intentfilterdemo" > <application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".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=".CustomActivity"
android:label="@string/title_activity_custom" >
<intent-filter>
<action android:name="android.intent.action.VIEW"></action>
<action android:name="com.example.shad_fnst.intentfilterdemo.LAUNCH"></action>
<category android:name="android.intent.category.DEFAULT"></category>
<data android:scheme="http"></data>
</intent-filter>
</activity>
</application> </manifest>

AndroidManifest.xml

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"
android:orientation="vertical"> <Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/start_browser_a"
android:text="@string/start_browser_a"/> <Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/start_browser_b"
android:text="@string/start_browser_b"/> <Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/start_browser_c"
android:text="@string/start_browser_c"/> </LinearLayout>

activity_main.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" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="com.example.shad_fnst.intentfilterdemo.CustomActivity"> <TextView
android:layout_width="fill_parent"
android:layout_height="400dp"
android:id="@+id/show_data"/> </RelativeLayout>

activity_custom.xml

 <resources>
<string name="app_name">IntentFilterDemo</string> <string name="hello_world">Hello world!</string>
<string name="action_settings">Settings</string>
<string name="start_browser_a">Start Browser with VIEW action</string>
<string name="start_browser_b">Start Browser with LAUNCH action</string>
<string name="start_browser_c">Exception Condition</string>
<string name="title_activity_custom">CustomActivity</string>
</resources>

strings.xml

IntentFilterDemo的更多相关文章

随机推荐

  1. 可配置多功能门 SN74LVC1G57, 1G58, 1G97, 1G98, 1G99

    Configurable Multiple-Function Gate  SN74LVC1G57 SN74LVC1G58 SN74LVC1G97 SN74LVC1G98 SN74LVC1G99

  2. 如何从iTunes Connect中提款呢?

    最近在AppStore有点小小小收入,但如何从iTunes Connect中提款呢? (Payments and Financial Reports) 网上查了下,发现有种说法:“只要账号余额达到15 ...

  3. Linux磁盘及文件系统管理 4---- Linux文件系统挂载管理

    1 挂载操作 1 磁盘或者分区需要创建好文件系统后,需要挂载到一个目录才能够使用 2 windows或者是Mac会自动的挂载文件系统,一旦创建好文件系统后会自动的挂载 3 对于Linux来说我们必须要 ...

  4. Java和C++中多态的实现方式

    多态是面向对象的最主要的特性之一,是一种方法的动态绑定,实现运行时的类型决定对象的行为.多态的表现形式是父类指针或引用指向子类对象,在这个指针上调用的方法使用子类的实现版本.多态是IOC.模板模式实现 ...

  5. 下一个系列学习列表Spring.net+NHibernate+MVC

    开源框架完美组合之Spring.NET + NHibernate + ASP.NET MVC + jQuery + easyUI 中英文双语言小型企业网站Demo 刘冬.NET 2011-08-19 ...

  6. mysql常用备注

    一:One Table  have only one Auto_Increment that column is must to be Primary key. (自增加的字段必须是主键且是数字类型) ...

  7. iOS开发,让数据更安全的几个加密方式

    任何应用的开发中安全都是重中之重,在信息交互异常活跃的现在,信息加密技术显得尤为重要.在app应用开发中,我们需要对应用中的多项数据进行加密处理,从而来保证应用上线后的安全性,给用户一个安全保障.这篇 ...

  8. PowerDesigner 之 PDM建模

    很多码农使用Powerdesigner用来创建数据库,下面来具体记录一下使用该工具创建的过程. 1.首先运行PowerDesigner程序,进入主界面,如下图: 2.菜单栏依次"File→N ...

  9. HttpSolrServer-采用静态工厂方法,创建HttpSolrServer单实例

    HttpSolrServer线程安全,如果使用下面构造器,必须对所有的请求重用相同的实例.如果实例在运行中创建的,它可能会导致连接泄漏.推荐的做法就是保持每个solr服务url的HttpSolrSer ...

  10. [Arduino] 基于Xbee Pro和网络技术的智能公交系统设计

    转自:http://www.21ic.com/app/rf/201112/99474.htm 引言 公共交通具有个体交通无法比拟的强大优势,优先发展城市公共交通系统是解决大.中城市交通问题的最佳途径. ...