intent-filter示例:

  1. <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>
  1. CustomActivity可以过滤接收到actionandroid.intent.action.VIEWcom.example.shad_fnst.intentfilterdemo.LAUNCH,以及datahttpIntent
    并作出响应。系统中的其他APP也可以过滤接收到MainActivity中的Intent,并作出响应,例如浏览器。
  1. package com.example.shad_fnst.intentfilterdemo;
  2.  
  3. import android.app.Activity;
  4. import android.content.Intent;
  5. import android.net.Uri;
  6. import android.os.Bundle;
  7. import android.view.Menu;
  8. import android.view.MenuItem;
  9. import android.view.View;
  10. import android.widget.Button;
  11.  
  12. public class MainActivity extends Activity {
  13.  
  14. @Override
  15. protected void onCreate(Bundle savedInstanceState) {
  16. super.onCreate(savedInstanceState);
  17. setContentView(R.layout.activity_main);
  18.  
  19. Button startBrowser_a = (Button) findViewById(R.id.start_browser_a);
  20. startBrowser_a.setOnClickListener(new View.OnClickListener() {
  21. @Override
  22. public void onClick(View v) {
  23. Intent intent = new Intent(android.content.Intent.ACTION_VIEW,
  24. Uri.parse("http://www.baidu.com"));
  25. startActivity(intent);
  26. }
  27. });
  28.  
  29. Button startBrowser_b = (Button) findViewById(R.id.start_browser_b);
  30. startBrowser_b.setOnClickListener(new View.OnClickListener() {
  31. @Override
  32. public void onClick(View v) {
  33. Intent intent = new Intent("com.example.shad_fnst.intentfilterdemo.LAUNCH",
  34. Uri.parse("http://www.baidu.com"));
  35. startActivity(intent);
  36. }
  37. });
  38.  
  39. Button startBrowser_c = (Button) findViewById(R.id.start_browser_c);
  40. startBrowser_c.setOnClickListener(new View.OnClickListener() {
  41. @Override
  42. public void onClick(View v) {
  43. Intent intent = new Intent("com.example.shad_fnst.intentfilterdemo.LAUNCH",
  44. Uri.parse("https://www.baidu.com"));
  45. startActivity(intent);
  46. }
  47. });
  48. }
  49.  
  50. @Override
  51. public boolean onCreateOptionsMenu(Menu menu) {
  52. // Inflate the menu; this adds items to the action bar if it is present.
  53. getMenuInflater().inflate(R.menu.menu_main, menu);
  54. return true;
  55. }
  56.  
  57. @Override
  58. public boolean onOptionsItemSelected(MenuItem item) {
  59. // Handle action bar item clicks here. The action bar will
  60. // automatically handle clicks on the Home/Up button, so long
  61. // as you specify a parent activity in AndroidManifest.xml.
  62. int id = item.getItemId();
  63.  
  64. //noinspection SimplifiableIfStatement
  65. if (id == R.id.action_settings) {
  66. return true;
  67. }
  68.  
  69. return super.onOptionsItemSelected(item);
  70. }
  71. }

MainActivity.java

  1. package com.example.shad_fnst.intentfilterdemo;
  2.  
  3. import android.app.Activity;
  4. import android.net.Uri;
  5. import android.os.Bundle;
  6. import android.widget.TextView;
  7.  
  8. public class CustomActivity extends Activity {
  9.  
  10. @Override
  11. protected void onCreate(Bundle savedInstanceState) {
  12. super.onCreate(savedInstanceState);
  13. setContentView(R.layout.activity_custom);
  14.  
  15. TextView label = (TextView) findViewById(R.id.show_data);
  16. Uri uri = getIntent().getData();
  17. label.setText(uri.toString());
  18. }
  19. }

CustomActivity.java

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  3. package="com.example.shad_fnst.intentfilterdemo" >
  4.  
  5. <application
  6. android:allowBackup="true"
  7. android:icon="@mipmap/ic_launcher"
  8. android:label="@string/app_name"
  9. android:theme="@style/AppTheme" >
  10. <activity
  11. android:name=".MainActivity"
  12. android:label="@string/app_name" >
  13. <intent-filter>
  14. <action android:name="android.intent.action.MAIN" />
  15.  
  16. <category android:name="android.intent.category.LAUNCHER" />
  17. </intent-filter>
  18. </activity>
  19. <activity
  20. android:name=".CustomActivity"
  21. android:label="@string/title_activity_custom" >
  22. <intent-filter>
  23. <action android:name="android.intent.action.VIEW"></action>
  24. <action android:name="com.example.shad_fnst.intentfilterdemo.LAUNCH"></action>
  25. <category android:name="android.intent.category.DEFAULT"></category>
  26. <data android:scheme="http"></data>
  27. </intent-filter>
  28. </activity>
  29. </application>
  30.  
  31. </manifest>

