android 管理類,android 線程管理類

1. 延時任務以及取消延時的應用場景:實時搜索,文字內容變化時候不發送請求,當停止變化指定時間后再發送請求

2. 使用方法:

// 回到主線程

MainThreadHelper.runOnUIthread(new Runnable() {

@Override

public void run() {

// pushActivity();

}

});

// 延時調用以及取消延遲

MainThreadHelper.runDelayedCall(300, new MainThreadHelper.DelayedCallListener() {

@Override

public void onClick() {

// animalHiddenView();

}

});

3. 實現類代碼:

import android.os.Handler;

import android.os.Looper;

import android.util.Log;

/**

* Created by jing on 17/5/19.

* 線程管理~

*/

public class MainThreadHelper{

private MainThreadHelper(){ super(); }

// 單利模式

private static MainThreadHelper mainThreadHelper = new MainThreadHelper();

/**

* 回到在主線程

**/

private MainThreadComplate mainThreadComplate = new MainThreadComplate();

public static void runOnUIthread(Runnable runnable){

mainThreadHelper.mainThreadComplate.runOnUIthread(runnable);

}

/**

* 打印當前線程

* */

public static void logCurrentThread(){

String string = Thread.currentThread().toString();

Log.d("當前線程",string);

}

/** 延時任務以及取消延時 */

private DelayedCallComplate delayedCallComplate = new DelayedCallComplate();

public interface DelayedCallListener{

void onClick();

}

public static void runDelayedCall(long delayMillis,DelayedCallListener listener){

mainThreadHelper.delayedCallComplate.runDelayedCall(delayMillis,listener);

}

}

/** 主線程相關類 */

class MainThreadComplate{

private Handler handler = new Handler(Looper.getMainLooper());

/** 回到在主線程 */

public void runOnUIthread(Runnable runnable){

if (Looper.myLooper() == Looper.getMainLooper()){

runnable.run();

return;

}

handler.post(runnable);

}

}

/** 延時任務以及取消延時類 */

class DelayedCallComplate implements Runnable{

private MainThreadHelper.DelayedCallListener listener = null;

private Handler handler = new Handler(Looper.getMainLooper());

public void runDelayedCall(long delayMillis,MainThreadHelper.DelayedCallListener listener){

this.listener = listener;

handler.removeCallbacks(this);

handler.postDelayed(this,delayMillis);

}

@Override

public void run() {

if (listener == null) return;

listener.onClick();

}

}

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/458857.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/458857.shtml
英文地址,請注明出處:http://en.pswp.cn/news/458857.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

計算一行文本的高度

計算一行文本的高度 說明 有時候我們需要知道指定的幾行文本的高度,此工具用于解決此種問題。 源碼 // // NSStringLabelWidthAndHeight.h // ZiPeiYi // // Created by YouXianMing on 15/12/9. // Copyright © 2015年 YouXianMing. All rights reserved.…

python筆記-python編程優化:常用原則和技術介紹

本人翻譯自《Exper Python Programming》 Premature optimization is the root of all evil in programming -Donald Knuth 優化的三原則 讓它跑起來先一個非常常見的錯誤就是在編寫代碼之初我們就開始對代碼進行優化。讓人傷心的是這通常做的都是無用功,很多軟…

C++成員函數重載、覆蓋和隱藏的區別

*************************************************** 更多精彩&#xff0c;歡迎進入&#xff1a;http://shop115376623.taobao.com *************************************************** C成員函數重載、覆蓋和隱藏的區別class Base{public:void Walk(int x){ cout <<…

用fputc()函數以字符串形式寫入字符到磁盤文件

2019獨角獸企業重金招聘Python工程師標準>>> #include <stdio.h> #include <stdlib.h>int main(){FILE *fp;char ch;if((fpfopen("testfile", "a")) NULL){fprintf(stderr, "Error opening file.\n",fp);exit(1);}print…

android 浮動文字提示,Android實現自由拖動并顯示文字的懸浮框

項目中需要實現一個狀態顯示的懸浮框&#xff0c;要求可以設置兩種模式&#xff1a;拖動模式和不可拖動模式。實現效果圖如下&#xff1a;實現步驟&#xff1a;1.首先要設置該懸浮框的基本屬性&#xff1a;/*** 顯示彈出框** param context*/SuppressWarnings("WrongConst…

Android4.2.2的Stagefright維護編解碼器的數據流

這里是他們自己的源代碼閱讀點滴總結屬性&#xff0c;轉請注明出處&#xff0c;謝謝。歡迎和大家分享。qq:1037701636 email:gzzaigcn2012gmail.comAndroid源代碼版本號Version&#xff1a;4.2.2; 硬件平臺 全志A31前沿&#xff1a;在前面的博文中&#xff0c;基本提到的是stag…

PHP的安裝

PHP的環境也是諸多服務器軟件的必要因素之一&#xff0c;它是一個HTML內嵌式語言&#xff0c;在服務器端執行。由于PHP的開源高效化平臺&#xff0c;所以搭建一個php環境是一個運維工程師必備的能力。現在lamp也有類似lnmp.org那種一鍵安裝包&#xff0c;地址是http://yumlamp.…

