2012-04-13 59 views
-1

我正在開發基於NFC的應用程序,從中獲取NFC設備的數據。但是,當我們將支持NFC的Android設備與NFC設備相鄰接時,會出現一個對話框,其中列出了所有基於NFC的應用程序。因此,其中一個也是我的。當我們從列表中點擊我們的應用程序圖標時,它會從我的應用程序(名爲NfcActivity)中打開一個活動並開始將數據傳輸到Android手機,我不想要。在該活動中還有兩個按鈕,即更新和取消。 我想要當我們點擊更新按鈕,然後NFC設備開始使用進度條將數據傳輸到手機,並且如果數據將完全傳輸,那麼進度欄會自動解除。我無法做同樣的事情,如果您對此有任何建議,請告訴我。開始從按鈕點擊傳輸NFC設備上的數據

在此先感謝。

NfcActivity:

package com.a1technology.impak; 


import java.io.IOException; 
import java.io.UnsupportedEncodingException; 
import java.math.BigInteger; 
import java.nio.ByteBuffer; 
import java.util.ArrayList; 
import java.util.Date; 
import java.util.List; 

import android.app.Activity; 
import android.app.AlertDialog; 
import android.app.PendingIntent; 
import android.content.ComponentName; 
import android.content.Context; 
import android.content.DialogInterface; 
import android.content.Intent; 
import android.content.ServiceConnection; 
import android.nfc.NfcAdapter; 
import android.nfc.tech.IsoDep; 
import android.os.Bundle; 
import android.os.IBinder; 
import android.util.Log; 
import android.view.View; 
import android.widget.Toast; 

import com.a1technology.impak.db.DBAdapter; 
import com.cypak.mobile.bridge.android.CypakDevice; 
import com.cypak.mobile.bridge.android.CypakDeviceListener; 
import com.cypak.mobile.bridge.android.NfcTransportService; 
import com.cypak.mobile.bridge.android.TransportBinder; 



public class NfcActivity extends Activity implements CypakDeviceListener { 

    private boolean update = false; 

    private String TAG = "NfcExampleActivity"; 

    List<EventByteGetSet> eventList = new ArrayList<EventByteGetSet>(); 

    // Required for foreground dispatch 
    private String[][] techListsArray = new String[][] { new String[] { IsoDep.class.getName() } }; 

    private NfcAdapter mAdapter; 

    private PendingIntent pendingIntent; 

    // Required when binding to NfcTransportService 
    private NfcTransportService mService; 

    private boolean bound = false; 


    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 

     super.onCreate(savedInstanceState); 
     setContentView(R.layout.nfc_activity); 

     findViewById(R.id.cancel_button).setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View arg0) { 

       finish(); 
      } 
     }); 
     findViewById(R.id.update_button).setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View arg0) { 

       update = true; 
      } 
     }); 
     // Required for foreground dispatch 
     pendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, new Intent(getApplicationContext(), NfcActivity.class).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0); 
     mAdapter = NfcAdapter.getDefaultAdapter(getApplicationContext());// Connecting 
               // to 
               // the 
               // adapter 

     resolveIntent(getIntent()); 

     Intent intent = new Intent(this, NfcTransportService.class); 
     // Bind to the service 
     if (!bindService(intent, mConnection, Context.BIND_AUTO_CREATE)) { 
      Log.e(TAG, "Failed to bind service"); 
     } 
    } 


    @Override 
    protected void onPause() { 

     super.onPause(); 
     // finish(); 
     // Required for foreground dispatch 
     mAdapter.disableForegroundDispatch(this); 

    } 


    @Override 
    protected void onDestroy() { 

     super.onDestroy(); 

     if (bound) { 
      unbindService(mConnection); 
      bound = false; 
     } 
    } 


    @Override 
    protected void onResume() { 

     super.onResume(); 

     // Required for foreground dispatch 
     mAdapter.enableForegroundDispatch(this, pendingIntent, null, techListsArray); 
    } 


    @Override 
    protected void onNewIntent(Intent intent) { 

     super.onNewIntent(intent); 
     setIntent(intent); 
     resolveIntent(intent); 
    } 


    private void resolveIntent(Intent intent) { 

     if (NfcAdapter.ACTION_TECH_DISCOVERED.equals(intent.getAction())// need 
               // to 
               // find 
               // what 
               // action 
               // is 
               // coming 
      && mService != null) { 
      // Let NfcTransportService handle the Nfc Intent 
      try { 
       mService.handleIntent(intent); 
      } 
      catch (IOException e) { 
       Log.w(TAG, "Failed to detect device", e); 
      } 

     } 
    } 

    /** 
    * Defines callbacks for service binding, passed to bindService(). Only 
    * needed if binding to MyMaxTransportService 
    * */ 
    private ServiceConnection mConnection = new ServiceConnection() { 

     @Override 
     public void onServiceConnected(ComponentName className, IBinder service) { 

      // We've bound to NfcTransportService, cast the IBinder and 
      // get NfcTransportService instance 
      Log.v(TAG, "Service connected"); 
      TransportBinder binder = (TransportBinder) service; 
      mService = (NfcTransportService) binder.getService(); 
      bound = true; 

      mService.registerDeviceListener(NfcActivity.this); 
     } 


     @Override 
     public void onServiceDisconnected(ComponentName name) { 

      Log.v(TAG, "Service disconnected"); 
      bound = false; 
     } 
    }; 


    @Override 
    public void deviceDiscovered(CypakDevice device) { 

     // String hex = "1"; 
     // byte[] byteClear= hex.getBytes(); 
     // 

     if (!update) { 
      return; 
     } 
     else 
      try { 
       // byte[] byteData= device.sendAppCommand((byte) 
       // 0x11, byteClear); 
       NFCDataHandler mNfcDataHandler = new NFCDataHandler(this); 
       byte[] byteStatusData = device.sendAppCommand((byte) 0x00, new byte[0]); 
       String stateHCC = mNfcDataHandler.getStatus(byteStatusData); 
       if (stateHCC.equalsIgnoreCase("04")) { 
        byte[] byteQueLogData = device.sendAppCommand((byte) 0x05, new byte[0]); 
        if (byteQueLogData.length < 6) { 
         ShowMessage("Warning", "Card is Empty"); 
         return; 
        } 
        mNfcDataHandler.getQueLog(byteQueLogData); 
       } 
       else { 
        ShowMessage("Warning", "Card is not Active"); 
        return; 

       } 

       Log.v("Value", ""); 
      } 
      catch (com.cypak.mobile.bridge.android.AppCommandErrorCodeException e) { 
       Log.e(TAG, "AppCommandErrorCodeException, error code " + e.getErrorCode(), e); 
      } 
      catch (com.cypak.mobile.bridge.android.AppCommandException e) { 
       Log.e(TAG, "AppCommandException", e); 
      } 
      catch (IllegalArgumentException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 

     // Toast.makeText(getApplicationContext(), "Device found! " + 
     // device, Toast.LENGTH_LONG).show(); 
    } 


    @Override 
    public void invalidDeviceDiscovered(CypakDevice device) { 

     Toast.makeText(getApplicationContext(), "Invalid device!", Toast.LENGTH_SHORT).show(); 
    } 


    @Override 
    public void deviceLost() { 

     Toast.makeText(getApplicationContext(), "Device lost!", Toast.LENGTH_SHORT).show(); 
    } 


    protected Date dateFromlongBuffer(long bb) { 

     return new Date((long) 1000 * (long) bb); 
    } 


    protected Date dateFromByteBuffer(ByteBuffer bb) { 

     return new Date((long) 1000 * (long) bb.getInt()); 
    } 

    static final byte[] HEX_CHAR_TABLE = { (byte) '0', (byte) '1', (byte) '2', (byte) '3', (byte) '4', (byte) '5', (byte) '6', (byte) '7', (byte) '8', (byte) '9', (byte) 'a', (byte) 'b', 
      (byte) 'c', (byte) 'd', (byte) 'e', (byte) 'f' }; 


    public static String getHexString(byte[] raw) throws UnsupportedEncodingException { 

     byte[] hex = new byte[2 * raw.length]; 
     int index = 0; 

     for (byte b : raw) { 
      int v = b & 0xFF; 
      hex[index++] = HEX_CHAR_TABLE[v >>> 4]; 
      hex[index++] = HEX_CHAR_TABLE[v & 0xF]; 
     } 
     return new String(hex, "ASCII"); 
    } 


    public static int hex2decimal(String s) { 

     String digits = "ABCDEF"; 
     s = s.toUpperCase(); 
     int val = 0; 
     for (int i = 0; i < s.length(); i++) { 
      char c = s.charAt(i); 
      int d = digits.indexOf(c); 
      val = 16 * val + d; 
     } 
     return val; 
    } 


    private void ShowMessage(String title, String message) { 

     AlertDialog.Builder b = new AlertDialog.Builder(this); 
     AlertDialog a = b.create(); 
     a.setTitle(title); 
     a.setMessage(message); 
     a.setButton("Ok", new DialogInterface.OnClickListener() { 

      public void onClick(DialogInterface arg0, int arg1) { 

       finish(); 
      } 
     }); 

     a.show(); 
    } 
} 

