mardi 6 décembre 2016

Android PhoneStateListener constructor reflection

There are 4 PhoneStateListener constructors in code but just one is exposed and others are hided like below :

public PhoneStateListener() {
    this(SubscriptionManager.DEFAULT_SUBSCRIPTION_ID, Looper.myLooper());
}

/**
* Create a PhoneStateListener for the Phone with the default subscription
* using a particular non-null Looper.
* @hide
*/
public PhoneStateListener(Looper looper) {
  this(SubscriptionManager.DEFAULT_SUBSCRIPTION_ID, looper);
}

/**
* Create a PhoneStateListener for the Phone using the specified subscription.
* This class requires Looper.myLooper() not return null. To supply your
* own non-null Looper use PhoneStateListener(int subId, Looper looper) below.
* @hide
*/
public PhoneStateListener(int subId) {
  this(subId, Looper.myLooper());
}

/**
* Create a PhoneStateListener for the Phone using the specified subscription
* and non-null Looper.
* @hide
*/
public PhoneStateListener(int subId, Looper looper) {
  ....
  ....

May I ask how to use contrcutor reflection for hidden constructors in my code ?

public final class MyStateListener extends PhoneStateListener {
    private static final String TAG = "MyStateListener ";

    public MyStateListener(Context c) {
        int subId = 1;
        Looper looper = c.getMainLooper();

        super(subId, looper); // <-- this is hidden constructor so I want to replace this into constructor refrection.
        ....
        ....

Instead of super(subId, looper), is below line acceptable ? If not, any good idea?

this.getClass().getSuperclass().getConstructors()[2].newInstance(1, c.getMainLooper());





Aucun commentaire:

Enregistrer un commentaire