码迷,mamicode.com
首页 > 移动开发 > 详细

Android 广播(Broadcast)

时间:2014-12-01 14:12:15      阅读:256      评论:0      收藏:0      [点我收藏+]

标签:android   style   blog   http   io   ar   color   os   使用   

bubuko.com,布布扣

广播事件的流程
①注册广播事件:注册方式有两种,一种是静态注册,就是在AndroidManifest.xml文件中定义,注册的广播接收器必须要继承BroadcastReceiver;另一种是动态注册,是在程序中使用Context.registerReceiver注册,注册的广播接收器相当于一个匿名类。两种方式都需要IntentFIlter。
②发送广播事件:通过Context.sendBroadcast来发送,由Intent来传递注册时用到的Action。
③接收广播事件:当发送的广播被接收器监听到后,会调用它的onReceive()方法,并将包含消息的Intent对象传给它。onReceive中代码的执行时间不要超过5s,否则Android会弹出超时dialog。

静态注册广播Demo:

manifest

bubuko.com,布布扣
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.rainwii.client"
    android:versionCode="1"
    android:versionName="1.0"
    android:sharedUserId="com.rainwii.share" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="18" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.rainwii.client.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="com.rainwii.client.MyBroadcastReceive"
            >
            <intent-filter>
                <action android:name="ccc"></action>
             </intent-filter>
        </receiver>
    </application>

</manifest>
View Code

MainActivity

bubuko.com,布布扣
package com.rainwii.client;

import com.rainwii.main.R;

import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager.NameNotFoundException;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends Activity {
Button startappbutton;
String str;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        startappbutton= (Button) findViewById(R.id.button1);
        startappbutton.setOnClickListener(new OnClickListener() {
            
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Intent intent = new Intent();
                intent.putExtra("msg", "你好,我是一个广播");
                intent.setAction("ccc");
                intent.setClass(MainActivity.this, MyBroadcastReceive.class);
                MainActivity.this.sendBroadcast(intent);
            }
        });
              
      
    }


    @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;
    }
    
}
View Code

MyBroadcastReceive

bubuko.com,布布扣
package com.rainwii.client;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;

public class MyBroadcastReceive extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
    
        String string=intent.getStringExtra("msg");
        Toast.makeText(context, "广播被收到了!\n"+string, Toast.LENGTH_SHORT).show();
    }
}
View Code

 

Android 广播(Broadcast)

标签:android   style   blog   http   io   ar   color   os   使用   

原文地址:http://www.cnblogs.com/xubuhang/p/4134899.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!