参考:
BroadcastReceiver的实现不难,其实就是三部曲:注册,接收,发送。
但有一点较疑惑的是:当我启动 BroadcastReceiver 应用(必须有界面)后,此时如果我没进行内存清理,则可以进行监听。但是,当我启动 BroadcastReceiver (没有界面)时或 清理内存后, 广播接收就没效。不知道是什么原因导致的。。。
以下是代码:
广播接收器:
package com.example.broadcastTest;import android.content.BroadcastReceiver;import android.content.Context;import android.content.Intent;import android.util.Log;/** * Created by britzlieg on 14-5-21. */public class MyReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { Log.v("MyTab","-->onReceive"); if("13553969709".equals(this.getResultData())) this.setResultData(null); //如果电话是5556,则清空,不让他拨打 }}
主程序代码:
package com.example.broadcastTest;import android.app.Activity;import android.os.Bundle;public class MyActivity extends Activity { /** * Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); }}
主布局代码:
AndroidMainfest.xml