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
- Android-BroadcastReceiver 向 Activity 传值(电量显示)
- Android获取当前电量信息(BroadcastReceiver的使用)
- Android之BroadcastReceiver监听手机电量
- eclipse android 电量显示
- Android开发之使用BroadcastReceiver实时监听电量(源代码分享)
- android 显示电量的widget
- Android四大组件之--BroadcastReceiver,监测手机电量变化
- Android -- BroadcastReceiver 中启动Activity
- Android 显示手机电池的当前电量
- Android 显示手机电池的当前电量
- Android 怎样控制电量百分比是否显示
- Android状态栏显示蓝牙耳机电量
- android 状态栏只显示电量时间
- 45 Android activity 向 fragment 传值
- android电量优化(一)
- android电量优化(二)
- Android 四大组件(Activity、Service、BroadCastReceiver、ContentProvider)
- 【代码】Android -- Activity 、Service、BroadcastReceiver 综合使用
- 导航框架
- 使用JNDI的方式配置c3p0连接池
- IOS-笔记14(UITextField,UITableView)
- Google Alphabet
- Python中文件对象的访问模式
- Android-BroadcastReceiver 向 Activity 传值(电量显示)
- c# winform 使DataGridView的一些总结
- js里面form传值
- windows bat注册ocx文件
- iOS中Tager-Action 应用举例实现 高内聚低耦合
- noi2015程序自动分析
- 一起来学嵌入式之ARM9中断机制-中断信号处理过程及实例
- matlab转C++ matlab与C++混合编程
- NSFetchedResultController讲解