Android webview Notification from Javascript



如何使javascriptinterface内部的showToast调用CreateNotification?

下面是我的代码:

CheckInventoryActivity.java

package com.CheckInventory;;

import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
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 CheckInventory;Activity 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());
    }
    public void OnResume(Bundle savedInstanceState) {
        super.onResume();
         CancelNotification();
    }

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

    public void CreateNotification()
    {
        Toast.makeText(getApplicationContext(),"Notifying", Toast.LENGTH_LONG).show();
        String ns = Context.NOTIFICATION_SERVICE;
        NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);
        int icon = R.drawable.ic_launcher;              // icon from resources
        long when = System.currentTimeMillis();         // notification time
        Context context = getApplicationContext();      // application Context
        CharSequence tickerText = "Chess";              // ticker-text
        CharSequence contentTitle = "CheckInventory;";      // message title
        CharSequence contentText = "You have an action to take";      // message text
        Intent notificationIntent = new Intent(CheckInventory;Activity.this, CheckInventory;Activity.class);
        PendingIntent contentIntent = PendingIntent.getActivity(CheckInventory;Activity.this, 0, notificationIntent, 0);
        Notification notification = new Notification(icon, tickerText, when);
        notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
        notification.flags |= Notification.FLAG_ONLY_ALERT_ONCE | Notification.FLAG_AUTO_CANCEL;    
        mNotificationManager.notify(1234, notification);
    }
    public void CancelNotification()
    {
        Toast.makeText(getApplicationContext(),"Lets get it on!!", Toast.LENGTH_LONG).show();
        String ns = Context.NOTIFICATION_SERVICE;
        NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);
        mNotificationManager.cancel(1234);
    }
}

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

我是新的Java和Android工作,当我尝试调用该方法时,它给了我一个错误,因为它不是静态的。底线我正在寻找javascript接口来创建一个通知。

由于我是新人,我需要一个与我的问题有直接关系的示例代码。

感谢您的帮助!

Thx

除了CheckInventory;Activity不是一个有效的类名这一事实之外,其余的似乎都是对的。如果通知用户要在你的活动中完成,这是一种方法。在你的JavaScriptInterfaceshowToast()调用CreateNotification如下:

((CheckInventoryActivity)mContext).CreateNotification();

这将转换你的mContext变量,所以它是一个CheckInventoryActivity,然后调用你的CreateNotification方法。

如果mContext不是总是 CheckInventoryActivity的一个实例,那么你需要这样做:

 if (mContext instanceof CheckInventoryActivity){
      ((CheckInventoryActivity)mContext).CreateNotification();
    }

然而,我不明白为什么你不把你的通知代码直接在JavaScriptInterface。您有一个Context变量(mContext)和Notification &intent需要一个Context变量作为参数之一。简单地说,您似乎拥有必要的工具来显示来自非活动类的通知。只要确保将getSystemService(ns);调用为context.getSystemService(ns);,并删除所有getApplicationContext()方法调用。

最新更新