android 橫向鋪滿,Android開發全程記錄(八)——設置ImageView顯示的圖片鋪滿全屏(適應魅族等不常見屏幕比例)...

為適應不同屏幕的手機&#xff0c;ImageView顯示的圖片可能不鋪滿屏幕&#xff0c;如果定高的話&#xff0c;兩邊可能會出現空白。魅族手機就會有這種情況&#xff0c;在其他手機里顯示正常&#xff0c;在魅族手機里顯示&#xff0c;圖片左右兩邊會出現空白&#xff0c;為解決這…

tihs 關鍵字

//this關鍵詞/*調用類中的屬性 調用類中的方法或構造方法 調用當前對象&#xff0c;調用自己的方法&#xff0c;可以省略。 */ //http://blog.sina.com.cn/s/blog_71f6c1980100wtj4.html//this指當前對象自己public class Google{String s"hello";public Google(Stri…

良好的編程習慣

*************************************************** 更多精彩&#xff0c;歡迎進入&#xff1a;http://shop115376623.taobao.com *************************************************** 良好的編程習慣良好的習慣對于人的成長是非常重要的&#xff0c;良好的編程習慣對于我…

ntfs for mac使用注意事項有哪些?

2019獨角獸企業重金招聘Python工程師標準>>> mac的用戶有很多&#xff0c;一些用戶朋友會發現自己的電腦是無法讀寫ntfs驅動器的。而ntfs驅動器又是一種常用的驅動器。面對這種情況我們可以選擇用NTFS for Mac軟件來幫助我們&#xff0c;它可以讀寫ntfs驅動器&#…

android音樂播放器文章,Android復習09【內容提供者、音樂播放器】

目 錄PersonCpPersonCp.javainsert()ContentObserver音樂播放器1、添加讀寫權限1.1、動態權限授予(調用封裝好的方法)2、獲取音樂文件(MainActivity.java)2、Music.java(實體類)申請訪問SD卡的權限設置適配器下拉刷新PersonCpPersonCp.javapackage cn.wangzg.personcp;import a…

程序員的業余項目

程序員的業余項目&#xff0c;我們也叫它 side project。 前幾天&#xff0c;100offer 發起了一場活動叫 <尋找實干和堅持的技術力量>&#xff0c;他們是這么說的&#xff1a; 世界在被代碼改變著&#xff0c;而我們在創造著代碼。 僅僅是因為好玩&#xff0c;他開發了…

C語言的數組名和對數組名取地址

*************************************************** 更多精彩&#xff0c;歡迎進入&#xff1a;http://shop115376623.taobao.com *************************************************** 相信不少的C語言初學者都知道&#xff0c;數組名相當于指針&#xff0c;指向數組的首地…

小米 android 8,小米華為們誰最良心?10大手機廠商安卓8.0升級情況盤點

3月8日&#xff0c;谷歌放出了首個安卓9.0開發者預覽版的固件包&#xff0c;不出意外的話&#xff0c;它的正式版會在今年正式亮相。但對廣大安卓用戶來說&#xff0c;想要立刻用上最新系統并非易事。目前來說&#xff0c;安卓碎片化問題依然嚴重&#xff0c;我們不妨現實點&am…

窺探Swift之數組安全索引與數組切片

在Swift中的數組和字典中下標是非常常見的&#xff0c;數組可以通過索引下標進行元素的查詢&#xff0c;字典可以通過鍵下標來獲取相應的值。在使用數組時&#xff0c;一個常見的致命錯誤就是數組越界。如果在你的應用程序中數組越界了&#xff0c;那么對不起&#xff0c;如果由…

大小端模式的快速判斷方法

*************************************************** 更多精彩&#xff0c;歡迎進入&#xff1a;http://shop115376623.taobao.com *************************************************** 大小端的問題剖析&#xff1a; 嵌 入式系統開發者應該對Little-endian和Big-endian模…

【RAC】How to Proceed from Failed 11gR2 CRS Installation

Applies to: [ID 942166.1] Oracle Server – Enterprise Edition – Version: 11.2.0.1 to 11.2.0.2 – Release: 11.2 to 11.2 Generic UNIX Generic Linux Goal This goal of this note is to provide steps to proceed from failed 11gR2 Grid Infrastructure installat…

WinForm支持拖拽效果

有一個MSDN客戶提問在WinForm中如何實現拖拽效果——比如在WinForm中有一個Button&#xff0c;我要實現的效果是拖拽這個Button到目標位置后生成一個該控件的副本。 其實這個操作主要分成三步走&#xff1a; 1&#xff09;確定被拖拽的對象&#xff1a; 這里是Button&#xff0…

win7 64位出現桌面右鍵鼠標顯示忙碌

*************************************************** 更多精彩&#xff0c;歡迎進入&#xff1a;http://shop115376623.taobao.com *************************************************** 將下面綠色內容復制到txt文本中&#xff0c;然后另存為1.bat 雙擊運行即可 【針對64位…