首页 > 代码库 > 自定义广播Broadcast

自定义广播Broadcast

/*发送一个自定义广播

* 指定广播目标Action

* 可通过Intent携带消息

* 发送广播消息

*/

private void sendMyBroadcast(){


Intent intent = new Intent("MyReceiver_Action");

intent.putExtra("msg", "发送自定义的广播");

sendBroadcast(intent);

}


//自己写的一个广播类

public class BroadcastReceiverr extends BroadcastReceiver{


@Override

public void onReceive(Context arg0, Intent arg1) {


if(arg1.getAction().equals("MyReceiver_Action")){

String strMsg = arg1.getStringExtra("msg");

Toast.makeText(arg0, "receive: " + strMsg, Toast.LENGTH_SHORT).show();

}

}

}


//manifest里注册自定义广播

<receiver

            android:name="com.example.lightcamera.BroadcastReceiverr" >

            <intent-filter>

                <action android:name="MyReceiver_Action" />

            </intent-filter>

        </receiver>


本文出自 “爬过山见过海” 博客,谢绝转载!