am ac6db56a: am e842e956: update andriod beam demo. add action item to go to Android Beam settings, add help info, and add callback for on-push-complete
* commit 'ac6db56abde6ec08a5b869dad7400eaa5344dfd1': update andriod beam demo. add action item to go to Android Beam settings, add help info, and add callback for on-push-complete
This commit is contained in:
@@ -10,6 +10,4 @@ LOCAL_PACKAGE_NAME := AndroidBeamDemo
|
|||||||
|
|
||||||
LOCAL_SDK_VERSION := current
|
LOCAL_SDK_VERSION := current
|
||||||
|
|
||||||
LOCAL_PROGUARD_FLAG_FILES := proguard.flags
|
|
||||||
|
|
||||||
include $(BUILD_PACKAGE)
|
include $(BUILD_PACKAGE)
|
||||||
|
|||||||
Binary file not shown.
|
After Width: | Height: | Size: 1.8 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 1.1 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 2.4 KiB |
@@ -21,4 +21,7 @@
|
|||||||
android:id="@+id/textView"
|
android:id="@+id/textView"
|
||||||
android:layout_width="fill_parent"
|
android:layout_width="fill_parent"
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
|
android:text="@string/info"
|
||||||
|
android:padding="10dp"
|
||||||
|
android:textSize="18sp"
|
||||||
/>
|
/>
|
||||||
|
|||||||
7
samples/AndroidBeamDemo/res/menu/options.xml
Normal file
7
samples/AndroidBeamDemo/res/menu/options.xml
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<menu xmlns:android="http://schemas.android.com/apk/res/android">
|
||||||
|
<item android:id="@+id/menu_settings"
|
||||||
|
android:icon="@drawable/ic_menu_preferences"
|
||||||
|
android:showAsAction="ifRoom"
|
||||||
|
android:title="@string/menu_settings" />
|
||||||
|
</menu>
|
||||||
@@ -1,4 +1,9 @@
|
|||||||
<?xml version="1.0" encoding="utf-8"?>
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
<resources>
|
<resources>
|
||||||
<string name="app_name">Beam</string>
|
<string name="app_name">Beam</string>
|
||||||
|
<string name="info">Ensure that Android Beam is enabled by turning it on in
|
||||||
|
the system Settings (select the setting icon above), then place this device up
|
||||||
|
against another Android device that supports Android Beam to send a
|
||||||
|
message.</string>
|
||||||
|
<string name="menu_settings">Android Beam settings</string>
|
||||||
</resources>
|
</resources>
|
||||||
|
|||||||
@@ -22,38 +22,57 @@ import android.nfc.NdefMessage;
|
|||||||
import android.nfc.NdefRecord;
|
import android.nfc.NdefRecord;
|
||||||
import android.nfc.NfcAdapter;
|
import android.nfc.NfcAdapter;
|
||||||
import android.nfc.NfcAdapter.CreateNdefMessageCallback;
|
import android.nfc.NfcAdapter.CreateNdefMessageCallback;
|
||||||
|
import android.nfc.NfcAdapter.OnNdefPushCompleteCallback;
|
||||||
import android.nfc.NfcEvent;
|
import android.nfc.NfcEvent;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
|
import android.os.Handler;
|
||||||
|
import android.os.Message;
|
||||||
import android.os.Parcelable;
|
import android.os.Parcelable;
|
||||||
|
import android.provider.Settings;
|
||||||
|
import android.text.format.Time;
|
||||||
|
import android.view.Menu;
|
||||||
|
import android.view.MenuInflater;
|
||||||
|
import android.view.MenuItem;
|
||||||
import android.widget.TextView;
|
import android.widget.TextView;
|
||||||
import android.widget.Toast;
|
import android.widget.Toast;
|
||||||
|
|
||||||
import java.nio.charset.Charset;
|
import java.nio.charset.Charset;
|
||||||
|
|
||||||
|
|
||||||
public class Beam extends Activity implements CreateNdefMessageCallback {
|
public class Beam extends Activity implements CreateNdefMessageCallback,
|
||||||
|
OnNdefPushCompleteCallback {
|
||||||
NfcAdapter mNfcAdapter;
|
NfcAdapter mNfcAdapter;
|
||||||
TextView textView;
|
TextView mInfoText;
|
||||||
|
private static final int MESSAGE_SENT = 1;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onCreate(Bundle savedInstanceState) {
|
public void onCreate(Bundle savedInstanceState) {
|
||||||
super.onCreate(savedInstanceState);
|
super.onCreate(savedInstanceState);
|
||||||
setContentView(R.layout.main);
|
setContentView(R.layout.main);
|
||||||
TextView textView = (TextView) findViewById(R.id.textView);
|
|
||||||
|
mInfoText = (TextView) findViewById(R.id.textView);
|
||||||
// Check for available NFC Adapter
|
// Check for available NFC Adapter
|
||||||
mNfcAdapter = NfcAdapter.getDefaultAdapter(this);
|
mNfcAdapter = NfcAdapter.getDefaultAdapter(this);
|
||||||
if (mNfcAdapter == null) {
|
if (mNfcAdapter == null) {
|
||||||
Toast.makeText(this, "NFC is not available", Toast.LENGTH_LONG).show();
|
mInfoText = (TextView) findViewById(R.id.textView);
|
||||||
finish();
|
mInfoText.setText("NFC is not available on this device.");
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
// Register callback
|
// Register callback to set NDEF message
|
||||||
mNfcAdapter.setNdefPushMessageCallback(this, this);
|
mNfcAdapter.setNdefPushMessageCallback(this, this);
|
||||||
|
// Register callback to listen for message-sent success
|
||||||
|
mNfcAdapter.setOnNdefPushCompleteCallback(this, this);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Implementation for the CreateNdefMessageCallback interface
|
||||||
|
*/
|
||||||
@Override
|
@Override
|
||||||
public NdefMessage createNdefMessage(NfcEvent event) {
|
public NdefMessage createNdefMessage(NfcEvent event) {
|
||||||
String text = ("Beam me up, Android!\n\n" +
|
Time time = new Time();
|
||||||
"Beam Time: " + System.currentTimeMillis());
|
time.setToNow();
|
||||||
|
String text = ("Beam me up!\n\n" +
|
||||||
|
"Beam Time: " + time.format("%H:%M:%S"));
|
||||||
NdefMessage msg = new NdefMessage(
|
NdefMessage msg = new NdefMessage(
|
||||||
new NdefRecord[] { createMimeRecord(
|
new NdefRecord[] { createMimeRecord(
|
||||||
"application/com.example.android.beam", text.getBytes())
|
"application/com.example.android.beam", text.getBytes())
|
||||||
@@ -70,6 +89,28 @@ public class Beam extends Activity implements CreateNdefMessageCallback {
|
|||||||
return msg;
|
return msg;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Implementation for the OnNdefPushCompleteCallback interface
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void onNdefPushComplete(NfcEvent arg0) {
|
||||||
|
// A handler is needed to send messages to the activity when this
|
||||||
|
// callback occurs, because it happens from a binder thread
|
||||||
|
mHandler.obtainMessage(MESSAGE_SENT).sendToTarget();
|
||||||
|
}
|
||||||
|
|
||||||
|
/** This handler receives a message from onNdefPushComplete */
|
||||||
|
private final Handler mHandler = new Handler() {
|
||||||
|
@Override
|
||||||
|
public void handleMessage(Message msg) {
|
||||||
|
switch (msg.what) {
|
||||||
|
case MESSAGE_SENT:
|
||||||
|
Toast.makeText(getApplicationContext(), "Message sent!", Toast.LENGTH_LONG).show();
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onResume() {
|
public void onResume() {
|
||||||
super.onResume();
|
super.onResume();
|
||||||
@@ -89,13 +130,12 @@ public class Beam extends Activity implements CreateNdefMessageCallback {
|
|||||||
* Parses the NDEF Message from the intent and prints to the TextView
|
* Parses the NDEF Message from the intent and prints to the TextView
|
||||||
*/
|
*/
|
||||||
void processIntent(Intent intent) {
|
void processIntent(Intent intent) {
|
||||||
textView = (TextView) findViewById(R.id.textView);
|
|
||||||
Parcelable[] rawMsgs = intent.getParcelableArrayExtra(
|
Parcelable[] rawMsgs = intent.getParcelableArrayExtra(
|
||||||
NfcAdapter.EXTRA_NDEF_MESSAGES);
|
NfcAdapter.EXTRA_NDEF_MESSAGES);
|
||||||
// only one message sent during the beam
|
// only one message sent during the beam
|
||||||
NdefMessage msg = (NdefMessage) rawMsgs[0];
|
NdefMessage msg = (NdefMessage) rawMsgs[0];
|
||||||
// record 0 contains the MIME type, record 1 is the AAR, if present
|
// record 0 contains the MIME type, record 1 is the AAR, if present
|
||||||
textView.setText(new String(msg.getRecords()[0].getPayload()));
|
mInfoText.setText(new String(msg.getRecords()[0].getPayload()));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -109,4 +149,27 @@ public class Beam extends Activity implements CreateNdefMessageCallback {
|
|||||||
NdefRecord.TNF_MIME_MEDIA, mimeBytes, new byte[0], payload);
|
NdefRecord.TNF_MIME_MEDIA, mimeBytes, new byte[0], payload);
|
||||||
return mimeRecord;
|
return mimeRecord;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean onCreateOptionsMenu(Menu menu) {
|
||||||
|
// If NFC is not available, we won't be needing this menu
|
||||||
|
if (mNfcAdapter == null) {
|
||||||
|
return super.onCreateOptionsMenu(menu);
|
||||||
|
}
|
||||||
|
MenuInflater inflater = getMenuInflater();
|
||||||
|
inflater.inflate(R.menu.options, menu);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean onOptionsItemSelected(MenuItem item) {
|
||||||
|
switch (item.getItemId()) {
|
||||||
|
case R.id.menu_settings:
|
||||||
|
Intent intent = new Intent(Settings.ACTION_NFCSHARING_SETTINGS);
|
||||||
|
startActivity(intent);
|
||||||
|
return true;
|
||||||
|
default:
|
||||||
|
return super.onOptionsItemSelected(item);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user