Broadcast Receiver(1)

来源:互联网 发布:centos yum安装jdk 编辑:程序博客网 时间:2024/04/29 07:25

Broadcast Receiver

他是Android系统提供的一个用于组件和组件之间通信的结构

可以在应用程序间通信,在操作系统中的事件可以监听得到的,例如低电量的提示广播,一旦收到低电量的提示,我们可以将应用相关功能关闭。我们还可以监听收到的短信,联系人给你发送的短信,系统可以发送一个广播,我们可以写一个收发应用程序的短信,来进行相关提示。可以监听操作系统启动完毕的事件,执行相关操作,启动服务或者其他的事情。

 

使用很方便,但是运行效率不高。要需要传递大数据一定不能使用broadcast receive。

 

声明方法:跟service差不多,都是新建一个类继承broadcastReceiver

静态注册广播方法:

在AndroidManifest.xml文件中这样注册

                <category android:name="android.intent.category.LAUNCHER" />            </intent-filter>        </activity>    <receiver android:name="MyBC"></receiver>     </application></manifest>
</pre><span style="color: rgb(85, 85, 85); font-family: 'microsoft yahei'; font-size: 15.5555562973022px; line-height: 35px;"><span style="color: rgb(85, 85, 85); font-family: 'microsoft yahei'; font-size: 15.5555562973022px; line-height: 35px;">注意,这种方式的注册是常驻型的,也就是说当应用关闭后,如果有广播信息传来,所定一的BroadCast Receiver也会被系统调用而自动运行。</span></span></p><p><pre name="code" class="java">public void onClick(View v) {Intent intent = new Intent(MainActivity.this, MyBC.class);//Intent intent = new Intent(MyBC.ACTION);intent.putExtra("text", "这是广播实例");sendBroadcast(intent);}});
在Mainactivity中监控按钮,在按钮的监控中实现该方法。首先定义一个intent然后传入packageContext和指向的broadcast Receiver类,通过这种方法传递广播,然而我们得知,在intent中放置数据也是可以的,因此我们可以通过放置数据来实现广播对用户的提示。

public class MyBC extends BroadcastReceiver {@Overridepublic void onReceive(Context arg0, Intent arg1) { String msg=arg1.getStringExtra("text"); System.out.println(msg);        Toast.makeText(arg0, msg, Toast.LENGTH_LONG).show();  }

在上面继承自broadcast Receiver的文件中,我们对传来的广播进行界面提示。当广播传来后,系统将调用onReceive(),传来的数据就被存储在arg1中。


动态注册广播方法:

  <receiver android:name="MyBC"></receiver> 

动态注册广播在manifest中并不需要这一句话来注册,因为是动态注册。所以我们在主activity中需要另外两个按钮来注册和取消注册广播

package com.example.l004usingbc;import android.app.Activity;import android.content.Intent;import android.content.IntentFilter;import android.os.Bundle;import android.view.Menu;import android.view.View;import android.widget.Button;public class MainActivity extends Activity {private Button btnSendBC,btnRegBCR,btnUnRegBCR;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);btnSendBC= (Button) findViewById(R.id.btnSendBC);btnRegBCR=(Button) findViewById(R.id.btnRegBCR);btnUnRegBCR = (Button) findViewById(R.id.btnUnRegBCR);btnSendBC.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {Intent intent = new Intent(MainActivity.this, MyBC.class);//Intent intent = new Intent(MyBC.ACTION);intent.putExtra("text", "这是广播实例");sendBroadcast(intent);}});btnRegBCR.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {registerReceiver(mybc,new IntentFilter(MyBC.ACTION));}});btnUnRegBCR.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {// TODO 自动生成的方法存根unregisterReceiver(mybc);}});}private final MyBC mybc = new MyBC();@Overridepublic boolean onCreateOptionsMenu(Menu menu) {// Inflate the menu; this adds items to the action bar if it is present.getMenuInflater().inflate(R.menu.main, menu);return true;}}
第二个按钮的作用就是为了注册广播。
动态注册需要在代码中指定广播的地址并注册,因此
new IntentFilter(MyBC.ACTION)
这个东西就是指定了广播的地址,ACTION是我们指定的地址,我们在MyBC中有定义到ACTION;这个ACTION最好是这样定义,包名.intent.action.类名

package com.example.l004usingbc;import android.content.BroadcastReceiver;import android.content.Context;import android.content.Intent;import android.util.Log;import android.widget.Toast;public class MyBC extends BroadcastReceiver {public static final String ACTION = "com.example.usingbc.intent.action.MyBC";@Overridepublic void onReceive(Context arg0, Intent arg1) { String msg=arg1.getStringExtra("text"); System.out.println(msg);        Toast.makeText(arg0, msg, Toast.LENGTH_LONG).show();  }public static void main(String[] args) {}}



0 0
原创粉丝点击