• 【Android基础】利用Intent在Activity之间传递数据


    前言:

    上一篇文章给大家聊了Intent的用法,如何用Intent启动Activity和隐式Intent,这一篇文章给大家聊聊如何利用Intent在Activity之间进行沟通。
     

    从一个Activity获取返回结果:

    启动一个Activity不仅仅是startActivity(Intent intent)一种方法,你也可以通过startActivityForResult()启动一个Activity并且在它退出的时候收到一个返回结果。

    比如,你可以调用系统相机在你的应用中,拍了一张照片,然后返回到你的Activity,这个时候就可以通过这种方法把照片作为结果返回给你的Activity。再比如,你可以通过这种方法启动系统联系人应用,然后获取一个人的详细联系方式。

    注意:在调用startActivityForResult()时你可以利用显示Intent或者隐式Intent,但是在你能够利用显式Intent的时候尽量利用显式Intent,这样能够保证返回的结果是你期待的正确结果。
     

    启动一个Activity:

     

    在用startActivityForResult()来启动一个Activity时,Intent的写法与startActivity()是一样的,没有任何区别,只是你需要传递一个额外的Integer的变量作为启动参数,当启动的那个Activity退出时这个参数会被作为回调函数的一个参数,用来区分返回结果,也就是说你启动Activity时传递的参数(requestCode)和返回结果时的那个参数(requestCode)是一致的。

    下面是一个调用startActivityForResult()获取联系人的例子:

    static final int PICK_CONTACT_REQUEST = 1;  // The request code
    ... 
    private void pickContact() { 
        Intent pickContactIntent = new Intent(Intent.ACTION_PICK, Uri.parse("content://contacts"));
        pickContactIntent.setType(Phone.CONTENT_TYPE); // Show user only contacts w/ phone numbers
        startActivityForResult(pickContactIntent, PICK_CONTACT_REQUEST);
    }
    

      

    startActivityForResult()函数在Activity源码中是这样的:

     /**
         * Same as calling {@link #startActivityForResult(Intent, int, Bundle)}
         * with no options.
         *
         * @param intent The intent to start.
         * @param requestCode If >= 0, this code will be returned in
         *                    onActivityResult() when the activity exits.
         *
         * @throws android.content.ActivityNotFoundException
         *
         * @see #startActivity
         */
        public void startActivityForResult(Intent intent, int requestCode) {
            startActivityForResult(intent, requestCode, null);
        }
    

      

    这个方法在Activity中进行了重载,startActivityForResult(intent, requestCode, null);方法就不贴出来了。但是对于这个方法使用时的注意事项我给大家翻译一下:
    • 这个方法只能用来启动一个带有返回结果的Activity,Intent的参数设定需要注意一下,你不能启动一个Activity使用singleTask的launch mode,用singleTask启动Activity,那个Activity在另外的一个Activity栈中,你会立刻收到RESULT_CANCELED消息;
    • 不能在Activity生命周期函数onResume之前调用startActivityForResult()方法,如果你在onResume之前调用了,那么所在的Activity就无法显示,直到启动的那个Activity退出然后返回结果,这是为了避免在重新定向到另外Activity时窗口闪烁;
     

    接收返回结果:

    当startActivityForResult()启动的Activity完成任务退出时,系统会回调你调用Activity的onActivityResult()方法,这个方法有三个参数:

    • resquestCode : 启动Activity时传递的requestCode;
    • resultCode: 表示调用成功或者失败的变量,值为下面二者之一;
          /** Standard activity result: operation canceled. */
          public static final int RESULT_CANCELED    = 0;
          /** Standard activity result: operation succeeded. */
          public static final int RESULT_OK           = -1;
    • Intent:包含返回内容的Intent;
    下面的代码是处理获取联系人结果的例子:
    @Override 
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // Check which request we're responding to 
        if (requestCode == PICK_CONTACT_REQUEST) {
            // Make sure the request was successful 
            if (resultCode == RESULT_OK) {
                // The user picked a contact. 
                // The Intent's data Uri identifies which contact was selected. 
     
                // Do something with the contact here (bigger example below) 
            } 
        } 
    }
    

      

    注意:为了正确的处理返回的Intent结果,你需要清楚的了解Intent返回结果的格式。如果是你自己写的Intent作为返回结果你会很清楚,但是如果是调用的系统APP(相机,联系人等),那么Intent返回结果格式你应该清楚的知道。比如:联系人应用是返回的联系人URI,相机返回的是Bitmap数据。
     

    处理返回结果:

    下面的代码是如何处理获取联系人的结果:
    @Override 
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // Check which request it is that we're responding to 
        if (requestCode == PICK_CONTACT_REQUEST) {
            // Make sure the request was successful 
            if (resultCode == RESULT_OK) {
                // Get the URI that points to the selected contact 
                Uri contactUri = data.getData();
                // We only need the NUMBER column, because there will be only one row in the result 
                String[] projection = {Phone.NUMBER};
     
                // Perform the query on the contact to get the NUMBER column 
                // We don't need a selection or sort order (there's only one result for the given URI) 
                // CAUTION: The query() method should be called from a separate thread to avoid blocking 
                // your app's UI thread. (For simplicity of the sample, this code doesn't do that.) 
                // Consider using CursorLoader to perform the query. 
                Cursor cursor = getContentResolver()
                        .query(contactUri, projection, null, null, null);
                cursor.moveToFirst();
     
                // Retrieve the phone number from the NUMBER column 
                int column = cursor.getColumnIndex(Phone.NUMBER);
                String number = cursor.getString(column);
     
                // Do something with the phone number... 
            } 
        } 
    }
    

      

    下面的代码是处理调用系统相机返回的结果:
    @Override 
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
            Bundle extras = data.getExtras();
            Bitmap imageBitmap = (Bitmap) extras.get("data");
            mImageView.setImageBitmap(imageBitmap);
        } 
    }
    

      

    获取启动Intent:

    在被启动的Activity中你可以接收启动这个Activity的Intent,在生命周期范围内都能调用getIntent()来获取这个Intent,但是一般都是在onCreat和onStart函数中获取,下面就是一个获取Intent的例子:

    @Override 
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
     
        setContentView(R.layout.main);
     
        // Get the intent that started this activity 
        Intent intent = getIntent();
        Uri data = intent.getData();
     
        // Figure out what to do based on the intent type 
        if (intent.getType().indexOf("image/") != -1) {
            // Handle intents with image data ... 
        } else if (intent.getType().equals("text/plain")) {
            // Handle intents with text ... 
        } 
    }
    

      

    设置返回Intent:

    上面介绍了怎么在onActivityResult()中处理Intent,但是怎么在你的应用中设置这个返回Intent呢?如果你想给调用你的Activity返回一个结果可以通过调用setResult()设置返回内容,然后结束这个Activity。下面的代码是一个示例:

    // Create intent to deliver some kind of result data 
    Intent result = new Intent("com.example.RESULT_ACTION", Uri.parse("content://result_uri");
    setResult(Activity.RESULT_OK, result);
    finish();
    

      

    以上就是使用Intent在不同Activity进行信息传递和沟通的讲解,到此Intent系列文章完结,前两篇文章是关于Intent详解Intent使用的文章,有什么不明白的请留言,大家共同学习,共同进步,谢谢!

    大家如果对编程感兴趣,想了解更多的编程知识,解决编程问题,想要系统学习某一种开发知识,我们这里有java高手,C++/C高 手,windows/Linux高手,android/ios高手,请大家关注我的微信公众号:程序员互动联盟or coder_online,大牛在线为您提供服务。

    【答疑解惑】Java类的初始化顺序

  • 相关阅读:
    【stanford】梯度、梯度下降,随机梯度下降
    [philosophy]空间
    【crawler】heritrix 3 使用
    【database】database domain knowledge
    【java】Java异常处理总结
    【computer theory】一、集合、关系和语言
    【java】ubuntu部署web项目war包到tomcat上
    【MachineLeaning】stanford lesson one
    【数据立方】由表和电子数据表到数据立方体,cuboid方体
    PHP变参函数的实现
  • 原文地址:https://www.cnblogs.com/2010wuhao/p/4738373.html
Copyright © 2020-2023  润新知