development
修订版 | e8b4ac2e6d04cc4651c8b0852d8bcf02c4e5019a (tree) |
---|---|
时间 | 2011-01-14 05:46:08 |
作者 | Jeff Hamilton <jham@andr...> |
Commiter | Android Git Automerger |
am aad3a1b7: Add a sample for the NFC foreground dispatch APIs.
* commit 'aad3a1b7327491f29db69a4e4bf7c3791662d6b5':
@@ -31,6 +31,7 @@ | ||
31 | 31 | <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> |
32 | 32 | <uses-permission android:name="android.permission.SEND_SMS" /> |
33 | 33 | <uses-permission android:name="android.permission.RECEIVE_SMS" /> |
34 | + <uses-permission android:name="android.permission.NFC" /> | |
34 | 35 | |
35 | 36 | <!-- For android.media.audiofx.Visualizer --> |
36 | 37 | <uses-permission android:name="android.permission.RECORD_AUDIO" /> |
@@ -1951,6 +1952,13 @@ | ||
1951 | 1952 | </intent-filter> |
1952 | 1953 | </activity> |
1953 | 1954 | |
1955 | + <activity android:name=".nfc.ForegroundDispatch" android:label="NFC/ForegroundDispatch"> | |
1956 | + <intent-filter> | |
1957 | + <action android:name="android.intent.action.MAIN" /> | |
1958 | + <category android:name="android.intent.category.SAMPLE_CODE" /> | |
1959 | + </intent-filter> | |
1960 | + </activity> | |
1961 | + | |
1954 | 1962 | </application> |
1955 | 1963 | |
1956 | 1964 | <instrumentation android:name=".app.LocalSampleInstrumentation" |
@@ -0,0 +1,23 @@ | ||
1 | +<?xml version="1.0" encoding="utf-8"?> | |
2 | +<!-- Copyright (C) 2007 The Android Open Source Project | |
3 | + | |
4 | + Licensed under the Apache License, Version 2.0 (the "License"); | |
5 | + you may not use this file except in compliance with the License. | |
6 | + You may obtain a copy of the License at | |
7 | + | |
8 | + http://www.apache.org/licenses/LICENSE-2.0 | |
9 | + | |
10 | + Unless required by applicable law or agreed to in writing, software | |
11 | + distributed under the License is distributed on an "AS IS" BASIS, | |
12 | + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
13 | + See the License for the specific language governing permissions and | |
14 | + limitations under the License. | |
15 | +--> | |
16 | + | |
17 | +<TextView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/text" | |
18 | + android:layout_width="match_parent" | |
19 | + android:layout_height="match_parent" | |
20 | + | |
21 | + android:gravity="center_vertical|center_horizontal" | |
22 | + android:textAppearance="?android:attr/textAppearanceMedium" | |
23 | +/> |
@@ -0,0 +1,90 @@ | ||
1 | +/* | |
2 | + * Copyright (C) 2011 The Android Open Source Project | |
3 | + * | |
4 | + * Licensed under the Apache License, Version 2.0 (the "License"); | |
5 | + * you may not use this file except in compliance with the License. | |
6 | + * You may obtain a copy of the License at | |
7 | + * | |
8 | + * http://www.apache.org/licenses/LICENSE-2.0 | |
9 | + * | |
10 | + * Unless required by applicable law or agreed to in writing, software | |
11 | + * distributed under the License is distributed on an "AS IS" BASIS, | |
12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
13 | + * See the License for the specific language governing permissions and | |
14 | + * limitations under the License | |
15 | + */ | |
16 | + | |
17 | +package com.example.android.apis.nfc; | |
18 | + | |
19 | +import com.example.android.apis.R; | |
20 | + | |
21 | +import android.app.Activity; | |
22 | +import android.app.PendingIntent; | |
23 | +import android.content.Intent; | |
24 | +import android.content.IntentFilter; | |
25 | +import android.content.IntentFilter.MalformedMimeTypeException; | |
26 | +import android.nfc.NfcAdapter; | |
27 | +import android.os.Bundle; | |
28 | +import android.util.Log; | |
29 | +import android.widget.TextView; | |
30 | + | |
31 | +/** | |
32 | + * An example of how to use the NFC foreground dispatch APIs. | |
33 | + */ | |
34 | +public class ForegroundDispatch extends Activity { | |
35 | + private NfcAdapter mAdapter; | |
36 | + private PendingIntent mPendingIntent; | |
37 | + private IntentFilter[] mFilters; | |
38 | + private TextView mText; | |
39 | + private int mCount = 0; | |
40 | + | |
41 | + @Override | |
42 | + public void onCreate(Bundle savedState) { | |
43 | + super.onCreate(savedState); | |
44 | + | |
45 | + setContentView(R.layout.foreground_dispatch); | |
46 | + mText = (TextView) findViewById(R.id.text); | |
47 | + mText.setText("Scan a tag"); | |
48 | + | |
49 | + mAdapter = NfcAdapter.getDefaultAdapter(this); | |
50 | + | |
51 | + // Create a generic PendingIntent that will be deliver to this activity. The NFC stack | |
52 | + // will fill in the intent with the details of the discovered tag before delivering to | |
53 | + // this activity. | |
54 | + mPendingIntent = PendingIntent.getActivity(this, 0, | |
55 | + new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0); | |
56 | + | |
57 | + // Setup intent filters for all types of NFC dispatches to intercept all discovered tags | |
58 | + IntentFilter ndef = new IntentFilter(NfcAdapter.ACTION_NDEF_DISCOVERED); | |
59 | + try { | |
60 | + ndef.addDataType("*/*"); | |
61 | + } catch (MalformedMimeTypeException e) { | |
62 | + throw new RuntimeException("fail", e); | |
63 | + } | |
64 | + IntentFilter tech = new IntentFilter(NfcAdapter.ACTION_TECHNOLOGY_DISCOVERED); | |
65 | + tech.addDataScheme("vnd.android.nfc"); | |
66 | + mFilters = new IntentFilter[] { | |
67 | + ndef, | |
68 | + tech, | |
69 | + new IntentFilter(NfcAdapter.ACTION_TAG_DISCOVERED), | |
70 | + }; | |
71 | + } | |
72 | + | |
73 | + @Override | |
74 | + public void onResume() { | |
75 | + super.onResume(); | |
76 | + mAdapter.enableForegroundDispatch(this, mPendingIntent, mFilters); | |
77 | + } | |
78 | + | |
79 | + @Override | |
80 | + public void onNewIntent(Intent intent) { | |
81 | + Log.i("Foreground dispatch", "Discovered tag with intent: " + intent); | |
82 | + mText.setText("Discovered tag " + ++mCount + " with intent: " + intent); | |
83 | + } | |
84 | + | |
85 | + @Override | |
86 | + public void onPause() { | |
87 | + super.onPause(); | |
88 | + mAdapter.disableForegroundDispatch(this); | |
89 | + } | |
90 | +} |