I am creating a home automation app that has allows plugin views. I have been able to create a class as a sample plugin in a separate project (apk):
public class MyTestClass_IRDroidUIPlugIn extends Button implements IRDroidInterface{
Context mContext;
public MyTestClass_IRDroidUIPlugIn(Context context) {
super(context);
mContext = context;
setText("I was loaded dynamically! (1)");
setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// how do I show the dialog from here?
Activity.showDialog(1);
}}
);
}
public Dialog buildConfigDialog(int ID){
AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
builder.setMessage("Click the Button...(1)")
.setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.dismiss();
}
});
return builder.create();
}
}
I can load this class at run time and create an instance of it:
try {
final File filesDir = this.getFilesDir();
final File tmpDir = getDir("dex", 0);
final DexClassLoader classloader = new DexClassLoader( filesDir.getAbsolutePath()+"/testloadclass.apk",
tmpDir.getAbsolutePath(),
null, this.getClass().getClassLoader());
final Class<View> classToLoad =
(Class<View>) classloader.loadClass("com.strutton.android.testloadclass.MyTestClass_IRDroidUIPlugIn");
mybutton = (View) classToLoad.getDeclaredConstructor(Context.class).newInstance(this);
mybutton.setId(2);
main.addView((View)mybutton);
} catch (Exception e) {
e.printStackTrace();
}
setContentView(main);
}
protected Dialog onCreateDialog(int id) {
switch (id) {
case 1:
return ((IRDroidInterface) mybutton).buildConfigDialog(id);
}
return null;
}
I want the plugin to be able to show a configuration dialog. Is there a way I can pass the Activity object to this class so it can use .showDialog(ID). This would be ideal so that the dialog life cycle can be managed properly.
Thanks in advance.
Maybe I’m missing something, but why can’t you do something like this?
When you’re creating this class, you’re already passing
Activityinto this class – so just use it.