Skip to content

Instantly share code, notes, and snippets.

@vuhung3990
vuhung3990 / SyncService.java
Created February 13, 2017 08:06
simple IntentService task queue - limit: can't update queue when running
public class SyncService extends IntentService {
public static final String TASK_KEY = "";
public static final int SYNC_CONTACT = 2;
public static final int SYNC_HISTORY = 1;
public static final int SYNC_GROUP = 0;
public SyncService() {
super(null);
}
@vuhung3990
vuhung3990 / MainActivity.java
Created January 30, 2017 05:37
Espresso idle test
public class MainActivity extends AppCompatActivity {
private CountingIdlingResource idlingResource = new CountingIdlingResource("aaaaa");
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final AlertDialog.Builder builder = new AlertDialog.Builder(this);
@vuhung3990
vuhung3990 / RxBus.java
Created January 16, 2017 07:06
Rxjava 2 Bus
/**
* for send object between fragment, activity, service...
* REMEMBER: dispose if not in use to avoid leak memory
*/
public class RxBus<T> {
private static final String PREFIX = "$^%)@";
private static RxBus instance;
private PublishSubject<BusMessage<T>> subject = PublishSubject.create();
@vuhung3990
vuhung3990 / layout.xml
Created December 4, 2016 22:24
custom background state on toggle radio group button
<RadioGroup
android:orientation="horizontal"
android:id="@+id/radio_group"
android:layout_width="match_parent"
android:checkedButton="@+id/bt1"
android:layout_height="wrap_content">
<RadioButton
android:id="@+id/bt1"
android:background="@drawable/toggle_bg"
  1. tránh tạo các đối tượng không cần thiết

  2. sử dụng string buffer (multi thread) hoặc string builder cho việc append chiều chuỗi string

  3. hai mảng a[] b[] tốt hơn so với (a,b)

  4. Nếu bạn không cần truy xuất vào các biến của object. Có thể làm cho phương thức của bạn thành static. Lúc đó việc gọi hàm sẽ nhanh hơn khoảng 15-20%. Việc này cũng có một lợi thế nữa là method của bạn khi được gọi cũng không làm ảnh hưởng đến object

  5. Use Static Final For Constants, const int BIT BETTER than enum

// search text timer
mEdittext = (EditText) findViewById(R.id.editText);
subTextChange = new Subscriber<CharSequence>() {
@Override
public void onCompleted() {
Log.d(TAG, "onCompleted: ");
}
@Override
public void onError(Throwable e) {
@vuhung3990
vuhung3990 / validation.java
Created February 2, 2016 03:21
rxJava combine (form validation)
Observable<CharSequence> observable2 = Observable.create(new Observable.OnSubscribe<CharSequence>() {
@Override
public void call(final Subscriber<? super CharSequence> subscriber) {
txt2.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
@vuhung3990
vuhung3990 / RxBus.java
Created February 2, 2016 02:27
RxBus replace of eventbus
package com.example.hungvu.testpack;
import rx.Observable;
import rx.Subscriber;
import rx.Subscription;
import rx.functions.Func1;
import rx.subjects.PublishSubject;
import rx.subjects.Subject;
/**
@vuhung3990
vuhung3990 / rx click event.java
Created February 2, 2016 02:23
Rxjava for handle event
// sample 3 UI event
mButton = (Button) findViewById(R.id.button);
Observable<View> clickEventObservable = Observable.create(new Observable.OnSubscribe<View>() {
@Override
public void call(final Subscriber<? super View> subscriber) {
mButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
subscriber.onNext(v);
}
@vuhung3990
vuhung3990 / async rx.java
Created February 2, 2016 02:16
asynchronous in Rxjava
// sample 2 asynchronous task
Observable<String> jsonTask = Observable.create(new Observable.OnSubscribe<String>() {
@Override
public void call(Subscriber<? super String> subscriber) {
subscriber.onNext(getJsonFromInternet());
subscriber.onCompleted();
}
}).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread());
// SUBCRIBE