lundi 3 octobre 2016

Invoke method inside a Thread

I just learn about Invoking Method from this link, and now I am trying to used it but I can't make it work with the logic I want. I simply want to invoke a method from a Thread.

I've used the below code and it work. But the problem here is the loop, The loop have to list all the method from a class and check if the specific method is exist and then invoke it.

Class myclass = Class.forName("com.package.MainActivity");
Method[] methods = myclass.getMethods();
for (int i = 0; i < methods.length; i++) {
    if (methods[i].getName().equals("methodName")) {
        Log.w(TAG, "Method Found!");
        methods[i].invoke("methodName", "string data", 0123, 123, "string data");
    }
}

I want to make it simple with the below code, but it keeps on telling me that the method was not found. ERROR: java.lang.NoSuchMethodException: methodName []

Class c=Class.forName("com.package.MainActivity");
Method m=c.getMethod("methodName");
Object t = c.newInstance();
Object o= m.invoke(t,"string data", 0123, 123, "string data");

I also tried the below code, but it's also not working and the error says : Can't create handler inside thread that has not called Looper.prepare()

Object carObj = Class.forName("com.package.MainActivity").newInstance();
Method method = carObj.getClass().getMethod("methodName");
method.invoke(method, "string data", 0123, 123, "string data");

I am doing all the above code with the below:

Main Class where the method should be call

public class MainActivity extends AppCompatActivity {

    static MyHelper myHelper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btnStart = (Button)findViewById(R.id.btnStart);

        myHelper = new MyHelper(this);
        myHelper.StartThread();

    }

    /*The Method to Invoke*/
    public static void methodName(String data1, int var1, int var2, String data2){
        //some code here...
    }
}

Helper Class for starting Thread including some of my functions

public class MyHelper{
    private Context context;
    public MyHelper(Context context){
       this.context = context;
    }

    public static void StartThread(){
        TheThread theThread = new TheThread(context);
        Thread t = new Thread(theThread);
        t.start();
    }

    //Some code below
}

The Thread where the method should be invoke

public class TheThread implements Runnable{

    private Context context;

    public TheThread(Context context){
       this.context = context;
    }

    @Override
    public void run(){
        try{

            String className = context.getClass().getName(); //The Class Name including its package.

            //First Attempt
            /*Class myclass = Class.forName(className);
            Method[] methods = myclass.getMethods();
            for (int i = 0; i < methods.length; i++) {
                if (methods[i].getName().equals("methodName")) {
                    Log.w(TAG, "Method Found!");
                    methods[i].invoke("methodName", "string data", 0123, 123, "string data");
                }
            } */

            //Second Attempt
            /*Class c=Class.forName(className);
            Method m=c.getMethod("methodName");
            Object t = c.newInstance();
            Object o= m.invoke(t,"string data", 0123, 123, "string data");*/

            //Third attempt
            /*Object carObj = Class.forName(className).newInstance();
            Method method = carObj.getClass().getMethod("methodName");
            method.invoke(method, "string data", 0123, 123, "string data"); */

        }catch(Exception e){
            Log.e("Invoke Error","Unable to invoke method => " + e.toString());
        }
    }
}

I am currently stock and totally confused about invoke method. How can I solve this? I don't prefer the first attempt because it needs to list all the methods from a class just to check the specific method before invoking it.





Aucun commentaire:

Enregistrer un commentaire