应用程序之间的交互

来源:互联网 发布:ubuntu caffe 教程 编辑:程序博客网 时间:2024/05/22 06:07

 一.怎么去启动另一个应用程序

     1.定义阴式的Intent

       1>启动拨号:

           Uri uri=Uri.parse("tel:010888888");

         Intent callIntent=new Intent(Intent.ACTION_DIAL,uri);

        2>浏览地图

          // Map point based on address
        Uri location = Uri.parse("geo:0,0?q=1600+Amphitheatre+Parkway,+Mountain+View,+California");
        // Or map point based on latitude/longitude
       // Uri location = Uri.parse("geo:37.422219,-122.08364?z=14"); // z param is zoom level
        Intent mapIntent = new Intent(Intent.ACTION_VIEW, location);

       3>访问浏览器

         Uri uri=Uri.parse("http://www.baidu.com");

       Intent webIntent=new Intent(Intent.ACTION_VIEW,uri);

       4>发送emial

         Intent emailIntent = new Intent(Intent.ACTION_SEND);
        // The intent does not have a URI, so declare the "text/plain" MIME type
          emailIntent.setType(HTTP.PLAIN_TEXT_TYPE);
      emailIntent.putExtra(Intent.EXTRA_EMAIL, new String[] {"jon@example.com"}); // recipients
      emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Email subject");
        emailIntent.putExtra(Intent.EXTRA_TEXT, "Email message text");
      emailIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("content://path/to/email/attachment"));
    /  / You can also attach multiple items by passing an ArrayList of Uris

2检验是否存在接受当前Intent的app

     PackageManager  pm=getPackageManager();   

    List<ResolveInfo> activities = pm.queryIntentActivities(intent, 0);
    boolean isIntentSafe = activities.size() > 0;

3 弹出app chooser

    Intent choolserIntent=Intent.createChooser(Intent intent,String title);

二 从被访问App中获取结果数据

   启动方式:由startActivity(),改为startActivityForResult();在源app 的Activity中实现onActivityResult()方法,从参数Intent data获取返回的数据。

三 如何实现第三方app访问您的app

    1 定义Intent Filter,必须包含action,category标签,一般,使用:<category android:name="android.intent.category.DEFAULT"/>即可;data标签可选。

     2 在需要被访问的Activity onCreate或者Onstart()方法中,处理第三方传递过来的Intent,如果需要回传数据,添加:setResult(int resultCode,Intent resultIntent),

       或者setResult(int  resultCode);