简体   繁体   中英

Android Studio Java runnable not running

I do not know why, but my runnable is not working. I am very new to programming. Please help me.

@Override
public void onCreate() {

    Toast.makeText(ApplicationContextProvider.getContext(), "Service Template", Toast.LENGTH_SHORT).show();

    handler = new Handler();
    runnable = new Runnable() {
        public void run() {
            Toast.makeText(ApplicationContextProvider.getContext(), "Runnable", Toast.LENGTH_LONG).show();


            Calendar calendar = Calendar.getInstance();
            int day = calendar.get(Calendar.DAY_OF_WEEK);
            if (day == Calendar.WEDNESDAY){
                Toast.makeText(ApplicationContextProvider.getContext(), "Wednesday", Toast.LENGTH_LONG).show();


                WindowManager.LayoutParams params = new WindowManager.LayoutParams(WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY, WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE, PixelFormat.TRANSLUCENT);
                params.gravity = Gravity.TOP | Gravity.LEFT;
                params.x = 0;
                params.y = 0;
                params.width = 0;
                params.height = 0;

                WindowManager windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);

                LinearLayout view = new LinearLayout(context);
                view.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT));

                WebView wv = new WebView(context);
                String newUA= "Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.4) Gecko/20100101 Firefox/4.0";
                wv.getSettings().setJavaScriptEnabled(true);
                wv.getSettings().setUserAgentString(newUA);
                wv.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT));
                view.addView(wv);
                wv.loadUrl(url1);
                wv.setWebViewClient(new HelloWebViewClient());
                windowManager.addView(view, params);
            }
            handler.postDelayed(runnable, 1000*5);
        }
    };
}

As you can see, I have a toast set up to let me know when the runnable is running. The toast that shows service template shows up, but the runnable toast does not.

In the main class before this class, I use a lot of shared preferences, but I do not know if this could be the cause of the problem. My logCat also does not change when I press the button to go to this class.

When I close the app, my logcat writes: W/IInputConnectionWrapper: showStatusIcon on inactive InputConnection

I am trying to make a web bot of sorts.

Please help me.

Your postDelay is in the wrong position. Try move it outside the run. Like so.

@Override public void onCreate() {
Toast.makeText(ApplicationContextProvider.getContext(), "Service Template", Toast.LENGTH_SHORT).show();

handler = new Handler();
runnable = new Runnable() {
    public void run() {
        Toast.makeText(ApplicationContextProvider.getContext(), "Runnable", Toast.LENGTH_LONG).show();


        Calendar calendar = Calendar.getInstance();
        int day = calendar.get(Calendar.DAY_OF_WEEK);
        if (day == Calendar.WEDNESDAY){
            Toast.makeText(ApplicationContextProvider.getContext(), "Wednesday", Toast.LENGTH_LONG).show();


            WindowManager.LayoutParams params = new WindowManager.LayoutParams(WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY, WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE, PixelFormat.TRANSLUCENT);
            params.gravity = Gravity.TOP | Gravity.LEFT;
            params.x = 0;
            params.y = 0;
            params.width = 0;
            params.height = 0;

            WindowManager windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);

            LinearLayout view = new LinearLayout(context);
            view.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT));

            WebView wv = new WebView(context);
            String newUA= "Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.4) Gecko/20100101 Firefox/4.0";
            wv.getSettings().setJavaScriptEnabled(true);
            wv.getSettings().setUserAgentString(newUA);
            wv.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT));
            view.addView(wv);
            wv.loadUrl(url1);
            wv.setWebViewClient(new HelloWebViewClient());
            windowManager.addView(view, params);
        }
    }
};
        handler.postDelayed(runnable, 1000*5);}

this will not run because you never run it

you are calling handler.postDelayed(runnable, 1000*5); inside the run method which never runs.

move the handler.postDelayed(runnable, 1000*5);

outside run() method

Edit:

using handler.postDelayed(runnable, 1000*5); insed the run() method is used to recursively run runnables .but it needs to be started first.

The problem is that you never actually post the handler to the message queue. Your run method is right. Assuming that you are in an Activity, post your handler in the onPostResume method.

@Override
protected void onPostResume() {
    super.onPostResume();
    ...
    handler.post(runnable);
    ...
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM