2012-08-30 36 views
0

我有一個應用程序有2個課程,我需要我的應用程序打開第二個課程CardActivity當NFC標籤點擊/滑動。該應用程序打開罰款,但MainActivity運行,而不是CardActivityAndroid NFC無法正常打開正確的課程

我會冒險猜測這是我的清單的問題,但它看起來是正確的。這是不分:

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.spotsofmagic.spotsofmagic" 
    android:versionCode="1" 
    android:versionName="1.0" android:installLocation="auto"> 

    <uses-sdk android:minSdkVersion="14" android:targetSdkVersion="14" /> 

    <uses-permission android:name="android.permission.NFC" /> 
    <uses-permission android:name="android.permission.BLUETOOTH" /> 
    <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" /> 
    <uses-feature android:name="android.hardware.nfc" android:required="true" /> 

    <application 
     android:icon="@drawable/ic_launcher" 
     android:label="@string/app_name" > 

     <activity 
      android:name=".MainActivity" 
      android:label="@string/app_name" > 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 
       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 

     <activity 
      android:name=".CardActivity" 
      android:label="@string/app_name" > 

      <!-- Handle a collectable card NDEF record --> 
      <intent-filter> 
       <action android:name="android.nfc.action.NDEF_DISCOVERED"/> 
       <data android:mimeType="application/vnd.spotsofmagic.spotsofmagic"/> 
       <category android:name="android.intent.category.DEFAULT"/> 
      </intent-filter> 
     </activity> 

    </application> 

</manifest> 

我相信標籤本身是正確的,因爲我已經在另一個應用程序打開它來查看其內容。

下面是兩個類。

CardActivity:

package com.spotsofmagic.spotsofmagic; 

import android.app.Activity; 
import android.app.AlertDialog; 
import android.content.DialogInterface; 
import android.content.DialogInterface.OnClickListener; 
import android.content.Intent; 
import android.nfc.NdefMessage; 
import android.nfc.NdefRecord; 
import android.nfc.NfcAdapter; 
import android.os.Bundle; 
import android.os.Parcelable; 
import android.util.Log; 
import android.bluetooth.*; 

public class CardActivity extends Activity implements OnClickListener { 

    private static final String TAG = null; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.card_activity); 

     // see if app was started from a tag and show game console 
     Intent intent = getIntent(); 

     Log.e(TAG, "Hello world. Intent Type: "+ intent.getType()); 

     if(intent.getType() != null && intent.getType().equals(MimeType.NFC_DEMO)) { 
      Parcelable[] rawMsgs = getIntent().getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES); 
      NdefMessage msg = (NdefMessage) rawMsgs[0]; 
      NdefRecord cardRecord = msg.getRecords()[0]; 
      String payload = new String(cardRecord.getPayload()); 
      turnBluetoothOn(payload); 
     } 
    } 

    private void turnBluetoothOn(String payload) { 

     final AlertDialog.Builder builder=new AlertDialog.Builder(this); 
     builder.setTitle("Alert Dialog"); 
     builder.setMessage(payload); 
     builder.setIcon(android.R.drawable.ic_dialog_alert); 


     BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); 
     if (mBluetoothAdapter == null) { 
      // Device does not support Bluetooth 
     } 
     if (!mBluetoothAdapter.isEnabled()) { 
      Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); 
      startActivityForResult(enableBtIntent, 1); 
     } 
     android.os.Process.killProcess(android.os.Process.myPid()); 

    } 

    public void onClick(DialogInterface dialog, int which) { 
     // TODO Auto-generated method stub 

    } 
} 

MainActivity:

package com.spotsofmagic.spotsofmagic; 

import android.app.Activity; 
import android.app.AlertDialog; 
import android.bluetooth.BluetoothAdapter; 
import android.content.DialogInterface; 
import android.content.DialogInterface.OnClickListener; 
import android.content.Intent; 
import android.nfc.NdefMessage; 
import android.nfc.NdefRecord; 
import android.nfc.NfcAdapter; 
import android.os.Bundle; 
import android.os.Parcelable; 
import android.util.Log; 
import android.widget.TextView; 


public class MainActivity extends Activity implements OnClickListener { 
    private static final String TAG = "Activity..."; 
    private NfcAdapter mAdapter; 
    private TextView mTextView; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main_activity); 

     // grab our NFC Adapter 
     mAdapter = NfcAdapter.getDefaultAdapter(this); 

     // TextView that we'll use to output messages to screen 
     mTextView = (TextView)findViewById(R.id.text_view); 

     displayMessage("Loading payload..."); 

    } 

    private void displayMessage(String message) { 
     mTextView.setText(message); 
    } 

    public void onClick(DialogInterface arg0, int arg1) { 
     // TODO Auto-generated method stub 

    } 
} 

這裏是我以前寫標籤的代碼。

NdefRecord appRecord = NdefRecord.createApplicationRecord("com.spotsofmagic.spotsofmagic"); 

     // record that contains our custom "retro console" game data, using custom MIME_TYPE 
     byte[] payload = getPayload().getBytes(); 
     byte[] mimeBytes = MimeType.NFC_DEMO.getBytes(Charset.forName("US-ASCII")); 
     NdefRecord cardRecord = new NdefRecord(NdefRecord.TNF_MIME_MEDIA, mimeBytes, 
               new byte[0], payload); 
     NdefMessage message = new NdefMessage(new NdefRecord[] { cardRecord, appRecord}); 

// Some code here removed for readability 


Ndef ndef = Ndef.get(tag); 
    if (ndef != null) { 
     ndef.connect(); 
     ndef.writeNdefMessage(message); 
+0

package =「com.spotsofmagic.spotsofmagic」不知道是否它的問題。您可能需要更改子包名稱或包名稱,使它們不相同。 – wtsang02

+0

不是問題,但絕對是我的錯誤做法。將得到改變:) – Mike

回答

1

是否在標籤NDEF消息包含一個Android Application Record:這是一個不同的應用程序順帶?這可以解釋MainActivity是如何啓動的。但是,如果AAR是標籤上NDEF消息的第一條記錄,或者第一條記錄與意圖過濾器不匹配,則這隻能是原因。

+0

是的,我正在使用Android應用程序記錄。我編輯了這個問題,包括用於編寫NFC標籤的代碼 – Mike

+0

我的猜測是意圖過濾器中的MIME類型(「application/vnd.spotsofmagic.spotsofmagic」)與「MimeType.NFC_DEMO」不同。然後意圖過濾器不匹配,這導致MainActivity啓動(由AAR)。 –

+0

你是對的。 'MimeType.NFC_DEMO'被設置爲作者應用程序,而不是讀者。謝謝你的幫助 :) – Mike