【重要组件间的数据传递1】如何将BroadcastReceiver中的数据传递给activity

题目描述

使用broadcastreceiver获得android手机的电池电量,并把电量显示到activity中。

技术分析

用接口传。定义一个接口 让 Activity实现这个接口,然后接受者调用接口里面的方法把要传递的参数传进去。

效果

布局文件

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

    <TextView
            android:id="@+id/text"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="Hello World, MainActivity"
            />
</LinearLayout>

清单文件

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
          package="com.ht.dianliang"
          android:versionCode="1"
          android:versionName="1.0"
        >

    <uses-sdk android:minSdkVersion="7"/>
    <application
            android:label="@string/app_name"
            android:icon="@drawable/ic_launcher"
            >
        <activity
                android:name="MainActivity"
                android:label="@string/app_name"
                >
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>
                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>
        <receiver android:name=".DianLiangBR"/>
    </application>
</manifest>

广播接收者子类书写

package com.ht.dianliang;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

/**
 * Created by annuo on 2015/5/16.
 */
public class DianLiangBR extends BroadcastReceiver {

    private BRInteraction brInteraction;
    @Override
    public void onReceive(Context context, Intent intent) {
        Bundle bundle = intent.getExtras();
        //获取当前电量
        int current = bundle.getInt("level");
        //获取总电量(电池的电池容量)
        int total = bundle.getInt("scale");
        brInteraction.setText("当前电量:" + current + ",总电量:" + total);
    }

    public interface BRInteraction {
        public void setText(String content);
    }

    public void setBRInteractionListener(BRInteraction brInteraction) {
        this.brInteraction = brInteraction;
    }
}

activity书写

package com.ht.dianliang;

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

public class MainActivity extends Activity {

    private TextView textView;

    /**
     * Called when the activity is first created.
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        textView = (TextView) findViewById(R.id.text);
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(Intent.ACTION_BATTERY_CHANGED);
        DianLiangBR dianLiangBR = new DianLiangBR();
        registerReceiver(dianLiangBR, intentFilter);
        dianLiangBR.setBRInteractionListener(new DianLiangBR.BRInteraction() {
            @Override
            public void setText(String content) {
                if (content != null) {
                    textView.setText(content);
                }
            }
        });
    }
}
时间: 2024-08-27 06:06:44

【重要组件间的数据传递1】如何将BroadcastReceiver中的数据传递给activity的相关文章

父子组件间的数据传递

vue当中有个单向数据流的概念,也就是:父组件可以向子组件传递/修改参数(通过属性的方式传),但子组件不可以反过来修改父组件传递过来的参数!(因为怕子组件改了父组件引用类型的数据,可能会影响到其他组件).那怎样解决这个问题?可以复制给子组件自己的变量,然后子组件修改自己的变量啊!这是父组件向子组件传递数据: <!DOCTYPE html> <html> <head> <title></title> <meta http-equiv=&quo

vue2.0 组件之间的数据传递

组件间的数据传递// 父组件<template><div class="order"><dialog-addpro v-on:closedialog="close" :proinfo="proinfo"></dialog-addpro></div></template><script>import daddpro from '../../daddpro' expo

【Vue】浅谈Vue不同场景下组件间的数据交流

浅谈Vue不同场景下组件间的数据“交流” Vue的官方文档可以说是很详细了.在我看来,它和react等其他框架文档一样,讲述的方式的更多的是“方法论”,而不是“场景论”,这也就导致了:我们在阅读完文档许多遍后,写起代码还是不免感到有许多困惑,因为我们不知道其中一些知识点的运用场景.这就是我写这篇文章的目的,探讨不同场景下组件间的数据“交流”的Vue实现 父子组件间的数据交流 父子组件间的数据交流可分为两种: 1.父组件传递数据给子组件 2.子组件传递数据给父组件 父组件传递数据给子组件——pro

Vue组件注册与数据传递

父子组件创建流程 1.构建父子组件 1.1 全局注册 (1)构建注册子组件 //构建子组件child var child = Vue.extend({ template: '<div>这是子组件</div>' }); //注册名为'child'的组件 Vue.component('child',child); (2)构建注册父组件 //构建父组件parent,在其中嵌套child组件 var parent = Vue.extend({ template: '<div>这

C#不同窗体间通信,数据传递

在一个项目中,很多时候都需要在窗体间进行数据传递和通信,最觉见的是父子窗体之间的数据传递,比如登录ID,各个窗体都需要知道.有很多文章都写了这方面的问题,提出很多优秀的方法,鄙人不才,搜了一些资料之后,准备献丑了. 1.       如果很多窗体都需要用到某一窗体的东西,比如登录窗体记录的ID,为了避免每个窗体都去查询数据库,可以把这些公共变量或信息写入配置文件,每个窗体去读配置文件即可. 2.       如果共享信息的窗体不多,则对于兄弟窗口,可以通过其共同的父窗体来传递数据,此时父窗体如果

Vue 爬坑之路(二)—— 组件之间的数据传递

Vue 的组件作用域都是孤立的,不允许在子组件的模板内直接引用父组件的数据.必须使用特定的方法才能实现组件之间的数据传递. 首先用 vue-cli 创建一个项目,其中 App.vue 是父组件,components 文件夹下都是子组件. 一.父组件向子组件传递数据 在 Vue 中,可以使用 props 向子组件传递数据. 子组件部分: 这是 header.vue 的 HTML 部分,logo 是在 data 中定义的变量. 如果需要从父组件获取 logo 的值,就需要使用 props: ['lo

activity间的数据传递

1.创建一个新的activity,然后在AndroidManifest.xml配置文件中完成声明. <application android:label="@string/app_name" android:icon="@drawable/ic_launcher"> <activity android:name="QuizActivity"android:label="@string/app_name">

007-多控制器管理及其控制器间的数据传递

掌握 • 1.控制器以及view的多种创建方式 • 2.UINavigationController的简单使用:添加\移除子控制器 • 3.UINavigationBar内容的设置 • 4.Segue之数据传递 • 1.控制器以及view的多种创建方式 问题一:如何创建一个控制器? •控制器常见的创建方式有以下几种 1>通过storyboard创建控制器 •先加载storyboard文件(Test是storyboard的文件名) UIStoryboard *storyboard = [UISto

007-多控制器管理(控制器间的数据传递)

掌握 • 1.控制器以及view的多种创建方式 • 2.UINavigationController的简单使用:添加\移除子控制器 • 3.UINavigationBar内容的设置 • 4.Segue之数据传递 • 1.控制器以及view的多种创建方式 问题一:如何创建一个控制器? •控制器常见的创建方式有以下几种 1>通过storyboard创建控制器 •先加载storyboard文件(Test是storyboard的文件名) UIStoryboard *storyboard = [UISto