Issue with Exception handling in Xamarin - Android -


i using following helper method open external app's in android application.

using android.content;   namespace xyzab  {    public static class externalapplauncher    {     #region constants      const string faceboourlschema = "fb://page/34530418978";     const string facebookappname = "com.facebook.katana";      const string twitterurlschema = "twitter://user?id=1534530";     const string twitterappname = "com.twitter.android";        #endregion      #region public methods      /// <summary>     /// method launches pre-installed app      /// </summary>     /// <returns>the app.</returns>     /// <param name="launcher">launcher.</param>     public static intent launchapp (launcher launcher)     {         intent intent = null;         switch (launcher) {          case launcher.facebook:              try {                 var uri = android.net.uri.parse (faceboourlschema);                 intent = new intent (intent.actionview, uri);              } catch (activitynotfoundexception) {                 var uri = android.net.uri.parse ("market://details?id=" + facebookappname);                 intent = new intent (intent.actionview, uri);             }             break;            case  launcher.twitter:             try {                 var uri = android.net.uri.parse (twitterurlschema);                 intent = new intent (intent.actionview, uri);              } catch (activitynotfoundexception) {                 var uri = android.net.uri.parse ("market://details?id=" + twitterappname);                 intent = new intent (intent.actionview, uri);             }             break;         }         return intent;     }     #endregion } 

}

the intention behind helper is, if target app installed have open app. otherwise have open market place.

but here issue if app not present in phone. throwing activitynotfoundexception not enters try catch block.

even in calling place added try catch. not catching exception. wrong here.

    void abcd(object sender, eventargs e)     {          try         {             var intent = externalapplauncher.launchapp(launcher.twitter);             if (null != intent)                 startactivity(intent);         }         catch (exception)         {              throw;         } 

`

i don't think best way check whether app installed on device. android has built-in ways that.

i have @ packagemanager class. has method getpackageinfo provides more information particular package.

the packagemanager can used context class (e.g. activity or service)

for example, create method below:

private bool isappinstalled(string packagename) {     try     {        packagemanager.getpackageinfo(packagename, packageinfoflags.activities);        return true;     }     catch (packagemanager.namenotfoundexception e)     {         return false;     } } 

you call method this:

isappinstalled("com.twitter.android"); 

Comments

Popular posts from this blog

android - MPAndroidChart - How to add Annotations or images to the chart -

javascript - Add class to another page attribute using URL id - Jquery -

firefox - Where is 'webgl.osmesalib' parameter? -