AndroidManifest.xml

  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:tools="http://schemas.android.com/tools" android:layout_width="fill_parent"
  3. android:layout_height="fill_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
  4. android:paddingRight="@dimen/activity_horizontal_margin"
  5. android:paddingTop="@dimen/activity_vertical_margin"
  6. android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"
  7. android:orientation="vertical">
  8.  
  9. <Button
  10. android:layout_width="fill_parent"
  11. android:layout_height="wrap_content"
  12. android:id="@+id/start_browser_a"
  13. android:text="@string/start_browser_a"/>
  14.  
  15. <Button
  16. android:layout_width="fill_parent"
  17. android:layout_height="wrap_content"
  18. android:id="@+id/start_browser_b"
  19. android:text="@string/start_browser_b"/>
  20.  
  21. <Button
  22. android:layout_width="fill_parent"
  23. android:layout_height="wrap_content"
  24. android:id="@+id/start_browser_c"
  25. android:text="@string/start_browser_c"/>
  26.  
  27. </LinearLayout>

activity_main.xml

  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
  3. android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
  4. android:paddingRight="@dimen/activity_horizontal_margin"
  5. android:paddingTop="@dimen/activity_vertical_margin"
  6. android:paddingBottom="@dimen/activity_vertical_margin"
  7. tools:context="com.example.shad_fnst.intentfilterdemo.CustomActivity">
  8.  
  9. <TextView
  10. android:layout_width="fill_parent"
  11. android:layout_height="400dp"
  12. android:id="@+id/show_data"/>
  13.  
  14. </RelativeLayout>

activity_custom.xml

  1. <resources>
  2. <string name="app_name">IntentFilterDemo</string>
  3.  
  4. <string name="hello_world">Hello world!</string>
  5. <string name="action_settings">Settings</string>
  6. <string name="start_browser_a">Start Browser with VIEW action</string>
  7. <string name="start_browser_b">Start Browser with LAUNCH action</string>
  8. <string name="start_browser_c">Exception Condition</string>
  9. <string name="title_activity_custom">CustomActivity</string>
  10. </resources>

strings.xml

IntentFilterDemo的更多相关文章

随机推荐

  1. Trie树学习2

    数组实现的Trie树 字符容量有限,能够使用链表实现更为大容量的Trie #include <iostream> #include <cstdio> #include < ...

  2. ARM&Linux 下驱动开发第二节

    驱动文件:qudong.c,make生成qudong.ko文件,烧录到ARM板上 #include<linux/init.h> #include<linux/module.h> ...

  3. 利用FluorineFx的ByteArray上传图片

    Flex端利用new PNGEncoder().encode(bitmapData)将png图片转换为ByteArray,然后传给服务器,服务端需要定义一个public ByteArray Uploa ...

  4. 翻译学python---《Learn Python the hard Way》---第一章 绪论

    打算学习python,但是又不想单纯地看书或是写个小项目,干脆引入很流行的翻译学习法来学习吧-         在论坛上看到了国外的一本<Learn Python the hard Way> ...

  5. IOS触摸事件和手势识别

    IOS触摸事件和手势识别 目录 概述 触摸事件 手势识别 概述 为了实现一些新的需求,我们常常需要给IOS添加触摸事件和手势识别 触摸事件 触摸事件的四种方法 -(void)touchesBegan: ...

  6. MySQL auto_increment实现

    http://www.cnblogs.com/xpchild/p/3825309.html 运维的时候,经常遇到auto_increment的疑惑: 机器异常crash,重启后id回退的问题 性能考虑 ...

  7. android studio无法更新之解决方案

    当发现android studio有更新时,当然第一时间就想更新,可惜被墙了. 解决方案: 下载wallproxy,百度你懂的 在proxy.ini中最上面,找到ip和port 接着,在android ...

  8. ecshop被加入了黑链

    朋友一个ecshop网站被攻击了,查看代码如下: <?php $password = "1";//设置密码 error_reporting(E_ERROR); header( ...

  9. 如何制作gif动画,丰富自己的博客?

    不久前在博客园上看到有个哥们发表了一篇博客,其中使用了大量的动态的gif动画,感觉这种方法对于丰富博客内容非常有帮助,然后在网上搜索了一些关于制作gif动画的资料.我的方法不一定好,在这里仅作为抛砖引 ...

  10. InAction-MR的topK

    本来只是想拿搜狗的数据练练手的,却无意踏足MR的topK问题.经过几番波折,虽然现在看起来很简单,但是摸爬滚打中也学到了不少 数据是搜狗实验室下的搜索日志,格式大概为: 00:00:00 298219 ...