Android-BroadcastReceiver 向 Activity 传值(电量显示)

来源:互联网 发布:微信公众号java php 编辑:程序博客网 时间:2024/06/05 19:25
BroadcastReceiver 向 Activity 传值
MainActivity实现了setBatteryText接口在Activity中调用BatteryChange中的getBatteryText(setBatteryText setBattery)方法把当前对象传到BatteryChange中setBatteryTxt要将数据传给Activity,只要调用 实现接口的Activity中的setTxt
如图:
结果图:
manifest文件
<pre name="code" class="java"><?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"    package="com.example.yulongji.android6" >    <application        android:allowBackup="true"        android:icon="@mipmap/ic_launcher"        android:label="@string/app_name"        android:theme="@style/AppTheme" >        <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=".BatteryChange"            android:enabled="true"            android:exported="true" >        </receiver>    </application></manifest>


MainActivity.java
package com.example.yulongji.android6;import android.app.Activity;import android.content.Intent;import android.content.IntentFilter;import android.os.Bundle;import android.widget.TextView;/*BroadcastReceiver 向 Activity 传值//MainActivity实现了setBatteryText接口在Activity中调用BatteryChange中的getBatteryText(setBatteryText setBattery)方法把当前对象传到BatteryChange中setBatteryTxt要将数据传给Activity,只要调用 实现接口的Activity中的setTxt */public class MainActivity extends Activity implements BatteryChange.setBatteryText {    private TextView text;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        text = (TextView) findViewById(R.id.text);        IntentFilter filter = new IntentFilter();        filter.addAction(Intent.ACTION_BATTERY_CHANGED);        BatteryChange batteryChange = new BatteryChange();        //注册广播接收者        registerReceiver(batteryChange, filter);        //把当前对象传给BatteryChange        batteryChange.getBatteryText(this);    }    @Override    public void setTxt(String content) {        if (content != null) {            text.setText(content);        }    }}

BatteryChange.java
package com.example.yulongji.android6;import android.content.BroadcastReceiver;import android.content.Context;import android.content.Intent;import android.os.Bundle;public class BatteryChange extends BroadcastReceiver {private setBatteryText setBattery;    @Override    public void onReceive(Context context, Intent intent) {        Bundle bundle = intent.getExtras();        //获取当前电量        int current = bundle.getInt("level");        //获取总电量(电池的电池容量)        int total = bundle.getInt("scale");        setBattery.setTxt("当前电量:" + current + ", 总电量:" + total);    }    public interface setBatteryText{        void setTxt(String content);    }    public void getBatteryText(setBatteryText setBattery){        this.setBattery = setBattery;    }}


                                             
0 0
原创粉丝点击