IntentFilterDemo
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的更多相关文章
随机推荐
- RHEL 5.4下部署LVS(DR)+keepalived实现高性能高可用负载均衡
原文地址:http://www.cnblogs.com/mchina/archive/2012/05/23/2514728.html 一.简介 LVS是Linux Virtual Server的简写, ...
- 新唐M0 ISP下载要点
http://blog.csdn.net/rejoice818/article/details/7736029 一.注意:官方光盘内“Software Utilities”目录下,可找到ICP或ISP ...
- java建立二叉树,递归/非递归先序遍历,递归/非递归中序遍历,层次遍历
import java.util.LinkedList; import java.util.Scanner; import java.util.Stack; //structure of binary ...
- Linux makefile 教程 很具体,且易懂
近期在学习Linux下的C编程,买了一本叫<Linux环境下的C编程指南>读到makefile就越看越迷糊,可能是我的理解能不行. 于是google到了下面这篇文章.通俗易懂.然后把它贴出 ...
- GlusterFS创建volume失败的解决方法(* or a prefix of it is already part of a volume)
问题描写叙述: 之前已经创建了一个replicated的volume gv0,replica=2,两个文件夹为:/test/data1和/test/data2,之后发现这两个文件夹不太合适,想在/te ...
- 从Project 2007导出WBS图表到Visio 2007
微软官网讲:在 Microsoft Office Project 2007 中,Visio WBS 图表向导已被可视报表代替.您可以使用 Microsoft Office Visio Professi ...
- C#MongoDB 分页查询的方法及性能
传统的SQL分页 传统的sql分页,所有的方案几乎是绕不开row_number的,对于需要各种排序,复杂查询的场景,row_number就是杀手锏.另外,针对现在的web很流行的poll/push加载 ...
- dsPIC33EP 高速PWM模块初始化设置及应用
//文件 p33pwm6.h #ifndef _P33PWM6_H_ #define _P33PWM6_H_ //#include "p33pwm6.h" #define FSYN ...
- EasyUI基础入门之Pagination(分页)
前言 对于一些企业级的应用来说(非站点),页面上最为基本的内容也就是表格和form了.对于类似于ERP这类系统来说数据记录比較大,前端表格展示的时候必需得实现分页功能了.恰巧EasyUI就提供了分页组 ...
- Mac OS系统
Mac OS系统 目录 概述 Mac OS系统常用操作 概述 Mac OS系统常用操作 显示或隐藏文件 在终端输入:defaults write com.apple.finder AppleShowA ...