【问题标题】:Android webview Notification from Javascript来自Javascript的Android webview通知
【发布时间】:2012-12-23 00:38:40
【问题描述】:

如何让 JavaScriptInteface 中的 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 的新手,当我尝试调用该方法时,它给了我一个错误,因为它不是静态的。底线我正在寻找 JavaScriptInterface 来创建通知。

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

感谢您的帮助!

谢谢

【问题讨论】:

    标签: android notifications


    【解决方案1】:

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

    ((CheckInventoryActivity)mContext).CreateNotification();
    

    这将转换您的 mContext 变量,使其成为 CheckInventoryActivity,然后调用您的 CreateNotification 方法。

    如果mContext 不是始终CheckInventoryActivity 的实例,那么您将需要执行以下操作:

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

    但是,我不明白您为什么不将通知代码直接放在JavaScriptInterface 中。您有一个 Context 变量 (mContext),并且通知和意图需要一个 Context 变量作为参数之一。简而言之,您似乎拥有从该非活动类显示通知的必要工具。只需确保将getSystemService(ns); 称为context.getSystemService(ns); 并删除所有getApplicationContext() 方法调用即可。

    【讨论】:

    • A--C:很好的答案,谢谢!我能够用 ((CheckInventoryActivity)mContext).CreateNotification();我只是很好奇为什么当我点击通知时,它显示的好像应用程序正在重新启动,而不仅仅是显示离开时的页面。
    • 再次调用Activity的onCreate()方法。如果您想改变这种行为,This 可能会有所帮助。
    • A--C 如何改变这种行为?
    • @MrM 如链接所述,您可能希望查看将android:launchMode="singleInstance" 添加到清单中。不幸的是,我没有使用这些标志,因此帮不上什么忙。
    • 我尝试了单实例,但行为保持不变。我会通过这个继续工作。衷心感谢您的帮助。
    猜你喜欢
    • 1970-01-01
    • 2012-03-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-12
    • 2021-11-03
    • 1970-01-01
    相关资源
    最近更新 更多