Android:在服务类中读取NFC标签 [英] Android : Reading NFC tag in service class

查看:102
本文介绍了Android:在服务类中读取NFC标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个需要读取NFC标签中存储的数据的应用程序,我所说的数据是简单的整数值,例如0、1、2、3等.从NFC读取数据的功能在Activity类中时工作正常,但我需要在后台运行应用程序,因此即使应用程序未在前台运行,我也可以从NFC读取数据.因此,我编写了一个服务类,并将函数从Activity移到了服务类.但这没有用.

I am working on an application where i need to read the data stored in NFC tag, by data i mean is simple integer values as such 0,1,2,3 and so. The functions to read the data from NFC works fine when it's in Activity class but i need to run the application in the background so even when application is not running in foreground i can read the data from the NFC. So i wrote a service class and moved the function from the Activity to the service class. But it didn't work.

这是"MainActivity.java"

This is the "MainActivity.java"

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

startService(新的Intent(this,MyService.class));
此行启动服务类.

Service类为"MyService.java"

startService(new Intent(this, MyService.class));
This line starts the service class.

The Service class is "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");
    }

}

在此 onCreate()函数运行良好.问题是这一行:

In this the onCreate() function is running fine. The problem is this line :

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

该函数的第一个参数接受一个Activity的上下文,这就是我被困住的地方. 有什么办法可以解决这个问题或任何替代方案.谢谢您的帮助.

The first argument of the function accepts the context of an Activity and this is where i am stuck. Is there any way to solve this or any alternative. Thankyou for your help.

推荐答案

Android的NFC功能(专门用于接收标签,Beam等的Intent)仅适用于前台活动.

Android's NFC functionality (specifically receiving Intents for tags, Beam, etc) is available to foreground activities only.

这篇关于Android:在服务类中读取NFC标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