【发布时间】:2012-01-10 19:17:18
【问题描述】:
简单的一般性问题。
Webview 已连接到我的 JavascriptInterface 类,它肯定是功能性的。但是,由于 JavascriptInterface 没有扩展 Activity,我似乎无法使用 startActivity(intent); 来意图新的活动;
我应该延长 Activity 吗?是否有另一种方法可以意图进行另一项活动? (在我非常特殊的情况下,我试图使用 YouTube 应用)
package com.privateized.moreprivate;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.util.Log;
import android.widget.Toast;
public class JavaScriptInterface {
Context mContext;
/** Instantiate the interface and set the context */
JavaScriptInterface(Context c) {
mContext = c;
}
/** Show a toast from the web page */
public void showToast(String toast) {
Toast.makeText(mContext, toast, Toast.LENGTH_SHORT).show();
}
///// vvvvv This no Worky vvvvv Just crashes ////////
public void YouTube(String id) {
Log.d("JS", "Launching YouTube:" + id);
Activity activity = new Activity();
String videoId = id;
Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse("vnd.youtube:"+videoId));
i.putExtra("VIDEO_ID", videoId);
Log.d("JS", "Starting Activity");
activity.startActivity(i);
}
}
【问题讨论】:
-
Activity activity = new Activity();- 永远不要那样做。 AndroidActivity类永远不会以这种方式直接实例化。 sgarman 的答案应该可以满足您的需要。
标签: java javascript android android-intent