문제

Well, I'm at a dilemma here. I made my own class that uses the Bluetooth class from android but I'm not sure where to put it. Extending the android Bluetooth class seems like a good idea but I need to override the onActivityResult() which is only available to an activity class. So, where would I put my class so that I have access to onActivityResult() (keeping in mind the idea here is to use as few dependencies as possible)?

In other words, I want to move the Bluetooth code from the main activity to a separate class.

도움이 되었습니까?

해결책

You should to use separate file for each class. You can create a folder "engine". For example: com.mycorp.myapp.engine. You can get access to onActivityResult() very simple. For example: MainActivity.onActivityResult(). Note: function should be public. Or you can pass your activity to your CustomBluetooth's constructor.

public class CustomBluetooth {

    private Activity mActivity;

    /* Constructor */
    public CustomBluetooth (Activity pActivity ) {
        super();
        this.mActivity = pActivity;
    }

    /* Your functions */
    public int getResult() {
        return this.mActivity.onActivityResult();
    }
}

Alex. P.S. Sorry for my English:)

다른 팁

Add an interface to your Bluetooth class and implement the interface in your activity.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top