回答

0

它看起來像您所呼叫的onCreate()resolveIntent()。當您從應用選擇器中選擇您的應用時,NFC Discovered Intent將通過onCreate()傳遞給您的Activity,我假設您在撥打mService.handleIntent(intent);時正在處理它。因此,每當您的應用程序從NFC Discovery中啓動時,您都會立即將Intent關閉至您的Service。從您的問題中,您似乎希望在您收到NFC數據時從Intent中取出NFC,存儲該數據,然後在按下更新按鈕時撥打您的服務。

/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 

    super.onCreate(savedInstanceState); 
    setContentView(R.layout.nfc_activity); 

    findViewById(R.id.cancel_button).setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View arg0) { 

      finish(); 
     } 
    }); 
    findViewById(R.id.update_button).setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View arg0) { 

      update = true; 
     } 
    }); 
    // Required for foreground dispatch 
    pendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, new Intent(getApplicationContext(), NfcActivity.class).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0); 
    mAdapter = NfcAdapter.getDefaultAdapter(getApplicationContext());// Connecting 
              // to 
              // the 
              // adapter 
    // HERE, this could be the Intent from the NFC Discovery 
    **resolveIntent(getIntent());** 

    Intent intent = new Intent(this, NfcTransportService.class); 
    // Bind to the service 
    if (!bindService(intent, mConnection, Context.BIND_AUTO_CREATE)) { 
     Log.e(TAG, "Failed to bind service"); 
    } 
} 

所以你最終經過NFC意向給您的服務的時候了:

private void resolveIntent(Intent intent) { 

    if (NfcAdapter.ACTION_TECH_DISCOVERED.equals(intent.getAction())// need 
              // to 
              // find 
              // what 
              // action 
              // is 
              // coming 
     && mService != null) { 
     // Let NfcTransportService handle the Nfc Intent 
     try { 
      // HERE: you are handling the NFC Intent via onCreate() 
      mService.handleIntent(intent); 
     } 
     catch (IOException e) { 
      Log.w(TAG, "Failed to detect device", e); 
     } 

    } 
} 

你不希望等到用戶回調方法之前,單擊該按鈕,deviceDiscovered()被稱爲?

+0

您能否詳細說明您的答案? – 2012-04-13 19:22:03

+0

也許你應該包括服務代碼? – sethro 2012-04-13 19:37:38

+0

哪個服務代碼? – 2012-04-13 19:42:33