你好,游客 登录 注册 搜索
背景:
阅读新闻

Android启动Activity前确定Intent 能否解析

[日期:2015-01-20] 来源:Linux社区  作者:chao2010686 [字体:大 中 小]

在自己的应用程序中利用第三方应用程序的Activity 和Service 是十分方便的,但是,你无法保证用户设备上安装了特定的某个应用程序,或者设备上有能够处理你的请求的应用程序。

因此,在调用startActivity 之前,确定调用是否可以解析为一个Activity 是一种很好的做法。

通过调用Intent 的resolveActivity 方法,并向该方法传入包管理器,可以对包管理器进行查询,

确定是否有Activity 能够启动以响应该Intent。

if (somethingWeird && itDontLookGood) {
      // Createthe impliciy Intent to use to start a new Activity.
          Intentintent =
          newIntent(Intent.ACTION_DIAL, Uri.parse("tel:555-2368"));
          // Check ifan Activity exists to perform this action.
        PackageManager pm = getPackageManager();
        ComponentName cn = intent.resolveActivity(pm);
      if (cn ==null) {
      // If thereis no Activity available to perform the action
      // Check tosee if the Google Play Store is available.
          UrimarketUri =
        Uri.parse("market://search?q=pname:com.myapp.packagename");
          IntentmarketIntent = new
        Intent(Intent.ACTION_VIEW).setData(marketUri);
      // If theGoogle Play Store is available, use it to download anapplication
      // capableof performing the required action. Otherwise log an
      //error.
      if(marketIntent.resolveActivity(pm) != null)
        startActivity(marketIntent);
      else
          Log.d(TAG,"Market client not available.");
      }
      else
        startActivity(intent);
      }

更多Android相关信息见Android 专题页面 http://www.linuxidc.com/topicnews.aspx?tid=11

本文永久更新链接地址:http://www.linuxidc.com/Linux/2015-01/112102.htm

linux
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款