Android - Open Line Application from URL in Web-View

Keyword easy to search: 
 - How to open the Google Play Store directly from my Android application?
 - Sending an Intent to browser to open specific URL
 - URL encoding and decoding
 - Install Application programmatically on Android
Example 
Line Url : line://msg/text/%E3%80%90%E3%82%AB%E3%83%A9%E3%82%B3%E3%83%B3%E7%9D%80%......

Create class to check app with package => CheckAppInstallation.java

public class CheckAppInstallation {
    public static boolean isAppInstalled(Context context, String packageName) {
        try {
            context.getPackageManager().getApplicationInfo(packageName, 0);
            return true;
        } catch (PackageManager.NameNotFoundException e) {
            return false;
        }
    }
}
------------------------------------------------------------
Constant.java
public static String LINE_PACKAGE = "jp.naver.line.android";
public static String PLAY_STORE_URL_DOWNLOAD_LINE  = "https://play.google.com/store/apps/details?id=";
------------------------------------------------------------
How to check it have app on device or not ? If it's does't have it will link to download
Apply it in shouldOverrideUrlLoading

private void setMainWebView() {
        wvMain.getSettings().setJavaScriptEnabled(true);
        wvMain.setWebViewClient(new WebViewClient(){
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                super.shouldOverrideUrlLoading(view, url);

                        if(url.contains("line://")){
                               if(CheckAppInstallation.isAppInstalled(getApplicationContext(), Constants.LINE_PACKAGE))
                             {
                                     openLineApp(url);
                             }
                                else
                            {
                               gotoDownloadLineApp();
                         }
      }
});
wvMain.loadUrl(Constants.BASE_URL);
}

openLineApp()=> Open Line App In Device
private void openLineApp(String url)
    {
        try{
            getBaseContext().getPackageManager().getPackageInfo(Constants.LINE_PACKAGE, 0);
            Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
            startActivity(i);
        }catch (PackageManager.NameNotFoundException e){
            startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(Constants.PLAY_STORE_URL_DOWNLOAD_LINE + Constants.LINE_PACKAGE)));
        }
    }

gotoDownloadLineApp() => Download in PlayStore
 private void gotoDownloadLineApp()
    {
        try {
            startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + Constants.LINE_PACKAGE)));
        } catch (android.content.ActivityNotFoundException anfe) {
            startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=" + Constants.LINE_PACKAGE )));
        }
    }
------------------------------------------------------------
Website For Decode Line message : http://tech-unlimited.com/urlencode.html

SHARE

About Unknown

    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment