android 从 JavascriptInterface 启动Activity

2024-02-04

简单的一般问题。

Webview 连接到我的 JavascriptInterface 类,并且它肯定是有用的。但是,因为 JavascriptInterface 不扩展 Activity,所以我似乎无法使用 startActivity(intent); Intent 到新活动;

我应该延长活动时间吗?是否有其他方法来意图进行另一项活动? (在我的非常特殊的情况下,我尝试使用 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);
    }

}

只需使用mContext.startActivity(i);

无需创建 Activity 也无需扩展 Activity,您只需要对已存储的 Context 的引用。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

android 从 JavascriptInterface 启动Activity 的相关文章

随机推荐