Search This Blog

Showing posts with label Hàm. Show all posts
Showing posts with label Hàm. Show all posts

Friday, October 7, 2016

Hàm - Lấy thứ từ ngày

private String GetDay_Of_Week(Date date)
{
Calendar cal = Calendar.getInstance();
cal.setTime(date);
int intDayOfWeek=cal.get(Calendar.DAY_OF_WEEK);
return weekdays[intDayOfWeek-1];

}

Friday, September 9, 2016

Wifi - Đến màn hình mở 3g & wifi

private Boolean CheckSetting_GPS()
{
Boolean blreturn = true;
// if(!isSimAvail()){
// Toast.makeText(getApplicationContext(), getString(R.string.lblNoticeSim_frmMain), Toast.LENGTH_LONG).show() ;
// blreturn = false;
// }
//Check airplane mode
if(isAirplaneMode(getApplicationContext()))
{
Toast.makeText(getApplicationContext(), getString(R.string.lblCheckAirplaneMode), Toast.LENGTH_SHORT).show();
blreturn = false;
}
//Check GPS mode
Boolean checkGPS = CheckLocationSetting();
if(!checkGPS)
{
AlertDialog.Builder ab = new AlertDialog.Builder(this);
ab.setTitle(getString(R.string.lblNotice));
ab.setMessage(getString(R.string.lblCheckGPS_frmPJPReview));
ab.setPositiveButton(getString(R.string.lblClose), new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {    
dialog.dismiss();
}
});
ab.setNeutralButton(getString(R.string.lblSetting_frmPJPReview), new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(intent);
dialog.dismiss();
}
});
ab.show();

blreturn = false;
}
if(m_objSetting.CheckFakeGPS==1)
//Check vi tri ao
if(isMockSettingsON(getApplicationContext())){
startActivity(new Intent(android.provider.Settings.ACTION_APPLICATION_DEVELOPMENT_SETTINGS));
Toast.makeText(getApplicationContext(), getString(R.string.lblNoticeGPS1_frmPJPReview), Toast.LENGTH_LONG).show() ;
blreturn = false;
}
//Check ngay thang tu dong
if(!isDTimeSettingsON(getApplicationContext())){
startActivity(new Intent(android.provider.Settings.ACTION_DATE_SETTINGS));
Toast.makeText(getApplicationContext(), getString(R.string.lblNoticeTime2_frmPJPReview), Toast.LENGTH_LONG).show() ;
blreturn = false;
}
//Check mui gio tu dong
if(!isTimeZoneSettingsON(getApplicationContext())){
startActivity(new Intent(android.provider.Settings.ACTION_DATE_SETTINGS));
Toast.makeText(getApplicationContext(), getString(R.string.lblNoticeTime3_frmPJPReview), Toast.LENGTH_LONG).show() ;
blreturn = false;
}
return blreturn;
}

Hàm - Lấy thứ của 1 ngày

private String GetDay_Of_Week(Date date)
{
Calendar cal = Calendar.getInstance();
cal.setTime(date);
int intDayOfWeek=cal.get(Calendar.DAY_OF_WEEK);
return weekdays[intDayOfWeek-1];

}

Hàm - Hàm kiểm tra có mạng hay không

