Android广播的静态与动态注册

来源:互联网 发布:剑三脸型数据怎么导入 编辑:程序博客网 时间:2024/05/20 18:50

  静态广播:不用程序启动就可以接收,可用作开机自启动(设备开机时会发送广播,下面列出几项广播的ACTION),当然也能自己设定action的值。
  Intent.ACTION_BOOT_COMPLETED //系统启动完成
  Intent.ACTION_MEDIA_MOUNTED //SD卡挂载
  Intent.ACTION_MEDIA_UNMOUNTED //SD卡卸载
  Intent.ACTION_USER_PRESENT//解除锁屏
  ConnectivityManager.CONNECTIVITY_ACTION//网络状态变化

  动态广播:在代码中注册,程序适应系统变化做操作,动态广播只有在程序运行状态下才能接收到。
  Intent.ACTION_SCREEN_ON //屏幕亮时发出的广播
  Intent.ACTION_SCREEN_OFF //屏幕灭时发出的广播
  Intent.ACTION_TIME_TICK //时间变化 每分钟一次发出的广播

  当然还有很多状态就不一一列举了,用到时自行搜索便是,那就贴一些代码吧。
  

  广播接收器类,用来接收广播。

package com.example.broadcastrecdemo_v1;import android.content.BroadcastReceiver;import android.content.Context;import android.content.Intent;import android.widget.Toast;public class MyBroadcastRec extends BroadcastReceiver{    @Override    public void onReceive(Context context, Intent intent)    {        String action = intent.getAction();        if (action.equals("com.xwk.brcr"))        {            Toast.makeText(context, intent.getStringExtra("jt"),                    Toast.LENGTH_SHORT).show();        } else if (action.equals("com.xwk.dt"))        {            Toast.makeText(context, intent.getStringExtra("dt"),                    Toast.LENGTH_SHORT).show();        }    }}

  界面。
  

<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=".MainActivity" >    <TextView        android:id="@+id/tv"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="@string/hello_world" />    <Button        android:id="@+id/bnj"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_below="@id/tv"        android:text="静态" />    <Button        android:id="@+id/bnd"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_below="@id/bnj"        android:text="动态" /></RelativeLayout>

  Activityi类。
  

package com.example.broadcastrecdemo_v1;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.view.View.OnClickListener;import android.widget.Button;public class MainActivity extends Activity implements OnClickListener{    private Button bnj, bnd;    @Override    protected void onCreate(Bundle savedInstanceState)    {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        bnj = (Button) findViewById(R.id.bnj);        bnd = (Button) findViewById(R.id.bnd);        bnj.setOnClickListener(this);        bnd.setOnClickListener(this);    }    @Override    public void onClick(View v)    {        switch (v.getId())        {        case R.id.bnj:            Intent it1 = new Intent();            it1.setAction("com.xwk.brcr");            it1.putExtra("jt", "这里是静态注册!");            sendBroadcast(it1);            break;        case R.id.bnd:            Intent it2 = new Intent();            it2.setAction("com.xwk.dt");            it2.putExtra("dt", "这里是动态注册!");            sendBroadcast(it2);            break;        default:            break;        }    }    @Override    protected void onStart()    {        super.onStart();        IntentFilter intentFilter = new IntentFilter();        intentFilter.addAction("com.xwk.dt");        registerReceiver(new MyBroadcastRec(), intentFilter);    }}

  静态广播需在AndroidManifest.xml中注册。
  

 <receiver android:name=".MyBroadcastRec" >            <intent-filter>                <action android:name="com.xwk.brcr" />            </intent-filter>        </receiver>

  来看下运行结果。
  这里写图片描述

1 0
原创粉丝点击