How to pass data in BroadcastReceiver to activity and broadcastreceiver
Description
Use broadcastreceiver to obtain the battery power of the android phone and display the power to the activity.
Technical Analysis
Use the API to upload data. Define an interface for the Activity to implement this interface, and then the Receiver calls the methods in the interface to pass in the parameters to be passed.
Effect
Layout File
<?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>
Configuration File
<?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>
Subcategory of broadcast Receiver
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 (); // get the current power int current = bundle. getInt ("level"); // get the total power (battery capacity) int total = bundle. getInt ("scale"); brInteraction. setText ("current power:" + current + ", total power:" + total);} public interface BRInteraction {public void setText (String content );} public void setBRInteractionListener (BRInteraction brInteraction) {this. brInteraction = brInteraction ;}}
Activity writing
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); } } }); }}