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的更多相关文章
随机推荐
- cocos2dx json数据解析
转自:http://blog.csdn.net/wangbin_jxust/article/details/9707873 cocos2dx本身没有json解析类库,我们这里引入libjson进行解析 ...
- Keil MDK AGDI Drivers, ULink, JLink, ST-Link, NuLink, JTAGjet
AGDI Drivers AGDI is an Application Program Interface (API) third-party developers can use to create ...
- Architecture of Device I/O Drivers, Device Driver Design
http://www.kalinskyassociates.com/Wpaper4.html Architecture of Device I/O Drivers Many embedded syst ...
- Linux(Centos)配置OpenSSH无密码登陆<转>
最近在搭建Hadoop环境需要设置无密码登陆,所谓无密码登陆其实是指通过证书认证的方式登陆,使用一种被称为"公私钥"认证的方式来进行ssh登录. " 公私钥" ...
- C++ 方法隐藏
1.过载:一个类中,方法名相同,形参表不同的方法. 2.重写:子类与父类的virtual方法,方法名,形参表相同. 3.考虑下面的情况,子类与父类方法名相同. 父类是virtual方法 ...
- C#类的成员初始化顺序
首先我们来看看引用类型的成员初始化过程 我们来看一个例子吧 class Program { static void Main(string[] args) { Driv ...
- Android微信SDK API 调用教程【转】
原文:http://blog.csdn.net/worker90/article/details/8211451 最近一直在调用微信的API,却发现一直调用不成功,纠结了好久,各方面找教程,找官方,官 ...
- iOS开发——实用技术OC篇&简单抽屉效果的实现
简单抽屉效果的实现 就目前大部分App来说基本上都有关于抽屉效果的实现,比如QQ/微信等.所以,今天我们就来简单的实现一下.当然如果你想你的效果更好或者是封装成一个到哪里都能用的工具类,那就还需要下一 ...
- Cocos2D-X2.2.3学习笔记9(处理重力感应事件,移植到Android加入两次返回退出游戏效果)
这节我们来学习Cocos2d-x的最后一节.怎样处理重力感应事件.移植到Android后加入再按一次返回键退出游戏等.我这里用的Android.IOS不会也没设备呃 效果图不好弄,由于是要移植到真机上 ...
- XCODE4.6从零开始添加视图
转自:http://www.cnblogs.com/luoxs/archive/2012/09/23/2698995.html 对于很多初学者来说,肯定希望自己尝试不用傻瓜的“Single View ...