Android技术知识Android开发

基于Aop的 简单易用线程切换

2018-04-26  本文已影响0人  不听话好孩子

基于Aop的 线程切换实现

先说说用法

@NewThread
void thread(){
//请求网络
}
@NewThread(delay = 1000)
void thread(){
//请求网络
}
@NewThread(delay = 1000,repate = true, period = 10000)
void thread(){
//请求网络
}
   @CancelThread
    private void cancel() {
    }
//调用就会取消了
 cancel();
    @CancelThread("子线程")
    private void cancel() {
    }

    @NewThread(value = "子线程", repate = true, period = 10000,delay = 1000)
    public void jump() {}
    //调用取消value值为"子线程"的任务
     cancel();

UI线程 和NewThread用法相同,就不重复说了。注意要和CancelThread搭配使用,以免内存溢出哦

实现

以下是我的测试用例,基本没发现有什么问题
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        viewById = findViewById(R.id.bt);

        viewById.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showxc("正在获取...");
                if (!start) {
                    start = true;
                    toast();
                }
            }
        });
        findViewById(R.id.stop).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                cancel();
                start=false;
            }
        });
        main();
        viewById.setText("点击请求网络吧,延迟3秒显示结果\n @UI @NewThread都可以设置延迟时间");
        System.out.println("MainActivity2主线程 " + java.lang.Thread.currentThread().getId());
    }

    @CancelThread("子线程")
    private void cancel() {
    }

    @NewThread(value = "子线程", repate = true, period = 10000,delay = 1000)
    @Debounce(value = 2000)
    public void toast() {
        showxc("正在获取...");
        System.out.println("------------------start------------------------toast");
        System.out.println("Thread子线程 " + java.lang.Thread.currentThread().getId());
        try {
            URL url = new URL("https://read.qidian.com/chapter/6wiYP-yFPlNrZK4x-CuJuw2/lLgJiXhXccDgn4SMoDUcDQ2");
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setInstanceFollowRedirects(true);
            if (connection.getResponseCode() == 200) {
                BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
                StringBuffer stringBuffer = new StringBuffer();
                String str;
                while ((str = reader.readLine()) != null) {
                    stringBuffer.append(str);
                }
                viewById.getLayoutParams();
                showxc("开始解析...");
                Spanned text = Html.fromHtml(stringBuffer.toString());
                showxc("完成解析,准备延迟3s展示");
                show(text);
            }
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println("-----------------end-------------------------toast");
    }

    @UI(delay = 3000, value = "主线程")
    private void show(CharSequence s) {
        TextView tv = findViewById(R.id.content);
        tv.setText(s);
    }

    @UI
    private void showxc(String s) {
        aLong = System.currentTimeMillis();
        TextView tv = findViewById(R.id.content);
        tv.setText(s);
    }

    @UI(repate = true, delay = 2000)
    public void main() {
        System.out.println("Thread主线程2 " + java.lang.Thread.currentThread().getId());
    }

    @CancelThread(value = {"主线程", "子线程"})
    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}

最后

上一篇 下一篇

猜你喜欢

热点阅读