笔记 Android广播初使用

来源:互联网 发布:java api接口开发 编辑:程序博客网 时间:2024/06/06 20:38


注册方式

所谓动态静态注册在形式上的区别是
静态:
receiver android:name=".MyBroadcastReceiver" >
    <intent-filter>
        <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
    </intent-filter>
    <intent-filter>
        <action android:name="android.intent.action.BOOT_COMPLETED" />
    </intent-filter>
</receiver>
动态:
// 注册广播
myReceive = new myReceviceBroad();
IntentFilter filter = new IntentFilter();
filter.addAction("com.guochaolang");
registerReceiver(myReceive, filter);

在实际上需要注意动态注册的既然注册了也需要取消注册
unregisterReceiver(myReceive);
这个在程序退出时也需要做下



这里是在一个文件中去写的一个广播发送接收实例

注册》》》》》》

myReceviceBroad myReceive;

自定义一个广播接收类
public class myReceviceBroad extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO Auto-generated method stub
        // 得到广播中获取的数据,并显示出来
        String msg = intent.getStringExtra("data");
        tv1.setText(msg);
        Log.i("--guo--", msg);
    }
}

// 注册广播
myReceive = new myReceviceBroad();
IntentFilter filter = new IntentFilter();
filter.addAction("com.guochaolang");
registerReceiver(myReceive, filter);

取消注册》》》》》》》》》
// 取消广播
unregisterReceiver(myReceive);

发送广播》》》》》》》》》》》
// 发送广播
Intent intent = new Intent();
intent.putExtra("data", et1.getText().toString());
//intent.putExtra("data", "this is data from broadcast "+ Calendar.getInstance().get(Calendar.SECOND));
intent.setAction("com.guochaolang");
sendBroadcast(intent);

注意1:
注册有两种方式第一种就是本文这样,另一种就是manifest.xml文件注册
这两种注册选择一种就好了,这里我选择的是用一个内部类去做广播,没有选择第二种注册
如下:
<receiver android:name="com.example.texttospeech.MainActivity$myReceviceBroad">
    <intent-filter>
        <action android:name="com.guochaolang" />
    </intent-filter>
</receiver>

注意2:发送广播的部分可以移植到Service等部分从而实现动态更新界面等操作



全部代码:
public class MainActivity extends Activity implements OnClickListener {
    Button b1, b2, b3;
    TextView tv1;
    EditText et1;
    myReceviceBroad myReceive;

    public class myReceviceBroad extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            // TODO Auto-generated method stub
            // 得到广播中获取的数据,并显示出来
            String msg = intent.getStringExtra("data");
            tv1.setText(msg);
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        tv1 = (TextView) findViewById(R.id.textView1);
        et1 = (EditText) findViewById(R.id.editText1);
        b1 = (Button) findViewById(R.id.button1);
        b2 = (Button) findViewById(R.id.button2);
        b3 = (Button) findViewById(R.id.button3);

        b1.setOnClickListener(this);
        b2.setOnClickListener(this);
        b3.setOnClickListener(this);

    }

    @Override
    public 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;
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        switch (v.getId()) {
        case R.id.button1:
            // TODO Auto-generated method stub
            // 注册广播
            myReceive = new myReceviceBroad();
            IntentFilter filter = new IntentFilter();
            filter.addAction("com.guochaolang");
            registerReceiver(myReceive, filter);
            Toast.makeText(getApplicationContext(), "注册广播成功", Toast.LENGTH_LONG).show();
            break;
        case R.id.button2:
            // TODO Auto-generated method stub
            // 取消广播
            unregisterReceiver(myReceive);
            break;
        case R.id.button3:
            // TODO Auto-generated method stub
            // 发送广播
            Intent intent = new Intent();
            intent.putExtra("data", et1.getText().toString());
            intent.setAction("com.guochaolang");
            sendBroadcast(intent);
            break;

        default:
            break;
        }
    }
}


笔记:
intent.putExtra("data", sum);对于这种传递数据的方式用来传递字符数组可能很习惯,但是获取数字类型就会陌生些

int mun = intent.getIntExtra("data", 0);

getIntExtra(String name, int defaultValue)
这里的defaultValue是一个默认值,当这个key的数字没获取到就给这个默认值


理解:广播定义了一个接收器后就可以在其他地方去发送广播,哪个地方需要接收数据或消息就可以在某个地方定义一个这样的广播接收器





0 0