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的更多相关文章
随机推荐
- Trie树学习2
数组实现的Trie树 字符容量有限,能够使用链表实现更为大容量的Trie #include <iostream> #include <cstdio> #include < ...
- ARM&Linux 下驱动开发第二节
驱动文件:qudong.c,make生成qudong.ko文件,烧录到ARM板上 #include<linux/init.h> #include<linux/module.h> ...
- 利用FluorineFx的ByteArray上传图片
Flex端利用new PNGEncoder().encode(bitmapData)将png图片转换为ByteArray,然后传给服务器,服务端需要定义一个public ByteArray Uploa ...
- 翻译学python---《Learn Python the hard Way》---第一章 绪论
打算学习python,但是又不想单纯地看书或是写个小项目,干脆引入很流行的翻译学习法来学习吧- 在论坛上看到了国外的一本<Learn Python the hard Way> ...
- IOS触摸事件和手势识别
IOS触摸事件和手势识别 目录 概述 触摸事件 手势识别 概述 为了实现一些新的需求,我们常常需要给IOS添加触摸事件和手势识别 触摸事件 触摸事件的四种方法 -(void)touchesBegan: ...
- MySQL auto_increment实现
http://www.cnblogs.com/xpchild/p/3825309.html 运维的时候,经常遇到auto_increment的疑惑: 机器异常crash,重启后id回退的问题 性能考虑 ...
- android studio无法更新之解决方案
当发现android studio有更新时,当然第一时间就想更新,可惜被墙了. 解决方案: 下载wallproxy,百度你懂的 在proxy.ini中最上面,找到ip和port 接着,在android ...
- ecshop被加入了黑链
朋友一个ecshop网站被攻击了,查看代码如下: <?php $password = "1";//设置密码 error_reporting(E_ERROR); header( ...
- 如何制作gif动画,丰富自己的博客?
不久前在博客园上看到有个哥们发表了一篇博客,其中使用了大量的动态的gif动画,感觉这种方法对于丰富博客内容非常有帮助,然后在网上搜索了一些关于制作gif动画的资料.我的方法不一定好,在这里仅作为抛砖引 ...
- InAction-MR的topK
本来只是想拿搜狗的数据练练手的,却无意踏足MR的topK问题.经过几番波折,虽然现在看起来很简单,但是摸爬滚打中也学到了不少 数据是搜狗实验室下的搜索日志,格式大概为: 00:00:00 298219 ...