Android Studio Java无法运行

时间:2018-02-07 22:40:36

标签: java android multithreading

我不知道为什么,但我的可运行不起作用。我是编程新手。请帮帮我。

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

正如您所看到的,我设置了一个Toast设置,以便在runnable运行时让我知道。显示服务模板的Toast显示,但runnable toast不显示。

在本课程之前的主要课程中,我使用了很多共享首选项,但我不知道这是否可能是导致问题的原因。当我按下按钮进入这个班级时,我的logCat也没有改变。

当我关闭应用程序时,我的logcat会写:W/IInputConnectionWrapper: showStatusIcon on inactive InputConnection

我正在努力制作各种各样的网络机器人。

请帮帮我。

3 个答案:

答案 0 :(得分:1)

你的postDelay位置错误。尝试将其移出运行范围。像这样。

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

答案 1 :(得分:1)

这不会运行,因为你从未运行它

你在永远不会运行的run方法中调用handler.postDelayed(runnable, 1000*5);

移动handler.postDelayed(runnable, 1000*5);

run()方法

编辑:

使用handler.postDelayed(runnable, 1000*5); insed run()方法用于递归运行runnables。但它需要先启动。

答案 2 :(得分:1)

问题是您实际上从未将处理程序发布到消息队列中。你的跑步方法是对的。假设您在Activity中,请在onPostResume方法中发布处理程序。

library(rvest)
url <- "https://finance.yahoo.com/quote/FNDB?p=FNDB"
#Reading the HTML code from the website
webpage <- read_html(url)
code <- "title"
result <- html_nodes(webpage,code)
result