简体   繁体   中英

Bluetooth Chat Sample and Context Parameter

I'm Android/Java begginer trying to understand how Bluetooth Chat sample works. It uses threads and an handler, so I spent some weeks learning about that and now I have much clearer how this sample works. The class BluetoothChatService has this constructor:

public BluetoothChatService(Context context, Handler handler) {
       mAdapter = BluetoothAdapter.getDefaultAdapter();
       mState = STATE_NONE;
       mHandler = handler;
}

And from the main activity:

mChatService = new BluetoothChatService(this, mHandler);

My problem is with the context parameter. A mouse over shows "Parameter context is never used". So my question is why BluetoothChatService class needs a context?

Link to the source

The context is not needed and you can safely remove the Context context, . This will also eliminate the warning you are seeing.

In Android programming, a Context is needed for many things and it is usually needed almost everywhere in a program. Therefore it's commonplace to pass in a Context instance even if you don't strictly need it yet. Or needed it at one point but the need was removed after the code was changed and the Context is still passed around. I suspect something like this happened with the example.

Current Android tooling is much better in detecting unused code than the ancient tooling back in 2009 when Android 2.0 Eclair was released and this example with it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM