android基础:广播BroadCast简单案例

运行效果

android基础:广播BroadCast简单案例

 

string.xml

<?xml version="1.0" encoding="utf-8"?>

<resources>



    <string name="app_name">TestBroadcast</string>

    <string name="param_input">请输入发送参数</string>

    <string name="send_button">发送广播</string>    

    <string name="action_settings">Settings</string>

</resources>

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:tools="http://schemas.android.com/tools"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:paddingBottom="@dimen/activity_vertical_margin"

    android:paddingLeft="@dimen/activity_horizontal_margin"

    android:paddingRight="@dimen/activity_horizontal_margin"

    android:paddingTop="@dimen/activity_vertical_margin"

    tools:context="com.mytest.testbroadcast.MainActivity" >



    <EditText

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:hint="@string/param_input" 

        android:id="@+id/param_input"/>

    <Button

        android:id="@+id/send_button"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_toRightOf="@id/param_input"

        android:text="@string/send_button"

        android:onClick="SendBroadCast" /> 

        

    <TextView

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_below="@id/param_input"

        android:text="@null"

        android:id="@+id/view_result"

       />    

    



</RelativeLayout>

 

MainActivity 

package com.mytest.testbroadcast;



import android.app.Activity;

import android.content.Intent;

import android.os.Bundle;

import android.view.Menu;

import android.view.MenuItem;

import android.view.View;

import android.widget.EditText;

import android.widget.TextView;

import android.widget.Toast;



public class MainActivity extends Activity {

	

	private EditText paramText;

	private TextView resultView;

	private final String ACTION_INTENT_TEST="com.mytest.testbroad.intent";



	@Override

	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);

		setContentView(R.layout.activity_main);

		

		paramText = (EditText) this.findViewById(R.id.param_input);

		resultView = (TextView) this.findViewById(R.id.view_result);

	}



	

	/**

	 * 发送广播按钮onclick事件

	 * @param view

	 */

	public void SendBroadCast(View view){

		String param = paramText.getText().toString();



		

		//创建发送intent

		Intent intent = new Intent(this.ACTION_INTENT_TEST);

		//绑定参数

		intent.putExtra("user_input", param);

		//发送广播

		this.sendBroadcast(intent); 

		

		resultView.setText("发送广播成功,参数值为:"+param);

		

		

	}

	

}

 

创建广播接听对象

package com.mytest.testbroadcast;



import android.content.BroadcastReceiver;

import android.content.Context;

import android.content.Intent;

import android.widget.Toast;



public class MyBroadCastReceiver extends BroadcastReceiver {



	@Override

	public void onReceive(Context context, Intent intent) {

		// TODO Auto-generated method stub

		String user_input = intent.getStringExtra("user_input");

		Toast.makeText(context,"接收到广播,得到参数值为"+user_input,Toast.LENGTH_SHORT).show();

		

	}



}

  

 AndroidMainfest.xml

<?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android="http://schemas.android.com/apk/res/android"

    package="com.mytest.testbroadcast"

    android:versionCode="1"

    android:versionName="1.0" >



    <uses-sdk

        android:minSdkVersion="14"

        android:targetSdkVersion="21" />



    <application

        android:allowBackup="true"

        android:icon="@drawable/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=".MyBroadCastReceiver">

            <intent-filter>

                <action android:name="com.mytest.testbroad.intent"/>

            </intent-filter>

            

        </receiver>

    </application>



</manifest>

  

案例实现小结:
1、定义ACTION,并建立发送Intent,调用sendBroadcast方法发送广播;
2、创建广播接收类。类要继承BroadcastReceiver,并重写onReceive方法;
3、在AndroidManifest.xml中注册广播接收对象,在intent-filter中添加ACTION。

 

你可能感兴趣的:(Broadcast)