从JavascriptInterface启动Android活动

3

一个简单的一般性问题。

Webview已经被连接到我的JavascriptInterface类,它肯定是可以使用的。然而,由于JavascriptInterface没有扩展Activity,我似乎无法使用startActivity(intent)转到新的Activity。

我应该扩展Activity吗?是否有其他方法可以跳转到另一个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(); - дёҚиҰҒиҝҷж ·еҒҡгҖӮAndroidзҡ„Activityзұ»д»ҺжқҘжІЎжңүж„Ҹе‘ізқҖд»ҘйӮЈз§Қж–№ејҸзӣҙжҺҘе®һдҫӢеҢ–гҖӮsgarmanзҡ„зӯ”жЎҲеә”иҜҘйҖӮеҗҲдҪ зҡ„йңҖжұӮгҖӮ - Squonk
2个回答

5

只需使用mContext.startActivity(i)

不需要创建Activity,也不需要扩展Activity,你只需要一个已经存储的Context引用。


看起来很奇怪,因为那是我尝试的第一件事,但Eclipse在自动完成列表中未能列出startActivity,并且当我手动输入时会告诉我它无效,随后不允许我编译。所以我就离题了哈哈。不过现在似乎可以工作了,也许Eclipse只需要重新启动 :x谢谢大家! - RedactedProfile
1
我是IntelliJ IDEA的忠实支持者,你可以在空闲时间里试试看。它有非常好的Android集成功能。 - sgarman

0

我有一个解决方案,可以在具有Fragments resp. SherlockFragments的情况下启动新的意图,我为这个问题苦苦挣扎了将近一个小时,但是现在你可以看到:

打开WebView的Fragment:

public class MyWebViewFragment extends SherlockFragment {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    super.onCreateView(inflater, container, savedInstanceState);

    View v = inflater.inflate(R.layout.webviewfragment, container, false);
    WebView myWebView = (WebView) v.findViewById(R.id.webviewcontainer);
    myWebView.getSettings().setJavaScriptEnabled(true);
    myWebView.addJavascriptInterface(new WebAppInterface(getActivity()),
            "Android");
    myWebView.loadUrl("file:///android_asset/javascript/index.html");

    return v;
}

Javascript接口: 我正在使用API Level 17,所以我使用@JavascriptInterface。在L16上,如果没有注释,它将无法使用id,但我不知道为什么...
public class WebAppInterface {
Context mContext;

/** Instantiate the interface and set the context */
WebAppInterface(Context myWebViewFragment) {
    mContext = myWebViewFragment;
}


@JavascriptInterface
public void playFragmentActivity(long id) {

    Intent intent = new Intent(mContext, FragmentActivity.class);
    intent.putExtra(FragmentActivity.EXTRA_ID,Id);
    mContext.startActivity(intent);
}

}

HTML

<body>
<div>
    <input type="button" value="Start X FragmentActivity"
        onClick="changeFragmentActivity(183216076724928)" />
</div>

<script type="text/javascript">

    function changeFragmentActivity(id) {
        Android.playFragmentActivity(id);
    }
</script>
</body>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接