3.一旦广播事件发生了,就会执行广播接受者的onreceive方法
首先看看IP拨号器器:模拟一个“伪基站”,就是 在拨打电话号码前面添加号码,市场上就是修改自己号码,实现对方看到的号码与自己本身号码不一致
学习内容1)初步理解 广播的概念和简单原理 2)复习SharedPreferences知识点,参数的传递。 3)配置广播
Activity
public class MainActivity extends Activity { private EditText et_number; private SharedPreferences sp; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); et_number=(EditText) findViewById(R.id.et_number); sp=getSharedPreferences("config", MODE_PRIVATE); } public void save(View view){ String ipnumber=et_number.getText().toString().trim(); if(TextUtils.isEmpty(ipnumber)){ Toast.makeText(this, "清楚IP号码成功", 0).show(); }else{ Toast.makeText(this, "设置IP号码成功", 0).show(); } Editor editor=sp.edit(); editor.putString("ipnumber", ipnumber); editor.commit(); } }
/* * 创建一个收音机,继承广播接受者 * */ public class OutCallReceiver extends BroadcastReceiver{ @Override public void onReceive(Context context, Intent intent) { //当接收到消息对应的方法 String number=getResultData(); System.out.println("哈哈,有电话打出去了"); SharedPreferences sp = context.getSharedPreferences("config", Context.MODE_PRIVATE); String ipnumber=sp.getString("ipnumber", ""); //判断是否是长途,是否有前缀 setResultData(ipnumber+number); } }
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.example.ip.MainActivity" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="请设置IP号码" /> <EditText android:id="@+id/et_number" android:layout_width="fill_parent" android:layout_height="wrap_content" android:inputType="phone" /> <Button android:onClick="save" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="保存" /> </LinearLayout>
在清单文件中需要配置:receiver
<receiver android:name=".OutCallReceiver"> <intent-filter> <action android:name="android.intent.action.NEW_OUTGOING_CALL"/> </intent-filter> </receiver>
SD卡移出下载的广播
需要加的广播配置:
<receiver android:name="com.itheima.sdcardmointor.SDStatusReceiver"> <intent-filter> <action android:name="android.intent.action.MEDIA_UNMOUNTED"/> <action android:name="android.intent.action.MEDIA_REMOVED"/> <data android:scheme="file"></data> </intent-filter> </receiver>
public class SDStatusReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { Toast.makeText(context, "sd卡别移除,微信头像或者图片暂时不可用", 1).show(); } }
短信和电话的广播:
<receiver android:name="com.itheima.smsreceiver.SmsReceiver"> <intent-filter android:priority="1000"><!-- 通知的优先级,android中优先级-1000~1000 --> <action android:name="android.provider.Telephony.SMS_RECEIVED"/> </intent-filter> </receiver> <!-- 配置广播接受者 --> <receiver android:name="com.itheima.smsreceiver.OutCallReceiver"> <intent-filter android:priority="1000"> <!-- 配置广播接收者关心的事件是外拨电话 --> <action android:name="android.intent.action.NEW_OUTGOING_CALL"/> </intent-filter> </receiver>
某人电话打不出去:【可以做一个黑名单】
/** * 1.创建一个收音机 继承广播接受者 * */ public class OutCallReceiver extends BroadcastReceiver { //当接收到消息对应的方法 @Override public void onReceive(Context context, Intent intent) { String number = getResultData(); if("15555215556".equals(number)){ setResultData(null); } } }
public class SmsReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { System.out.println("短信到来了。 。。。"); <strong>Object[] objs = (Object[]) intent.getExtras().get("pdus"); for (Object obj : objs) { // 得到短信对象 SmsMessage smsMessage = SmsMessage.createFromPdu((byte[]) obj); String body = smsMessage.getMessageBody(); String sender = smsMessage.getOriginatingAddress();</strong> // 终止掉当前的广播。 if ("15555215556".equals(sender)) { abortBroadcast(); } System.out.println("body:" + body); System.out.println("sender:" + sender); } } }
上面的都是系统自带的广播,很多时候需要自定义广播。这个时候设计到一个程序发送广播,另外一个程序接收到广播就要触发事件。
下面一个程序:广播发送,负责发送广播,
另外一个程序: 警察 :接收广播,触发事件
发送广播程序:【只需要定义一个action,广播一下。
public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } /* * 发送广播事件 */ public void click(View view) { Intent intent=new Intent(); //自定义一个广播动作 <strong>intent.setAction("yyy");</strong> //大吼一声,把消息发出去了。 //无序广播 sendBroadcast(intent); //发送有序广播 //sendBroadcast(intent, receiverPermission); } }
警察程序:
注册广播
<receiver android:name=".PoliceReceiver"> <intent-filter > <strong><action android:name="yyy"/> //就是这个action</strong> </intent-filter> </receiver> </application>
public class PoliceReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { Toast.makeText(context, "我是警察。。。。", 1).show(); } }
广播接收者分类:Android中广播接收者其实分为两类,如下有序广播和无序广播
下面通过一个案列说明:
MainActivity.java Level1Receiver.java Level2Receiver.java Level3Receiver.java FinalReceiver.java
/** * 发送无序广播 * @param view */ public void send1(View view){ <strong>Intent intent = new Intent(); intent.setAction("com.itheima.broadcasttest.songwennuan"); intent.putExtra("msg", "发1万块"); //无序广播,不可被拦截,不可终止。 sendBroadcast(intent);</strong> } /** * 发送有序广播 * @param view */ public void send2(View view){ <strong>Intent intent = new Intent(); intent.setAction("com.itheima.broadcasttest.songwennuan"); //有序广播,可被拦截,可终止,可以修改数据。 sendOrderedBroadcast(intent, null,null, null, 0, "给农民兄弟发10000块钱", null);</strong> } }
public class Level1Receiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) {
<strong>//String message = getResultData(); //有序反送接收数据的格式 String message = intent.getStringExtra("msg");</strong> System.out.println("省级部门得到中央的消息:"+message); //abortBroadcast(); //如果要在有序中使用这个方法需要在发送事件中修改【第三个参数】
//api:sendOrderedBroadcast(intent, null, new FinalReceiver(), null, 0, "给农民兄弟发10000块钱", null); setResultData("给农民兄弟发5000块钱"); } }
public class Level2Receiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { <pre name="code" class="java" style="font-size: 13.3333339691162px;"> //String message = getResultData(); //有序反送接收数据的格式 String message = intent.getStringExtra("msg"); //无序发送接收数据的格式System.out.println("市级部门得到省级的消息:"+message);setResultData("给农民兄弟发2000块钱");}}
public class Level3Receiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { //String message = getResultData(); String message = intent.getStringExtra("msg"); System.out.println("乡级部门得到市的消息:"+message); setResultData("给农民兄弟发两大大米"); } }
public class FinalReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { //String message = getResultData(); String message = intent.getStringExtra("msg"); System.out.println("农民兄弟得到乡的消息:"+message); } }
配置文件,需要配置优先级:
<receiver android:name="com.itheima.broadcasttest.Level1Receiver" > <intent-filter android:<strong>priority</strong>="1000" > <action android:name="com.itheima.broadcasttest.songwennuan" /> </intent-filter> </receiver> <receiver android:name="com.itheima.broadcasttest.Level2Receiver" > <intent-filter android:<strong>priority</strong>="500" > <action android:name="com.itheima.broadcasttest.songwennuan" /> </intent-filter> </receiver> <receiver android:name="com.itheima.broadcasttest.Level3Receiver" > <intent-filter android:<strong>priority</strong>="100" > <action android:name="com.itheima.broadcasttest.songwennuan" /> </intent-filter> </receiver> <receiver android:name="com.itheima.broadcasttest.FinalReceiver" > <intent-filter android:<strong>priority</strong>="0" > <action android:name="com.itheima.broadcasttest.songwennuan" /> </intent-filter> </receiver>