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. intval()和(int)转换使用与区别

    <?php echo "<br/>数值强制转换:"; $string="2a"; $string1=intval($string); echo ...

  2. uva 11324 The Largest Clique(强连通分量缩点+DAG动态规划)

    http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=25&page=sh ...

  3. 提升jQuery开发技能的教程

    iPhone-like Sliding Headers Simple jQuery Spy Effect Simple use of Event Delegation Adding Keyboard ...

  4. SAP实施方法与过程——ASAP

    ASAP是SAP公司为使R/3项目的实施更简单.更有效的一套完整的快速实施方法.ASAP优化了在实施过程中对时间.质量和资源的有效使用等方面的控制.它是一个包括了使得项目实施得以成功所有基本要素的完整 ...

  5. C++在堆上申请和释放内存 - new & delete

    // 动态申请内存, 指向一个未初始化的整型 int *pi = new int; // pi指向一个整型值,初始化为0 int *pi = new int(); // value of i is 1 ...

  6. UML精粹学习 - 订单类结构图

    Order Class Diagram of Martin Fowler's UML Distilled

  7. Codeforces Round #336 (Div. 2)A. Saitama Destroys Hotel 水题

    A. Saitama Destroys Hotel 题目连接: http://www.codeforces.com/contest/608/problem/A Description Saitama ...

  8. 【JavaScript】谈谈Google Polymer以及Web UI框架的未来

    摘要:开发者Axel Rauschmayer在自己的博客上详解了Google Polymer的设计理念与组成架构,深得Polymer开发者的认同.他认为Polymer这样高互操作性的设计才应该是Web ...

  9. 和iPhone有关的视图控制器:UIViewController、UITabBarController、UINavigationController及其混合用法

    iPhone中的view视图是应用程序对于数据最直观.最直接的呈现方式,如下是我在学习了iPhone中的视图控制器以及由其衍生的特殊子类的总结,希望对那些初学者有所帮助: UIViewControll ...

  10. oc-21-class对象

    /** 什么是类对象(Class对象)? 类在内存当中也是以对象形式进行存储的. 1.类对象的类型:Class类型 2.如何创建类对象: 1)Class 类对象名 = [类名 class]; 2)类名 ...