2013-10-28 16 views
5

Tôi đang làm việc trên một ứng dụng mà tôi cần đọc dữ liệu được lưu trữ trong thẻ NFC, theo dữ liệu tôi có nghĩa là các giá trị số nguyên đơn giản như 0,1,2,3 và như vậy. Các chức năng đọc dữ liệu từ NFC hoạt động tốt khi ở trong lớp Activity nhưng tôi cần chạy ứng dụng ở chế độ nền nên ngay cả khi ứng dụng không chạy ở nền trước tôi có thể đọc dữ liệu từ NFC. Vì vậy, tôi đã viết một lớp dịch vụ và di chuyển hàm từ Activity sang lớp dịch vụ. Nhưng nó không hoạt động.

Android: Đọc thẻ NFC trong lớp dịch vụ

Đây là "MainActivity.java"

protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     startService(new Intent(this, MyService.class)); 
    } 

startService (Ý định mới (điều này, MyService.class));
Dòng này bắt đầu lớp dịch vụ.

lớp Dịch vụ là "MyService.java"

public class MyService extends Service { 

    private NfcAdapter mNfcAdapter; 
    private PendingIntent mNfcPendingIntent; 
    private IntentFilter[] mReadTagFilters; 

    @Override 
    public IBinder onBind(Intent intent) { 
     return null; 
    } 

    @Override 
    public void onCreate() { 
     super.onCreate(); 
     mNfcAdapter = NfcAdapter.getDefaultAdapter(this); 
     mNfcPendingIntent = PendingIntent.getActivity(this, 0, new 
     Intent(this, 
     getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP 
     | Intent.FLAG_ACTIVITY_CLEAR_TOP), 0); 
     IntentFilter ndefDetected = new IntentFilter(
     NfcAdapter.ACTION_NDEF_DISCOVERED); 
     ndefDetected.addDataScheme("http"); 
     IntentFilter techDetected = new IntentFilter(
     NfcAdapter.ACTION_TECH_DISCOVERED);  
     mReadTagFilters = new IntentFilter[] { ndefDetected, techDetected };  
      Log.v("service", "return from onCreate function"); 
    } 

    @Override 
    public void onDestroy() { 
     super.onDestroy(); 
    } 

    @Override 
    @Deprecated 
    public void onStart(Intent intent, int startId) { 
     super.onStart(intent, startId); 


     Log.v("service", "onStart function"); 
     if (mNfcAdapter != null) { 
      if (!mNfcAdapter.isEnabled()) { 
       new AlertDialog.Builder(this) 
         .setTitle("NFC Dialog") 
         .setMessage("Please switch on NFC") 
         .setPositiveButton("Settings", 
           new DialogInterface.OnClickListener() { 
            public void onClick(DialogInterface arg0, 
              int arg1) { 
             Intent setnfc = new Intent(
               Settings.ACTION_WIRELESS_SETTINGS); 
             startActivity(setnfc); 
            } 
           }) 
         .setOnCancelListener(
           new DialogInterface.OnCancelListener() { 
            public void onCancel(DialogInterface dialog) { 
             dialog.dismiss(); 
            } 
           }).create().show(); 
      } 
      mNfcAdapter.enableForegroundDispatch (MainActivity.mContext, mNfcPendingIntent, 
        mReadTagFilters, null); 
     } else { 
      Toast.makeText(this, 
        "Sorry, NFC adapter not available on your device.", 
        Toast.LENGTH_SHORT).show(); 
     } 
     Log.v("service", "return fonStart function"); 
    } 

} 

Trong này chức năng onCreate() đang chạy tốt. Vấn đề là dòng này:

mNfcAdapter.enableForegroundDispatch (mContext, mNfcPendingIntent, 
        mReadTagFilters, null); 

Đối số đầu tiên của hàm này chấp nhận ngữ cảnh của Hoạt động và đây là nơi tôi bị kẹt. Có cách nào để giải quyết vấn đề này hay bất kỳ giải pháp thay thế nào khác. Cảm ơn sự giúp đỡ của bạn.

Trả lời

3

Chức năng NFC của Android (đặc biệt nhận Intents cho thẻ, Chùm, v.v.) chỉ khả dụng cho hoạt động tiền cảnh.

+0

Cảm ơn bạn đã trả lời nhưng bạn có thể đề xuất cách NFC Android mặc định đọc thẻ trong khi chạy ở chế độ nền và cố mở dữ liệu bằng trình duyệt không? –

+0

Phát hiện thẻ NFC được xử lý bởi dịch vụ hệ thống NFC. Sau đó, dịch vụ hệ thống sẽ thông báo ** các hoạt động ** dựa trên các ý định. Trình duyệt web, giống như các hoạt động khác muốn nhận sự kiện NFC, có bộ lọc ý định được đăng ký cho những mục đích đó. –

Các vấn đề liên quan