如何使用反射来访问安全元素

时间:2014-03-19 15:45:19

标签: android nfc

我正在尝试将以下代码块添加到现有类

Class nfcExtrasClazz = Class.forName("com.android.nfc_extras.NfcAdapterExtras");
Method getMethod = nfcExtrasClazz .getMethod("get", Class.forName("android.nfc.NfcAdapter"));
NfcAdapter adapter = NfcAdapter.getDefaultAdapter(this);
Object nfcExtras = getMethod .invoke(nfcExtrasClazz, adapter);
Method getEEMethod = nfcExtras.getClass().getMethod("getEmbeddedExecutionEnvironment", (Class[]) null);
Object ee = getEEMethod.invoke(nfcExtras , (Object[]) null);
Class eeClazz = se.getClass();
Method openMethod = eeClazz.getMethod("open", (Class[]) null);
Method transceiveMethod = ee.getClass().getMethod("transceive",new Class[] { byte[].class });
Method closeMethod = eeClazz.getMethod("close", (Class[]) null);
openMethod.invoke(se, (Object[]) null);
Object response = transceiveMethod.invoke(se, command);
closeMethod.invoke(se, (Object[]) null);

我意识到我需要将此代码放在另一个线程中。所以我得到了一个现有的本地服务实现,并试图将上面的代码添加到它。但是在服务实现代码中我得到以下错误:" NfcAdapter类型中的方法getDefaultAdapter(Context)不适用于参数  (BackgroundService.ServiceWorker)" 包中有两个类;一个是主要的: 公共类MainActivity扩展Activity         {             private static final String TAG =" MainActivity&#34 ;;             private int counter = 1;             私人NfcAdapter适配器;

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

        public void doClick(View view) {
            switch(view.getId()) {
            case R.id.startBtn:
                Log.v(TAG, "Starting service... counter = " + counter);
                adapter = NfcAdapter.getDefaultAdapter(this);
                Intent intent = new Intent(MainActivity.this,
                    BackgroundService.class);
            intent.putExtra("counter", counter++);
            startService(intent);
            break;
        case R.id.stopBtn:
            stopService();
        }
    }

    private void stopService() {
        Log.v(TAG, "Stopping service...");
        if(stopService(new Intent(MainActivity.this,
                    BackgroundService.class)))
            Log.v(TAG, "stopService was successful");
        else
            Log.v(TAG, "stopService was unsuccessful");
    }

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

这是实现本地服务的类:     公共类BackgroundService扩展了Service     {         private static final String TAG =" BackgroundService&#34 ;;         private NotificationManager notificationMgr;         私人NfcAdapter适配器;         private ThreadGroup myThreads = new ThreadGroup(" ServiceWorker");

    @Override
    public void onCreate() {
        super.onCreate();

        Log.v(TAG, "in onCreate()");
        notificationMgr =(NotificationManager)getSystemService(
               NOTIFICATION_SERVICE);
        displayNotificationMessage("Background Service is running");
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        super.onStartCommand(intent, flags, startId);
        int counter = intent.getExtras().getInt("counter");
        Log.v(TAG, "in onStartCommand(), counter = " + counter +
                ", startId = " + startId);

        new Thread(myThreads, new ServiceWorker(counter), "BackgroundService")
            .start();

        return START_NOT_STICKY;
    }

    class ServiceWorker implements Runnable
    {
        private int counter = -1;
        public ServiceWorker(int counter) {
            this.counter = counter;
        }

        public void run() {
            final String TAG2 = "ServiceWorker:" + Thread.currentThread().getId();
            // do background processing here...
            try {
                Log.v(TAG2, "sleeping for 10 seconds. counter = " + counter);
                Thread.sleep(10000);
                // I added the following code; I temprarily commented all of lines out
                //NfcAdapterExtras adapterExtras = NfcAdapterExtras.get(NfcAdapter.getDefaultAdapter(this));
                //NfcExecutionEnvironment nfceEe = adapterExtras.getEmbeddedExecutionEnvironment();
                Class nfcExtrasClazz = null;
                try {
                    nfcExtrasClazz = Class.forName("com.android.nfc_extras.NfcAdapterExtras");
                } catch (ClassNotFoundException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                Method getMethod = null;
                try {
                    getMethod = nfcExtrasClazz .getMethod("get", Class.forName("android.nfc.NfcAdapter"));
                } catch (NoSuchMethodException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (ClassNotFoundException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                adapter = NfcAdapter.getDefaultAdapter(this);
                Object nfcExtras = null;
                try {
                    nfcExtras = getMethod .invoke(nfcExtrasClazz, adapter);
                } catch (IllegalAccessException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IllegalArgumentException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (InvocationTargetException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

                Method getEEMethod = null;
                try {
                    getEEMethod = nfcExtras.getClass().getMethod("getEmbeddedExecutionEnvironment", 
                                      (Class[]) null);
                } catch (NoSuchMethodException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                Object ee = null;
                try {
                    ee = getEEMethod.invoke(nfcExtras , (Object[]) null);
                } catch (IllegalAccessException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IllegalArgumentException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (InvocationTargetException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                Class eeClazz = ee.getClass();
                Method openMethod = null;
                try {
                    openMethod = eeClazz.getMethod("open", (Class[]) null);
                } catch (NoSuchMethodException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                Method transceiveMethod = null;
                try {
                    transceiveMethod = ee.getClass().getMethod("transceive",
                                        new Class[] { byte[].class });
                } catch (NoSuchMethodException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                Method closeMethod = null;
                try {
                    closeMethod = eeClazz.getMethod("close", (Class[]) null);
                } catch (NoSuchMethodException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

                try {
                    openMethod.invoke(ee, (Object[]) null);
                } catch (IllegalAccessException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IllegalArgumentException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (InvocationTargetException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                String command = "00000000";
                try {
                    Object myresponse = transceiveMethod.invoke(ee, command );
                } catch (IllegalAccessException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IllegalArgumentException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (InvocationTargetException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                try {
                    closeMethod.invoke(ee, (Object[]) null);
                } catch (IllegalAccessException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IllegalArgumentException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (InvocationTargetException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                Log.v(TAG2, "... waking up");
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                Log.v(TAG2, "... sleep interrupted");
            }
        }
    }

    @Override
    public void onDestroy()
    {
        Log.v(TAG, "in onDestroy(). Interrupting threads and cancelling notifications");
        myThreads.interrupt();
        notificationMgr.cancelAll();
        super.onDestroy();
    }

    @Override
    public IBinder onBind(Intent intent) {
        Log.v(TAG, "in onBind()");
        return null;
    }

    private void displayNotificationMessage(String message)
    {
        Notification notification = new Notification(R.drawable.emo_im_winking, 
                message, System.currentTimeMillis());

        notification.flags = Notification.FLAG_NO_CLEAR;

        PendingIntent contentIntent = 
                PendingIntent.getActivity(this, 0, new Intent(this, MainActivity.class), 0);

        notification.setLatestEventInfo(this, TAG, message, contentIntent);

        notificationMgr.notify(0, notification);
    }
}

我想我可能不会将主活动中的nfcadapter实例添加到服务中?有人可以看看吗?

2 个答案:

答案 0 :(得分:1)

查看链接,用ee替换你的se,这是一个错字。

答案 1 :(得分:0)

se永远不会使用此代码创建,因此无法解析为对象。

我们第一次看到se在通话中

Class eeClazz = se.getClass();

这可能是你得到错误的地方。