How to pass data in BroadcastReceiver to activity and broadcastreceiver

Source: Internet
Author: User

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);                }            }        });    }}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.