android广播的实现

来源:互联网 发布:陆金所网络投融资平台 编辑:程序博客网 时间:2024/05/12 22:36

配置广播接收者
1 在清单文件中配置(静态广播)
2 使用 Java 代码 (动态广播)

<application>               <receiver android:name=".MyReceiver">           <intent-filter>               <action android:name="com.zking.android_recece01"/>          </intent-filter>      </receiver>  </application>   3 发送广播发送广播创建一个包含活动名称的Intent,将它传递给sendBroadcast()。Intent i = new Intent("com.zking.android_recece01");sendBroadcast(i);**创建MyPhoneState类继承BroadcastReceiver**1 重写 onReceive() 方法,在EditText中打印intent的活动名称.**获取Activity Context 的办法**package com.hmkcode.android;import android.app.Application;public class MainActivity  extends Application {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);    @Override    public void onCreate() {        super.onCreate();    }}ReceiverResctrictredContext传递给onReceive()方法。需要获取MainActivity context
0 0
原创粉丝点击