Intent 和 Intent 过滤器

Intent类型


Intent两种类型:
显示Intent:按名称指定启动组件
隐式Intent:不指定特定组件

Intent 和 Intent 过滤器_第1张图片
隐式Intent传递图解

[1] Activity A 把包含有信息的Intent传递给startActivity()
[2] system搜索所有应用中与Intent匹配的Intent过滤器
[3] system调用被匹配的Activity B的onCreate(),并得到Intent

注意:不要对service使用隐式Intent,存在安全隐患

构建Intent


Intent包含信息:
组件名称
仅对与显示Intent有。
方法有:setComponent(),setClass(),setClassName(),Intent构造函数

操作
使用setAction()或者Intent构造函数指定操作.
如果定义自己的操作,请确保将应用的软件包名称作为前缀.
static final String ACTION_TIMETRAVEL = "com.example.action.TIMETRAVEL";

数据
Uri对象,由操作决定。一般也设置上数据类型(MIME)
仅设置数据Uri,setData()
仅设置MIME类型,setType()
两者都设置,setDataAndType(),不能同时用上面两种function

类别
可以将任意数量的类别加入Intent.
addCategory()指定类别

以上操作、数据、类别都是system解析应当启动那个组件;

Extra
添加附件信息
putExtra() 键值对,键可以自定义(请确保将应用的软件包名称作为前缀)
static final String EXTRA_GIGAWATTS = "com.example.EXTRA_GIGAWATTS";
putExtras() 传入Bundle

标志
指定系统如何启动Activity
setFlags()

ACTION_SEND实例


Sharing Content with Intents

  • sharing Local Content
    Sending HTML:
Intent sharingIntent = new Intent(Intent.ACTION_SEND);
sharingIntent.setType("text/html");
sharingIntent.putExtra(android.content.EXTRA_TEXT,Html.fromHtml("

This is the text shared.

")); startActivity(Intent.createChooser(sharingIntent,"share using"));

Sending Images:

final Intent sharingIntent=new Intent(Intent.ACTION_SEND);
sharingIntent.setType("image/jpg");
sharingIntent.putExtra(Intent.EXTRA_STREAM,Uri.fromFile(photoFIle));
startActivity(Intent.createChooser(sharingIntent,"sharing image using"));

Sending Links:

Intent sharingIntent = new Intent(Intent.ACTION_SEND);
sharingIntent.setType("text/plain");
sharingIntent.putExtra(Intent.EXTRA_TEXT,"http://codepath.com");
startActivity(Intent.createChooser(sharingIntent,"Share link using"));

Sharing Multiple Type:

String text="look at   my awesome picture";
Uri pictureUri= Uri.parse("file://my_picture");
Intent sharingIntent = new Intent();
sharingIntent.setAction(Intent.ACTION_SEND);
sharingIntent.setType("image/*");
sharingIntent.putExtra(Intent.EXTRA_TEXT,text);
sharingIntent.putExtra(Intent.EXTRA_STREAM,pictureUri);
sharingIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivity(Intent.createChooser(sharingIntent,"share image ....."));

Sharing multipe images:

Intent shareIntent = new Intent(Intent.ACTION_SEND_MULTIPLE);
shareIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, imageUris);
shareIntent.setType("image/*");
  • Sharing Remote Images
    There are two ways to accomplish this.

The first way, takes the bitmap from the view and loads it into a file.

ImageView ivImage=(ImageView)findViewById(R.id.ivResult);
Glide.with(context).load(imagUrl).into(ivImage);
//can be triggered by a view event such as button click
    public void onShareItem(View v){
        ImageView ivImage = (ImageView)findViewById(R.id.ivResult);
        //takes bitmap from the view and loads it into a file
        Uri bitmapUri = getLocalBitmapUri(ivImage);
        if(bitmapUri!=null){
            Intent sharingIntent = new Intent();
            sharingIntent.setAction(Intent.ACTION_SEND);
            sharingIntent.setType("image/*");
            sharingIntent.putExtra(Intent.EXTRA_STREAM,bitmapUri);
            startActivity(Intent.createChooser(sharingIntent,"share image"));
        }else{
            Toast.makeText(this,"sharing failed",Toast.LENGTH_SHORT).show();
        }
    }
    //Return the URI path to the Bitmap displayed in the specified ImageView
    public Uri getLocalBitmapUri(ImageView imageView){
        //Extract Bitmap from imageView Drawable
        Drawable drawable = imageView.getDrawable();
        Bitmap bmp = null;
        if(drawable instanceof BitmapDrawable){
            bmp = ((BitmapDrawable)drawable).getBitmap();
        }else {
            return null;
        }


        //Store image to default external storage directory
        FileOutputStream out=null;
        Uri bitmpUri=null;
        try {
            //This is way, you don't need to request external read/write permission
            File file = new File(getExternalFilesDir(Environment.DIRECTORY_PICTURES), "share_image_" + System.currentTimeMillis() + ".png");
            out = new FileOutputStream(file);
            bmp.compress(Bitmap.CompressFormat.PNG,90,out);
            bitmpUri=Uri.fromFile(file);
        }catch (IOException e){
            e.printStackTrace();
        }finally {
            if(out!=null){
                try {
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return bitmpUri;
    }

你可能感兴趣的:(Intent 和 Intent 过滤器)