• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • JavaScript
  • ASP.NET
  • PHP
  • 正则表达式
  • AJAX
  • JSP
  • ASP
  • Flex
  • XML
  • 编程技巧
  • Android
  • swift
  • C#教程
  • vb
  • vb.net
  • C语言
  • Java
  • Delphi
  • 易语言
  • vc/mfc
  • 嵌入式开发
  • 游戏开发
  • ios
  • 编程问答
  • 汇编语言
  • 微信小程序
  • 数据结构
  • OpenGL
  • 架构设计
  • qt
  • 微信公众号
您的位置:首页 > 程序设计 >Android > Android之使用Bundle进行IPC,androidbundleipc

Android之使用Bundle进行IPC,androidbundleipc

作者:网友 字体:[增加 减小] 来源:互联网 时间:2017-05-26

网友通过本文主要向大家介绍了android bundle,android sdk bundle,android bundle传值,android adt bundle,android bundle详解等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com

Android之使用Bundle进行IPC,androidbundleipc


一、Bundle进行IPC介绍

四大组件中的三大组件(Activity、Service、Receiver)都是支持在Intent中传递Bundle数据的,由于Bundle实现了Parcelable接口,所以它可以方便地在不同的进程之间传输。当然,传输的数据必须能够被序列化,比如基本类型、实现了Parcelable接口的对象、实现了Serializable接口的对象以及一些Android支持的特殊对象,具体内容可以看Bundle这个类,就可以看到所有它支持的类型。Bundle不支持的类型无法通过它在进程间传递数据。

二、使用方法

1.打包数据发送

Intent intent1 = new Intent(MainActivity.this, ThirdActivity.class);
Bundle bundle = new Bundle();
bundle.putCharSequence("name", "zhangmiao");
bundle.putInt("age", 20);
intent1.putExtras(bundle);
startActivity(intent1);

2.接受数据

Intent intent = getIntent();
Bundle bundle = intent.getExtras();
String name = bundle.getString("name");
int age = bundle.getInt("age");

3.在AndroidManifest.xml中开启多进程

<activity
   ...
   android:process=":remote" />

三、小案例

1.修改activity_main.xml文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context="com.zhangmiao.ipcdemo.MainActivity"
    android:orientation="vertical"
    >
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:text="Bundler">
    </TextView>

    <Button
        android:id="@+id/bundler_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="send message">
    </Button>

</LinearLayout>

2.添加activity_third.xml文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:text="at activity Third" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Activity Third" />

</LinearLayout>

3.添加ThirdActivity类

package com.zhangmiao.ipcdemo;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;

/**
 * Created by zhangmiao on 2016/12/27.
 */
public class ThirdActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_third);
        Intent intent = getIntent();
        Bundle bundle = intent.getExtras();
        String name = bundle.getString("name");
        int age = bundle.getInt("age");
        TextView textView = (TextView) findViewById(R.id.textView1);
        textView.setText("name:" + name + ",age:" + age);
    }
}

4.修改MainActivity类

package com.zhangmiao.ipcdemo;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.os.Messenger;
import android.os.RemoteException;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;


import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.Serializable;

public class MainActivity extends AppCompatActivity {

    private static final String TAG = "MainActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button button = (Button) findViewById(R.id.bundler_button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent1 = new Intent(MainActivity.this, ThirdActivity.class);
                Bundle bundle = new Bundle();
                bundle.putCharSequence("name", "zhangmiao");
                bundle.putInt("age", 20);
                intent1.putExtras(bundle);
                startActivity(intent1);
            }
        });
    }
}

5.修改AndroidManifest.xml文件

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/androi



 
分享到:QQ空间新浪微博腾讯微博微信百度贴吧QQ好友复制网址打印

您可能想查找下面的文章:

  • Android之使用Bundle进行IPC,androidbundleipc

相关文章

  • 2017-05-26android studio导入PullToRefresh教程
  • 2017-05-26tcpdump的移植和使用方法
  • 2017-05-26android动画详解三 动画API概述
  • 2017-05-26【Android】常见问题解答,android
  • 2017-05-26Android开发重点难点1:RelativeLayout(相对布局)详解,
  • 2017-05-26SwipeRefreshLayout下拉刷新,swiperefreshlayout
  • 2017-05-26Android中Window添加View的底层原理
  • 2017-05-26Android Bottom Sheet详解
  • 2017-05-26OKHttp,okhttp使用
  • 2017-05-26手机影音7--视频播放器的基本功能(4),7--基本功能

文章分类

  • JavaScript
  • ASP.NET
  • PHP
  • 正则表达式
  • AJAX
  • JSP
  • ASP
  • Flex
  • XML
  • 编程技巧
  • Android
  • swift
  • C#教程
  • vb
  • vb.net
  • C语言
  • Java
  • Delphi
  • 易语言
  • vc/mfc
  • 嵌入式开发
  • 游戏开发
  • ios
  • 编程问答
  • 汇编语言
  • 微信小程序
  • 数据结构
  • OpenGL
  • 架构设计
  • qt
  • 微信公众号

最近更新的内容

    • Android API Guides---App Widget Host
    • android OrmLite,androidormlite
    • Android之利用HTTP网络通信实现与PHP的交互(三),android网络通信
    • androidStudio通过svn进行版本控制,androidstudiosvn
    • Android中常见的图片加载框架,
    • Kotlin操作符重载:把标准操作加入到任何类中(KAD 17),kotlinkad
    • Android 颜色Color,android颜色color
    • android handler传递消息机制,androidhandler
    • Android 面试题总结(二)
    • 手机安全卫士——通讯卫士-黑名单的管理,安全卫士卫士

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有