forked from Blankj/AndroidUtilCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
175 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
app/src/main/java/com/blankj/androidutilcode/activities/HandlerActivity.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package com.blankj.androidutilcode.activities; | ||
|
||
import android.app.Activity; | ||
import android.os.Bundle; | ||
import android.os.Message; | ||
import android.view.View; | ||
import android.widget.TextView; | ||
|
||
import com.blankj.androidutilcode.R; | ||
import com.blankj.utilcode.utils.DeviceUtils; | ||
import com.blankj.utilcode.utils.HandlerUtils; | ||
import com.blankj.utilcode.utils.LogUtils; | ||
|
||
/** | ||
* <pre> | ||
* author: Blankj | ||
* blog : http://blankj.com | ||
* time : 2016/9/27 | ||
* desc : Device工具类测试 | ||
* </pre> | ||
*/ | ||
public class HandlerActivity extends Activity | ||
implements View.OnClickListener, HandlerUtils.OnReceiveMessageListener { | ||
|
||
private TextView tvAboutHandler0; | ||
private TextView tvAboutHandler1; | ||
HandlerUtils.HandlerHolder handlerHolder; | ||
|
||
@Override | ||
protected void onCreate(Bundle savedInstanceState) { | ||
super.onCreate(savedInstanceState); | ||
setContentView(R.layout.activity_handler); | ||
|
||
tvAboutHandler0 = (TextView) findViewById(R.id.tv_about_handler0); | ||
tvAboutHandler1 = (TextView) findViewById(R.id.tv_about_handler1); | ||
findViewById(R.id.btn_send_msg_after_3s).setOnClickListener(this); | ||
|
||
handlerHolder = new HandlerUtils.HandlerHolder(this); | ||
} | ||
|
||
@Override | ||
public void onClick(View view) { | ||
switch (view.getId()) { | ||
case R.id.btn_send_msg_after_3s: | ||
handlerHolder.sendEmptyMessageDelayed(0, 30000); | ||
break; | ||
} | ||
} | ||
|
||
@Override | ||
public void handlerMessage(Message msg) { | ||
tvAboutHandler1.setText("get_msg_after_3s"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -36,6 +36,5 @@ | |
android:gravity="center" | ||
/> | ||
|
||
|
||
</LinearLayout> | ||
</ScrollView> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<ScrollView | ||
xmlns:android="http://schemas.android.com/apk/res/android" | ||
android:layout_width="match_parent" | ||
android:layout_height="match_parent"> | ||
<LinearLayout | ||
android:layout_width="match_parent" | ||
android:layout_height="wrap_content" | ||
android:gravity="center_horizontal" | ||
android:orientation="vertical" | ||
android:padding="@dimen/spacing_small"> | ||
|
||
|
||
<Button | ||
android:id="@+id/btn_send_msg_after_3s" | ||
style="@style/BtnFont" | ||
android:layout_width="match_parent" | ||
android:layout_height="wrap_content" | ||
android:text="@string/handler.send_msg_after_3s" | ||
/> | ||
|
||
<TextView | ||
android:id="@+id/tv_about_handler0" | ||
style="@style/Font" | ||
android:layout_width="match_parent" | ||
android:layout_height="wrap_content" | ||
android:gravity="center" | ||
/> | ||
|
||
<TextView | ||
android:id="@+id/tv_about_handler1" | ||
style="@style/Font" | ||
android:layout_width="match_parent" | ||
android:layout_height="wrap_content" | ||
android:gravity="center" | ||
/> | ||
|
||
|
||
</LinearLayout> | ||
</ScrollView> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
utilcode/src/main/java/com/blankj/utilcode/utils/HandlerUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package com.blankj.utilcode.utils; | ||
|
||
import android.os.Handler; | ||
import android.os.Message; | ||
|
||
import java.lang.ref.WeakReference; | ||
|
||
/** | ||
* <pre> | ||
* author: Blankj | ||
* blog : http://blankj.com | ||
* time : 16/11/01 | ||
* desc : Handler相关工具类 | ||
* </pre> | ||
*/ | ||
public class HandlerUtils { | ||
|
||
private HandlerUtils() { | ||
throw new UnsupportedOperationException("u can't instantiate me..."); | ||
} | ||
|
||
public static class HandlerHolder extends Handler { | ||
WeakReference<OnReceiveMessageListener> mListenerWeakReference; | ||
|
||
/** | ||
* @param listener 必读:推荐在Activity或者Activity内部持有类中实现该接口,不要使用匿名类,可能会被GC | ||
*/ | ||
public HandlerHolder(OnReceiveMessageListener listener) { | ||
mListenerWeakReference = new WeakReference<>(listener); | ||
} | ||
|
||
@Override | ||
public void handleMessage(Message msg) { | ||
if (mListenerWeakReference != null && mListenerWeakReference.get() != null) { | ||
mListenerWeakReference.get().handlerMessage(msg); | ||
} | ||
} | ||
} | ||
|
||
public interface OnReceiveMessageListener { | ||
void handlerMessage(Message msg); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters