0

如何根据来自 javascript 的调用向通知栏添加通知?

我有这个:

package com.CheckInventory;

import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.WindowManager;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebStorage;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;

@SuppressWarnings("unused")
public class CheckInventoryActivity extends Activity {
    WebView webview;
    String username; 
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
        setContentView(R.layout.main);
        webview = (WebView) findViewById(R.id.webview);
        WebView webView = (WebView) findViewById(R.id.webview);
        webView.setBackgroundColor(0);
        webView.setBackgroundResource(R.drawable.myimage);
        webView.addJavascriptInterface(new JavaScriptInterface(this), "Android");
        WebSettings webSettings = webview.getSettings();
        webSettings.setLoadWithOverviewMode(true);

        webview.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);

        webSettings.setJavaScriptEnabled(true);
        webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
        webSettings.setDatabasePath("/data/data/"+this.getPackageName()+"/databases/");
        webSettings.setDomStorageEnabled(true);
        webview.setWebChromeClient(new WebChromeClient());
        webview.loadUrl("http://192.168.0.124/android");
        webview.setWebViewClient(new HelloWebViewClient());

    }
    private class HelloWebViewClient extends WebViewClient {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return true;
        }

        @Override
        public void onReceivedError(WebView view, int errorCode, String description, String failingUrl)
        {
            super.onReceivedError(view, errorCode, description, failingUrl);
            webview.loadUrl("file:///android_asset/nodata.html");
            Toast.makeText(getApplicationContext(),"Not online", Toast.LENGTH_LONG).show();
        }
    }

    @Override
    public boolean onKeyDown (int keyCode, KeyEvent event) {
        if((keyCode == KeyEvent.KEYCODE_BACK) && webview.canGoBack()){
            //webview.goBack();
            return true;
        }

        return super.onKeyDown(keyCode, event);
    }
}

Javascript接口.Java

package com.CheckInventory;
import android.widget.Toast;
import android.content.Context;

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_LONG).show();
    }
}

我的 Javascript 与 android 应用程序正确交互,因为我看到了 toast,如何向通知栏添加通知?我对 android 很陌生,不知道如何从我在互联网上找到的示例转到我的应用程序中的有用状态。

先感谢您!!!

4

1 回答 1

0

另请参阅通知用户。您应该能够用通知替换您的 showToast。但是,您还应该查看 JavaScriptInterface 的参考文档;Java 对象需要使用@JavascriptInterface 进行注释。

于 2012-12-21T23:07:32.087 回答