public boolean isOnline() {
   ConnectivityManager cm =
       (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
   NetworkInfo netInfo = cm.getActiveNetworkInfo();
   return netInfo != null && netInfo.isConnectedOrConnecting();
}

Wednesday, August 31, 2016

Hàm - Hàm chuyễn đổi từ chuổi sang tiền

// hàm đổi chuổi số thành tiền   
     public static String[] splitStringEvery(String s, int interval) {
        int arrayLength = (int) Math.ceil(((s.length() / (double) interval)));
        String[] result = new String[arrayLength];
        int j = 0;
        int lastIndex = result.length - 1;
        for (int i = 0; i < lastIndex; i++) {
            result[i] = s.substring(j, j + interval);
            j += interval;
        } //Add the last bit        
        result[lastIndex] = s.substring(j);
        return result;
    }
    public static String XuLyChuoiThanhTien(String s){
        String S[] = splitStringEvery(s, 1);
        String chuoi = "" ;
        for(int i = 0 ; i < S.length;i++){
            if (S.length == 5 || S.length == 6 || S.length == 7 || S.length == 8 ){
                if(S.length == 5) {
                    if(i==1){
                        chuoi += S[i]+"." ;
                    }else {
                        chuoi += S[i] ;
                    }
                }
                if(S.length == 6) {
                    if(i==2){
                        chuoi += S[i]+"." ;
                    }else {
                        chuoi += S[i] ;
                    }
                }
                if(S.length == 7) {
                    if(i==0 || i==3){
                        chuoi += S[i]+"." ;
                    }else {
                        chuoi += S[i] ;
                    }
                }
                if(S.length == 8) {
                    if(i==1 || i==4){
                        chuoi += S[i]+"." ;
                    }else {
                        chuoi += S[i] ;
                    }
                }
            }else {
                chuoi += S[i] ;
            }
        }
        return  chuoi+" VNĐ" ;
    }

Saturday, August 27, 2016

Hàm - Hàm sự kiện chạm vào button

// sự kiện chạm cho button    
    public static void SuKienChamButton(Button btn,final Integer beginResource ,final Integer Resource) {
        btn.setOnTouchListener(new View.OnTouchListener() {
            @Override            
            public boolean onTouch(View v, MotionEvent event) {
                //imvBackActionBar.setBackgroundColor(Color.parseColor("#FF6E9BF5"));                
                 switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN: {
                        Button bt = (Button) v;
                        //overlay is black with transparency of 0x77 (119)                       
                        bt.setBackgroundResource(beginResource);
                        bt.invalidate();
                        break;
                    }
                    case MotionEvent.ACTION_UP:
                    case MotionEvent.ACTION_CANCEL: {
                        Button bt = (Button) v;
                        //clear the overlay                        
                        bt.setBackgroundResource(Resource);
                        bt.invalidate();
                        break;
                    }
                }
                return false;
            }
        });
    }

Friday, August 26, 2016

Ham - Ham thay the dong thoi nhieu ky tu

public static String XuLyDau(String chuoi){
        chuoi = chuoi.replaceAll("á|à|ả|ã|ạ|ă|ắ|ặ|ằ|ẳ|ẵ|â|ấ|ầ|ẩ|ẫ|ậ","a").replaceAll("é|è|ẻ|ẽ|ẹ|ê|ế|ề|ể|ễ|ệ", "e").replaceAll("í|ì|ỉ|ĩ|ị", "i").replaceAll("ó|ò|ỏ|õ|ọ|ô|ố|ồ|ổ|ỗ|ộ|ơ|ớ|ờ|ở|ỡ|ợ", "o").replaceAll("ú|ù|ủ|ũ|ụ|ư|ứ|ừ|ử|ữ|ự", "u").replaceAll("ý|ỳ|ỷ|ỹ|ỵ","y").replaceAll("đ","d")
                .replaceAll("Á|À|Ả|Ã|Ạ|Ă|Ắ|Ẳ|Ằ|Ẳ|Ẵ|Â|Ấ|Ầ|Ẩ|Ẫ|Ậ","A").replaceAll("É|È|Ẻ|Ẽ|Ẹ|Ê|Ế|Ề|Ể|Ễ|Ệ", "E").replaceAll("Í|Ì|Ỉ|Ĩ|Ị", "I").replaceAll("Ó|Ò|Ỏ|Õ|Ọ|Ô|Ố|Ồ|Ổ|Ỗ|Ộ|Ơ|Ớ|Ờ|Ở|Ỡ|Ợ", "O").replaceAll("Ú|Ù|Ủ|Ũ|Ụ|Ư|Ứ|Ừ|Ử|Ữ|Ự", "U").replaceAll("Ý|Ỳ|Ỷ|Ỹ|Ỵ","Y").replaceAll("Đ","D");
        return chuoi ;
    }

Friday, August 19, 2016

