diff options
author | Scott Main <smain@google.com> | 2011-10-14 01:19:19 -0700 |
---|---|---|
committer | Scott Main <smain@google.com> | 2011-10-14 10:28:25 -0700 |
commit | 0f53a4d2942a463bc3edf25210c523dc6c902373 (patch) | |
tree | f990382239244516473c1c6919bc5735bd14652d /samples/AndroidBeamDemo | |
parent | 34ff3e789cd0e2cb10ddff57055cb0d2be3ec83d (diff) | |
download | android_development-0f53a4d2942a463bc3edf25210c523dc6c902373.tar.gz android_development-0f53a4d2942a463bc3edf25210c523dc6c902373.tar.bz2 android_development-0f53a4d2942a463bc3edf25210c523dc6c902373.zip |
rename Android Beam sample to Andriod Beam Demo and add launcher icon
Change-Id: If6edb7ac9ba2743ce0dbc39be6f1eede8ef66aaa
Diffstat (limited to 'samples/AndroidBeamDemo')
-rw-r--r-- | samples/AndroidBeamDemo/AndroidManifest.xml | 42 | ||||
-rwxr-xr-x | samples/AndroidBeamDemo/_index.html | 3 | ||||
-rw-r--r-- | samples/AndroidBeamDemo/drawable-hdpi/ic_launcher.png | bin | 0 -> 5365 bytes | |||
-rw-r--r-- | samples/AndroidBeamDemo/drawable-ldpi/ic_launcher.png | bin | 0 -> 2475 bytes | |||
-rw-r--r-- | samples/AndroidBeamDemo/drawable-mdpi/ic_launcher.png | bin | 0 -> 3508 bytes | |||
-rw-r--r-- | samples/AndroidBeamDemo/drawable-xhdpi/ic_launcher.png | bin | 0 -> 6665 bytes | |||
-rw-r--r-- | samples/AndroidBeamDemo/res/layout/main.xml | 24 | ||||
-rw-r--r-- | samples/AndroidBeamDemo/res/values/strings.xml | 4 | ||||
-rw-r--r-- | samples/AndroidBeamDemo/src/com/example/android/beam/Beam.java | 112 |
9 files changed, 185 insertions, 0 deletions
diff --git a/samples/AndroidBeamDemo/AndroidManifest.xml b/samples/AndroidBeamDemo/AndroidManifest.xml new file mode 100644 index 000000000..e01eb2d40 --- /dev/null +++ b/samples/AndroidBeamDemo/AndroidManifest.xml @@ -0,0 +1,42 @@ +<?xml version="1.0" encoding="utf-8"?> +<!-- +/* +** +** Copyright 2011, The Android Open Source Project +** +** Licensed under the Apache License, Version 2.0 (the "License"); +** you may not use this file except in compliance with the License. +** You may obtain a copy of the License at +** +** http://www.apache.org/licenses/LICENSE-2.0 +** +** Unless required by applicable law or agreed to in writing, software +** distributed under the License is distributed on an "AS IS" BASIS, +** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +** See the License for the specific language governing permissions and +** limitations under the License. +*/ +--> +<manifest xmlns:android="http://schemas.android.com/apk/res/android" + package="com.example.android.beam" + android:versionCode="1" + android:versionName="1.0"> + <uses-permission android:name="android.permission.NFC" /> + <uses-sdk android:minSdkVersion="14" /> + <uses-feature android:name="android.hardware.nfc" /> + <application android:icon="@drawable/icon" android:label="@string/app_name"> + <activity android:name="com.example.android.beam.Beam" + android:label="@string/app_name" + android:launchMode="singleTop"> + <intent-filter> + <action android:name="android.intent.action.MAIN" /> + <category android:name="android.intent.category.LAUNCHER" /> + </intent-filter> + <intent-filter> + <action android:name="android.nfc.action.NDEF_DISCOVERED" /> + <category android:name="android.intent.category.DEFAULT" /> + <data android:mimeType="application/com.example.android.beam" /> + </intent-filter> + </activity> + </application> +</manifest> diff --git a/samples/AndroidBeamDemo/_index.html b/samples/AndroidBeamDemo/_index.html new file mode 100755 index 000000000..9fe80b08a --- /dev/null +++ b/samples/AndroidBeamDemo/_index.html @@ -0,0 +1,3 @@ +<p>Android Beam Demo shows how to use the Android Beam feature introduced in API level 14. This +application beams a simple message from one device to another when they are in close enough proximity. This +application must be installed on two devices that have NFC capabilities, running Android 4.0 or later.</p> diff --git a/samples/AndroidBeamDemo/drawable-hdpi/ic_launcher.png b/samples/AndroidBeamDemo/drawable-hdpi/ic_launcher.png Binary files differnew file mode 100644 index 000000000..c96fa9e05 --- /dev/null +++ b/samples/AndroidBeamDemo/drawable-hdpi/ic_launcher.png diff --git a/samples/AndroidBeamDemo/drawable-ldpi/ic_launcher.png b/samples/AndroidBeamDemo/drawable-ldpi/ic_launcher.png Binary files differnew file mode 100644 index 000000000..ab4d45e78 --- /dev/null +++ b/samples/AndroidBeamDemo/drawable-ldpi/ic_launcher.png diff --git a/samples/AndroidBeamDemo/drawable-mdpi/ic_launcher.png b/samples/AndroidBeamDemo/drawable-mdpi/ic_launcher.png Binary files differnew file mode 100644 index 000000000..e9ce6eb68 --- /dev/null +++ b/samples/AndroidBeamDemo/drawable-mdpi/ic_launcher.png diff --git a/samples/AndroidBeamDemo/drawable-xhdpi/ic_launcher.png b/samples/AndroidBeamDemo/drawable-xhdpi/ic_launcher.png Binary files differnew file mode 100644 index 000000000..cd225d768 --- /dev/null +++ b/samples/AndroidBeamDemo/drawable-xhdpi/ic_launcher.png diff --git a/samples/AndroidBeamDemo/res/layout/main.xml b/samples/AndroidBeamDemo/res/layout/main.xml new file mode 100644 index 000000000..dce616998 --- /dev/null +++ b/samples/AndroidBeamDemo/res/layout/main.xml @@ -0,0 +1,24 @@ +<?xml version="1.0" encoding="utf-8"?> +<!-- +/* +** +** Copyright 2011, The Android Open Source Project +** +** Licensed under the Apache License, Version 2.0 (the "License"); +** you may not use this file except in compliance with the License. +** You may obtain a copy of the License at +** +** http://www.apache.org/licenses/LICENSE-2.0 +** +** Unless required by applicable law or agreed to in writing, software +** distributed under the License is distributed on an "AS IS" BASIS, +** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +** See the License for the specific language governing permissions and +** limitations under the License. +*/ +--> +<TextView xmlns:android="http://schemas.android.com/apk/res/android" + android:id="@+id/textView" + android:layout_width="fill_parent" + android:layout_height="wrap_content" + /> diff --git a/samples/AndroidBeamDemo/res/values/strings.xml b/samples/AndroidBeamDemo/res/values/strings.xml new file mode 100644 index 000000000..ff4492fe5 --- /dev/null +++ b/samples/AndroidBeamDemo/res/values/strings.xml @@ -0,0 +1,4 @@ +<?xml version="1.0" encoding="utf-8"?> +<resources> + <string name="app_name">Beam</string> +</resources> diff --git a/samples/AndroidBeamDemo/src/com/example/android/beam/Beam.java b/samples/AndroidBeamDemo/src/com/example/android/beam/Beam.java new file mode 100644 index 000000000..17ec3256f --- /dev/null +++ b/samples/AndroidBeamDemo/src/com/example/android/beam/Beam.java @@ -0,0 +1,112 @@ +/* + * Copyright (C) 2011 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.android.beam; + +import android.app.Activity; +import android.content.Intent; +import android.nfc.NdefMessage; +import android.nfc.NdefRecord; +import android.nfc.NfcAdapter; +import android.nfc.NfcAdapter.CreateNdefMessageCallback; +import android.nfc.NfcEvent; +import android.os.Bundle; +import android.os.Parcelable; +import android.widget.TextView; +import android.widget.Toast; +import java.nio.charset.Charset; + + +public class Beam extends Activity implements CreateNdefMessageCallback { + NfcAdapter mNfcAdapter; + TextView textView; + + @Override + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.main); + TextView textView = (TextView) findViewById(R.id.textView); + // Check for available NFC Adapter + mNfcAdapter = NfcAdapter.getDefaultAdapter(this); + if (mNfcAdapter == null) { + Toast.makeText(this, "NFC is not available", Toast.LENGTH_LONG).show(); + finish(); + return; + } + // Register callback + mNfcAdapter.setNdefPushMessageCallback(this, this); + } + + @Override + public NdefMessage createNdefMessage(NfcEvent event) { + String text = ("Beam me up, Android!\n\n" + + "Beam Time: " + System.currentTimeMillis()); + NdefMessage msg = new NdefMessage( + new NdefRecord[] { createMimeRecord( + "application/com.example.android.beam", text.getBytes()) + /** + * The Android Application Record (AAR) is commented out. When a device + * receives a push with an AAR in it, the application specified in the AAR + * is guaranteed to run. The AAR overrides the tag dispatch system. + * You can add it back in to guarantee that this + * activity starts when receiving a beamed message. For now, this code + * uses the tag dispatch system. + */ + //,NdefRecord.createApplicationRecord("com.example.android.beam") + }); + return msg; + } + + @Override + public void onResume() { + super.onResume(); + // Check to see that the Activity started due to an Android Beam + if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(getIntent().getAction())) { + processIntent(getIntent()); + } + } + + @Override + public void onNewIntent(Intent intent) { + // onResume gets called after this to handle the intent + setIntent(intent); + } + + /** + * Parses the NDEF Message from the intent and prints to the TextView + */ + void processIntent(Intent intent) { + textView = (TextView) findViewById(R.id.textView); + Parcelable[] rawMsgs = intent.getParcelableArrayExtra( + NfcAdapter.EXTRA_NDEF_MESSAGES); + // only one message sent during the beam + NdefMessage msg = (NdefMessage) rawMsgs[0]; + // record 0 contains the MIME type, record 1 is the AAR, if present + textView.setText(new String(msg.getRecords()[0].getPayload())); + } + + /** + * Creates a custom MIME type encapsulated in an NDEF record + * + * @param mimeType + */ + public NdefRecord createMimeRecord(String mimeType, byte[] payload) { + byte[] mimeBytes = mimeType.getBytes(Charset.forName("US-ASCII")); + NdefRecord mimeRecord = new NdefRecord( + NdefRecord.TNF_MIME_MEDIA, mimeBytes, new byte[0], payload); + return mimeRecord; + } +} |