最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

java - android startActivity from JavascriptInterface - Stack Overflow

programmeradmin4浏览0评论

Simple general question.

Webview is hooked up to my JavascriptInterface class and it is most certainly functional. However, because JavascriptInterface does not extend Activity, I cannot seem to Intent to a new activity using startActivity(intent);

Should I extend Activity? Is there another method to intent to another activity? (in my very special case, im trying to intent to the YouTube app)

package .privateized.moreprivate;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.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);
    }

}

Simple general question.

Webview is hooked up to my JavascriptInterface class and it is most certainly functional. However, because JavascriptInterface does not extend Activity, I cannot seem to Intent to a new activity using startActivity(intent);

Should I extend Activity? Is there another method to intent to another activity? (in my very special case, im trying to intent to the YouTube app)

package .privateized.moreprivate;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.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);
    }

}
Share Improve this question asked Jan 10, 2012 at 19:17 RedactedProfileRedactedProfile 2,8086 gold badges35 silver badges55 bronze badges 1
  • Activity activity = new Activity(); - don't ever do that. The Android Activity class is never meant to be directly instantiated in that way. The answer from sgarman should work for what you need. – Squonk Commented Jan 10, 2012 at 19:25
Add a ment  | 

2 Answers 2

Reset to default 6

Just use mContext.startActivity(i);

No need to ever create Activity and no need to extend Activity, you just need a reference to Context which you already have stored.

So i've a solution to start a new intent if have Fragments resp. SherlockFragments, i was struggling with this problem for almost an hour, but here u go:

Fragment opening the WebView:

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;
}

Javascriptinterface: I'm working on API Level 17, thats why i use the @JavascriptInterface. On id wouldnt work with L16 without the annotation, but i dont got why...

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>
发布评论

评论列表(0)

  1. 暂无评论