Hàm - Hàm tạo mảng ngẫu nhiên không trùng lặp

// hàm tạo mảng ngẫu nhiên từ 1 đến 52public static ArrayList TaoMangNgauNhien(int number) {

 //Khai bao thanh phan trung gian    ArrayList tmp = new ArrayList();
    int count=0;
    int value;
    boolean flag;

    while(count<number) {
        flag = false;
        value = (int)(Math.random()*number);//cac gia tri nam trong khoang tu 1 den 52        value = value+1;
        for(int i=0; i<tmp.size(); i++){
            if(((Integer)tmp.get(i)).intValue()==value){
                flag = true;
                break;
            }
        }
        if(!flag){
            tmp.add(value);
            count++;
        }
    }
    return tmp;
}

Hàm - Hàm làm tròn hiệu của 2 số integer

public static int round2(int a,int n){
    double x = a ;
    double y = n ;
    double d =  x/y ;
    double dAbs = Math.abs(d);
    int i = (int) dAbs;
    double result = dAbs - (double) i;
    if(result<0.25){
        return d<0 ? -i : i;
    }else{
        return d<0 ? -(i+1) : i+1;
    }
}

Tuesday, July 12, 2016

Hàm

package firebase.com.minhnhut027.appthucte;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.AsyncTask;
import android.os.Build;
import android.view.MotionEvent;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.math.BigInteger;
import java.net.URL;
import java.net.URLConnection;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.HashMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/** * Created by USER on 03/30/2016. */
public class Ham extends Activity{
    SQLITE sql = new SQLITE(this);
    private static Activity activity;
    // hàm tạo sự kiện khi chạm vào ảnh   
    public static void SuKienChamVaoAnh(final ImageView imv){
    imv.setOnTouchListener(new View.OnTouchListener() {
        @Override        
        public boolean onTouch(View v, MotionEvent event) {
            //imvBackActionBar.setBackgroundColor(Color.parseColor("#FF6E9BF5"));            
            switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN: {
                    ImageView view = (ImageView) v;
                    //overlay is black with transparency of 0x77 (119)                    
                    imv.setBackgroundColor(Color.parseColor("#FF6E9BF5"));
                    break;
                }
                case MotionEvent.ACTION_UP:
                case MotionEvent.ACTION_CANCEL: {
                    ImageView view = (ImageView) v;
                    //clear the overlay                    
                    imv.setBackgroundColor(Color.TRANSPARENT);
                    break;
                }
            }
            return false;
        }
    });
   }
    // sự kiện chạm vào ảnh    
        public static void SuKienChamVaoAnh(final ImageView imv,final String chuoi){
        imv.setOnTouchListener(new View.OnTouchListener() {
            @Override            
            public boolean onTouch(View v, MotionEvent event) {
                //imvBackActionBar.setBackgroundColor(Color.parseColor("#FF6E9BF5"));                 
                 switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN: {
                        ImageView view = (ImageView) v;
                        //overlay is black with transparency of 0x77 (119)                         
                        imv.setBackgroundColor(Color.parseColor("#FF6E9BF5"));
                        break;
                    }
                    case MotionEvent.ACTION_UP:
                    case MotionEvent.ACTION_CANCEL: {
                        ImageView view = (ImageView) v;
                        //clear the overlay                        
                        imv.setBackgroundColor(Color.parseColor(chuoi));
                        break;
                    }
                }
                return false;
            }
        });
    }
    // sự kiện chạm vào textview    
    public static void SuKienChamVaoTextView(final TextView txt,final String chuoitr,final String chuoisau ){
        txt.setOnTouchListener(new View.OnTouchListener() {
            @Override            
            public boolean onTouch(View v, MotionEvent event) {
                //imvBackActionBar.setBackgroundColor(Color.parseColor("#FF6E9BF5"));                
                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN: {
                        TextView view = (TextView) v;
                        //overlay is black with transparency of 0x77 (119)                        
                        view.setBackgroundColor(Color.parseColor(chuoitr));
                        break;
                    }
                    case MotionEvent.ACTION_UP:
                    case MotionEvent.ACTION_CANCEL: {
                        TextView view = (TextView) v;
                        //clear the overlay                        
                        view.setBackgroundColor(Color.parseColor(chuoisau));
                        break;
                    }
                }
                return false;
            }
        });
    }
    // sự kiện chạm cho button    
    public static void SuKienChamButton(Button btn,final Integer beginResource ,final Integer Resource) {
        btn.setOnTouchListener(new View.OnTouchListener() {
            @Override            
            public boolean onTouch(View v, MotionEvent event) {
                //imvBackActionBar.setBackgroundColor(Color.parseColor("#FF6E9BF5"));                
                 switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN: {
                        Button bt = (Button) v;
                        //overlay is black with transparency of 0x77 (119)                       
                        bt.setBackgroundResource(beginResource);
                        bt.invalidate();
                        break;
                    }
                    case MotionEvent.ACTION_UP:
                    case MotionEvent.ACTION_CANCEL: {
                        Button bt = (Button) v;
                        //clear the overlay                        
                        bt.setBackgroundResource(Resource);
                        bt.invalidate();
                        break;
                    }
                }
                return false;
            }
        });
    }
    public static String ChuoiSangKyTu(String arg) {
        return String.format("%040x", new BigInteger(1, arg.getBytes(/*YOUR_CHARSET?*/)));
    }
    public static String XuLyDau(String chuoi){
        chuoi = chuoi.replaceAll("á|à|ả|ã|ạ|ă|ắ|ặ|ằ|ẳ|ẵ|â|ấ|ầ|ẩ|ẫ|ậ","a").replaceAll("é|è|ẻ|ẽ|ẹ|ê|ế|ề|ể|ễ|ệ", "e").replaceAll("í|ì|ỉ|ĩ|ị", "i").replaceAll("ó|ò|ỏ|õ|ọ|ô|ố|ồ|ổ|ỗ|ộ|ơ|ớ|ờ|ở|ỡ|ợ", "o").replaceAll("ú|ù|ủ|ũ|ụ|ư|ứ|ừ|ử|ữ|ự", "u").replaceAll("ý|ỳ|ỷ|ỹ|ỵ","y").replaceAll("đ","d")
                .replaceAll("Á|À|Ả|Ã|Ạ|Ă|Ắ|Ẳ|Ằ|Ẳ|Ẵ|Â|Ấ|Ầ|Ẩ|Ẫ|Ậ","A").replaceAll("É|È|Ẻ|Ẽ|Ẹ|Ê|Ế|Ề|Ể|Ễ|Ệ", "E").replaceAll("Í|Ì|Ỉ|Ĩ|Ị", "I").replaceAll("Ó|Ò|Ỏ|Õ|Ọ|Ô|Ố|Ồ|Ổ|Ỗ|Ộ|Ơ|Ớ|Ờ|Ở|Ỡ|Ợ", "O").replaceAll("Ú|Ù|Ủ|Ũ|Ụ|Ư|Ứ|Ừ|Ử|Ữ|Ự", "U").replaceAll("Ý|Ỳ|Ỷ|Ỹ|Ỵ","Y").replaceAll("Đ","D");
        return chuoi ;
    }
    //Hàm xử lý chuổi - thêm %20 vào giữa chuổi tìm kiếm    
    public static String XuLyChuoiTimKiem(String chuoi){

        String ChuoiKetQua = "";
        String[] parts = chuoi.split(" ");
        for (int i = 1 ; i< parts.length ; i++){
            ChuoiKetQua = ChuoiKetQua + "%20"+ parts[i]  ;
        }
        ChuoiKetQua = parts[0]+ ChuoiKetQua ;
        return  ChuoiKetQua ;
    }
    //Hàm xử lý chuổi lay 30 chu    
    public static String XuLyChuoiLay30chu(String chuoi){
        String ChuoiKetQua = "";
        String[] parts = chuoi.split(" ");
        if(parts.length < 30) {
            for (int i = 0; i < parts.length; i++) {
                ChuoiKetQua = ChuoiKetQua + " "+parts[i];
            }
        }else {
            for (int i = 0; i < 30; i++) {
                ChuoiKetQua = ChuoiKetQua + " "+parts[i];
            }
            ChuoiKetQua = ChuoiKetQua +"..";
        }
        return  ChuoiKetQua ;
    }
    //Hàm xử lý chuổi ra ngay thang nam    
    public static String XuLyChuoiRaNgayThangNam(String chuoi){
        String ChuoiKetQua = "";
        String[] parts = chuoi.split("-");
        String[] S = splitStringEvery(parts[1],2);
        ChuoiKetQua = S[3]+"-"+S[2]+"-"+S[0]+S[1];
        return  ChuoiKetQua ;
    }
    //Hàm xử lý chuổi ra ngay thang nam    
    public static String CatChuoiNgayThangNam(String chuoi){
        String ChuoiKetQua = "";
        String[] parts = chuoi.split("-");
        return   parts[1] ;
    }
    // hàm lấy N chữ    
    public static String XuLyChuoiLayNchu(String chuoi, int N){
        String ChuoiKetQua = "";
        String[] parts = chuoi.split(" ");
        if(parts.length < N) {
            for (int i = 0; i < parts.length; i++) {
                ChuoiKetQua = ChuoiKetQua + " "+parts[i];
            }
        }else {
            for (int i = 0; i < N; i++) {
                ChuoiKetQua = ChuoiKetQua + " "+parts[i];
            }
            ChuoiKetQua = ChuoiKetQua +"..";
        }
        return  ChuoiKetQua ;
    }
    //Hàm xử lý chuổi lay 10 chu    
    public static String XuLyChuoiLay10chu(String chuoi){
        String ChuoiKetQua = "";
        String[] parts = chuoi.split(" ");
        if(parts.length < 10) {
            for (int i = 0; i < parts.length; i++) {
                ChuoiKetQua = ChuoiKetQua + " "+parts[i];
            }
        }else {
            for (int i = 0; i < 10; i++) {
                ChuoiKetQua = ChuoiKetQua + " "+parts[i];
            }
            ChuoiKetQua = ChuoiKetQua +"..";
        }
        return  ChuoiKetQua ;
    }
    //Hàm xử lý chuổi lay 7 chu    
    public static String XuLyChuoiLay7chu(String chuoi){
        String ChuoiKetQua = "";
        String[] parts = chuoi.split(" ");
        if(parts.length < 8) {
            for (int i = 0; i < parts.length; i++) {
                ChuoiKetQua = ChuoiKetQua + " "+parts[i];
            }
        }else {
            for (int i = 0; i < 7; i++) {
                ChuoiKetQua = ChuoiKetQua + " "+parts[i];
            }
            ChuoiKetQua = ChuoiKetQua +"..";
        }
        return  ChuoiKetQua ;
    }
    // Kiểm tra edittext email    
     public static boolean isEmailValid(String email) {
        boolean isValid = false;

        String expression = "^[\\w\\.-]+@([\\w\\-]+\\.)+[A-Z]{2,4}$";
        CharSequence inputStr = email;

        Pattern pattern = Pattern.compile(expression, Pattern.CASE_INSENSITIVE);
        Matcher matcher = pattern.matcher(inputStr);
        if (matcher.matches()) {
            isValid = true;
        }
        return isValid;
    }
    //Hàm xử lý chuổi lay 4 chu    
    public static String XuLyChuoiLay4chu(String chuoi){
        String ChuoiKetQua = "";
        String[] parts = chuoi.split(" ");
        if(parts.length < 6) {
            for (int i = 1; i < parts.length; i++) {
                ChuoiKetQua = ChuoiKetQua + " "+parts[i];
            }
            ChuoiKetQua = parts[0] + ChuoiKetQua ;
        }else {
            for (int i = 1; i < 5; i++) {
                ChuoiKetQua = ChuoiKetQua + " "+parts[i];
            }
            ChuoiKetQua = parts[0] + ChuoiKetQua ;
            ChuoiKetQua = ChuoiKetQua +"..";
        }
        return  ChuoiKetQua ;
    }
    //Hàm xử lý chuổi lay 3 chu    
    public static String XuLyChuoiLay3chu(String chuoi){
        String ChuoiKetQua = "";
        String[] parts = chuoi.split(" ");
        if(parts.length < 4) {
            for (int i = 1; i < parts.length; i++) {
                ChuoiKetQua = ChuoiKetQua + " "+parts[i];
            }
            ChuoiKetQua = parts[0] + ChuoiKetQua ;
        }else {
            for (int i = 1; i < 3; i++) {
                ChuoiKetQua = ChuoiKetQua + " "+parts[i];
            }
            ChuoiKetQua = parts[0] + ChuoiKetQua ;
            ChuoiKetQua = ChuoiKetQua +"..";
        }
        return  ChuoiKetQua ;
    }
    // hàm reload activity   
   @SuppressLint("NewApi")
    public static final void ReloadActivity(final Activity a) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
            a.recreate();
        } else {
            final Intent intent = a.getIntent();
            intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
            a.finish();
            a.overridePendingTransition(0, 0);
            a.startActivity(intent);
            a.overridePendingTransition(0, 0);
        }
    }
    //Hàm xử lý chuổi tiêu đề    
    public static String XuLyChuoiTieuDe(String chuoi){
        String ChuoiKetQua = "";
        String S[] = splitStringEvery(chuoi, 1);
        for (int i = 0 ; i< 13 ; i++){
            ChuoiKetQua = ChuoiKetQua +S[i]  ;
        }
        ChuoiKetQua =  ChuoiKetQua +".." ;
        return  ChuoiKetQua ;
    }
    public static void restartActivity(Context act){
        Intent intent=new Intent();
        intent.setClass(act, act.getClass());
        intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
        act.startActivity(intent);
        //act.finish();    }
    // hàm lay activity hien tai    
    public static Activity getActivity()  {
        Class activityThreadClass = null;
        try {
            activityThreadClass = Class.forName("android.app.ActivityThread");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        Object activityThread = null;
        try {
            activityThread = activityThreadClass.getMethod("currentActivityThread").invoke(null);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        }
        Field activitiesField = null;
        try {
            activitiesField = activityThreadClass.getDeclaredField("mActivities");
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        }
        activitiesField.setAccessible(true);
        HashMap activities = null;
        try {
            activities = (HashMap) activitiesField.get(activityThread);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        for (Object activityRecord : activities.values()) {
            Class activityRecordClass = activityRecord.getClass();
            Field pausedField = null;
            try {
                pausedField = activityRecordClass.getDeclaredField("paused");
            } catch (NoSuchFieldException e) {
                e.printStackTrace();
            }
            pausedField.setAccessible(true);
            try {
                if (!pausedField.getBoolean(activityRecord)) {
                    Field activityField = activityRecordClass.getDeclaredField("activity");
                    activityField.setAccessible(true);
                    activity = (Activity) activityField.get(activityRecord);
                }
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (NoSuchFieldException e) {
                e.printStackTrace();
            }
        }
        return activity;
    }
    // hàm ẩn keyboard    
   public static void hideKeyboard(Activity activity) {
        InputMethodManager imm = (InputMethodManager) activity.getSystemService(Activity.INPUT_METHOD_SERVICE);
        //Find the currently focused view, so we can grab the correct window token from it.        View view = activity.getCurrentFocus();
        //If no view currently has focus, create a new one, just so we can grab a window token from it        if (view == null) {
            view = new View(activity);
        }
        imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
    }
    // hàm đổi chuổi số thành tiền   
     public static String[] splitStringEvery(String s, int interval) {
        int arrayLength = (int) Math.ceil(((s.length() / (double) interval)));
        String[] result = new String[arrayLength];
        int j = 0;
        int lastIndex = result.length - 1;
        for (int i = 0; i < lastIndex; i++) {
            result[i] = s.substring(j, j + interval);
            j += interval;
        } //Add the last bit        
        result[lastIndex] = s.substring(j);
        return result;
    }
    public static String XuLyChuoiThanhTien(String s){
        String S[] = splitStringEvery(s, 1);
        String chuoi = "" ;
        for(int i = 0 ; i < S.length;i++){
            if (S.length == 5 || S.length == 6 || S.length == 7 || S.length == 8 ){
                if(S.length == 5) {
                    if(i==1){
                        chuoi += S[i]+"." ;
                    }else {
                        chuoi += S[i] ;
                    }
                }
                if(S.length == 6) {
                    if(i==2){
                        chuoi += S[i]+"." ;
                    }else {
                        chuoi += S[i] ;
                    }
                }
                if(S.length == 7) {
                    if(i==0 || i==3){
                        chuoi += S[i]+"." ;
                    }else {
                        chuoi += S[i] ;
                    }
                }
                if(S.length == 8) {
                    if(i==1 || i==4){
                        chuoi += S[i]+"." ;
                    }else {
                        chuoi += S[i] ;
                    }
                }
            }else {
                chuoi += S[i] ;
            }
        }
        return  chuoi+" VNĐ" ;
    }
    // Hàm đọc nội dung từ internet    
    public static String docNoiDung_Tu_URL(String theUrl)
    {
        StringBuilder content = new StringBuilder();
        try    {
            // create a url object           
             URL url = new URL(theUrl);
            // create a urlconnection object            
            URLConnection urlConnection = url.openConnection();
            // wrap the urlconnection in a bufferedreader           
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
            String line;
            // read from the urlconnection via the bufferedreader            
            while ((line = bufferedReader.readLine()) != null)
            {
                content.append(line + "\n");
            }
            bufferedReader.close();
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
        return content.toString();
    }

    public static String LayThoiGian(){
        Calendar c = Calendar.getInstance();
        SimpleDateFormat df = new SimpleDateFormat("HHmmss-yyyyMMdd");
        String formattedDate = df.format(c.getTime());
        return formattedDate ;
    }
    // Hàm quay lui    
    //@Override    
    public static class LoadURL extends AsyncTask<String , Integer , String>{
        @Override        
        protected String doInBackground(String... params) {
            String s = "" ;
            s = docNoiDung_Tu_URL(params[0]);
            return s;
        }
        @Override        
        protected void onPostExecute(String s) {
            super.onPostExecute(s);
        }

    }


}

Thursday, April 21, 2016

SQLITE - GETCOUNT HAM DEM SO DONG

Cursor c1 = sql.SQLite_GetData("SELECT * FROM giohang");
Toast.makeText(getContext(),""+c1.getCount(),Toast.LENGTH_SHORT).show();

Wednesday, April 20, 2016

ACTIVITY - HAM GETACTIVITY HIEN TAI - LAY ACTIVITY CURRENT

public static Activity getActivity()  {
    Class activityThreadClass = null;
    try {
        activityThreadClass = Class.forName("android.app.ActivityThread");
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    }
    Object activityThread = null;
    try {
        activityThread = activityThreadClass.getMethod("currentActivityThread").invoke(null);
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    } catch (InvocationTargetException e) {
        e.printStackTrace();
    } catch (NoSuchMethodException e) {
        e.printStackTrace();
    }
    Field activitiesField = null;
    try {
        activitiesField = activityThreadClass.getDeclaredField("mActivities");
    } catch (NoSuchFieldException e) {
        e.printStackTrace();
    }
    activitiesField.setAccessible(true);
    HashMap activities = null;
    try {
        activities = (HashMap) activitiesField.get(activityThread);
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    }
    for (Object activityRecord : activities.values()) {
        Class activityRecordClass = activityRecord.getClass();
        Field pausedField = null;
        try {
            pausedField = activityRecordClass.getDeclaredField("paused");
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        }
        pausedField.setAccessible(true);
        try {
            if (!pausedField.getBoolean(activityRecord)) {
                Field activityField = activityRecordClass.getDeclaredField("activity");
                activityField.setAccessible(true);
                activity = (Activity) activityField.get(activityRecord);

            }
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        }
    }
    return activity;
}