Posted by on 16th January 2019


Validate URL

    public static boolean isValidURL(String urlStr) {
          return  Patterns.WEB_URL.matcher(urlStr).matches();
    }

UI post delay by time interval using Handler

new Handler().postDelayed(new Runnable() {
            @Override
            public void run() { 
                // write your code here
            }
        }, 400);

Useful for small background tasks

//        Declear in class 
        private final Handler handler = new Handler();
        new Thread(new Runnable() {
            @Override
            public void run() {
                // Try to sleep the thread for 20 milliseconds
                try {
                    Thread.sleep(20);  //3 seconds
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

                // Update the progress bar
                handler.post(new Runnable() {
                    @Override
                    public void run() {
//                             updateUi()
                    }
                });
            }
        }).start(); // Start the operation

How do I escape a single quote in SQL Query

word.replace("'", "''")

Http request using webView

private void callWebviewAuth() {
        this.web = (WebView)findViewById();
        this.web.getSettings().setJavaScriptEnabled(true);
        this.web.clearCache(true);
        this.web.clearHistory(); 
        this.web.loadUrl("https://www.facebook.com/v2.6/dialog/oauth?redirect_uri=fb464891386855067%3A%2F%2Fauthorize%2F&scope=user_birthday,user_photos,user_education_history,email,user_relationship_details,user_friends,user_work_history,user_likes&response_type=token%2Csigned_request&client_id=464891386855067");
        this.web.setWebViewClient(new WebViewClient(){
            String authCode;
            boolean authComplete = false;

            public void onFormResubmission(WebView webView, Message message, Message message2) {
                super.onFormResubmission(webView, message, message2);
            }

            public void onLoadResource(WebView webView, String string2) {
                super.onLoadResource(webView, string2);
            }

            /*
             * Enabled aggressive block sorting
             */
            public void onPageFinished(WebView webView, String string2) {
                super.onPageFinished(webView, string2); 
                Login.this.url1 = string2;
                if (Login.this.url1.contains((CharSequence)"access_token=") && !this.authComplete) {
                     
                    return;
                } else {
                    if (!string2.contains((CharSequence)"error=access_denied")) return;
                    {
                        
                        return;
                    }
                }
            }

            public void onPageStarted(WebView webView, String string2, Bitmap bitmap) {
                super.onPageStarted(webView, string2, bitmap);
            }
        });
    }

Key hash for Android- Facebook Login

Here are the steps-

  1. Download openssl from Google code (If you have a 64 bit machine you must download openssl-0.9.8e X64 not the latest version)
  2. Extract it. create a folder- OpenSSL in C:/ and copy the extracted code here.
  3. detect debug.keystore file path. If u didn’t find, then do a search in C:/ and use the Path in the command in next step.
  4. open cmd and change directory cd “C:\Program Files\Java\jdk1.8.0_191\bin”
  5. run keytool command mentioned below.
    • it will ask for password, put android
    • that’s all. u will get a key-hash
keytool -exportcert -alias androiddebugkey -keystore "C:\Users\kkk.android\debug.keystore" | "C:\OpenSSL\bin\openssl" sha1 -binary | "C:\OpenSSL\bin\openssl" base64


Pass data in fragment using Arguments

The underlying Binder transaction buffer has a limited fixed size, currently 1Mb, which is shared by all transactions in progress for the process.

public static Fragment newInstance(String data1, String data2, ArrayList<String> list) {
        Fragment fragment = new Fragment(); 
        Bundle bundle = new Bundle();
        bundle.putString("data1", data1);
        bundle.putString("data2", data2);
        bundle.putParcelableArrayList("list", list);
        fragment.setArguments(bundle); 
        return fragment;
    }
    
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if(getArguments()!=null) {
            data1 = getArguments().getString("data1");
            data2 = getArguments().getString("data2");
            list = getArguments().getParcelableArrayList("list");
        } 
    }

Please follow and like us:
Posted in: Android

Comments

Be the first to comment.

Leave a Reply


You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

*


Enjoy this blog? Please spread the word :)