2012-12-21 38 views
0

如何根據來自javascript的調用向通知欄添加通知?如何根據來自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); 
    } 
} 

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

我的JavaScript正確地與Android應用程序,因爲我看到了土司接口,如何將通知添加到通知欄?我對android非常陌生,並且不知道如何從我在互聯網上找到的示例轉到我的應用中的有用狀態。

謝謝!

+1

我會從這裏開始:http://developer.android.com/guide/topics/ui/notifiers/notifications.html,並將其納入'showToast()'方法。 – Eric

回答

0

也請看Notifying the User。您應該可以用通知來替換showToast。但是,您還應該查看JavaScriptInterface的參考文檔; Java對象需要用@JavascriptInterface進行註釋。

+0

喬:感謝您的意見,因爲我對此很新,您能分享一些有用的代碼嗎? – MrM