码迷,mamicode.com
首页 > 其他好文 > 详细

IntentFilterDemo

时间:2015-07-31 19:54:50      阅读:238      评论:0      收藏:0      [点我收藏+]

标签:

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

标签:

原文地址:http://www.cnblogs.com/hello-sandy/p/4692870.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!