根据给定的字符串启动活动?

时间:2016-05-01 18:49:56

标签: android android-studio

    private void sendNotification(String message, String uri) {
    Intent intent = new Intent(this, xxx.class);

如何更改“xxx.class”以使用变量uri并启动此活动?我正在使用webview发送带有变量“message”的GCM推送通知,并且还想打开变量活动。

如果我这样做,我在Android Studio中“无法解析构造函数”:

    Intent intent = new Intent(this, uri);

2 个答案:

答案 0 :(得分:2)

如果我理解你的错误,你可以使用Class.forName()方法来获得你需要的东西。

所以它会是:

Intent intent = new Intent(this, Class.forName(xxx));

但请勿忘记用try/catch阻止它。

答案 1 :(得分:2)

调用Class.forName()时,应使用不带扩展名的类名。如果你的班级是MyTest并且在mytestpackage.tests包中,那么你应该使用:

Class.forName("mytestpackage.tests.MyTest")
相关问题