0

How can I make the showToast inside the JavaScriptInteface call the CreateNotification?

Here is my code:

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

I'm new at working with Java and Android, at when I try to call the method, it gives me an error because it is not static. Bottom line I am looking for the JavaScriptInterface to create a Notification.

Since I am this new, I would need a sample code that has direct relation to my problem.

Your help is appreciated!!

Thx

MrM
  • 85
  • 1
  • 3
  • 12

1 Answers1

1

Besides the fact that CheckInventory;Activity is not a valid class name, the rest seems to be alright. If notifying the user has to be done in your Activity, this is one approach. In your JavaScriptInterface's showToast() call CreateNotification as so:

((CheckInventoryActivity)mContext).CreateNotification();

This casts your mContext variable so it is a CheckInventoryActivity then calls your CreateNotification method.

If mContext is not always an instance of CheckInventoryActivity then you will need to do something like this:

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

However, I don't understand why you don't put your notification code directly in JavaScriptInterface. You have a Context variable (mContext), and the Notification & Intents require a Context variable as one of the arguments. Simply put, you seem to have the necessary tools to display a Notification right from that Non-Activity Class. Just make sure to call getSystemService(ns); as context.getSystemService(ns); and to remove all the getApplicationContext() method calls.

A--C
  • 36,351
  • 10
  • 106
  • 92
  • A--C: Great answer thank you! I was able to do this with the ((CheckInventoryActivity)mContext).CreateNotification(); I am just curious as to why when I click on the notification, it shows as if the app is re-starting and not just showing the page as it was left. – MrM Dec 23 '12 at 04:30
  • The Activity's `onCreate()` method is called again. [This](http://stackoverflow.com/questions/5605207/android-notification-restarts-app-but-want-to-resume) may be of help if you want to change this behaviour. – A--C Dec 23 '12 at 04:38
  • A--C How do I change this behavior? – MrM Dec 24 '12 at 15:11
  • @MrM as the link says, you may want to look at adding `android:launchMode="singleInstance"` to the manifest. Unfortunately I can't be of much help as I haven't used these flags. – A--C Dec 24 '12 at 15:17
  • I tried the singleinstance, but the behavior stayed the same. I will continue working thru this. Sincerely, Thank you for your help. – MrM Dec 24 '12 at 18:22