BroadCastRecerver配置例子

  1. 1.凡是广播必须是要到AndroidMainfest中配置信息的

    <receiver android:name=".CallReceive">   -->类的全路径
                <intent-filter >
                    <action android:name="android.intent.action.NEW_OUTGOING_CALL"/>   -->设置的是接收什么样的广播,根据实现的功能而定
                </intent-filter>
     </receiver>





  2. public class AppStatusReceiver extends BroadcastReceiver {  
  3.   
  4.     /** 
  5.      * 在AndroidMainfest中添加: 
  6.      * <receiver android:name=".AppStatusReceiver" > 
  7.             <intent-filter> 
  8.                 <action android:name="android.intent.action.PACKAGE_ADDED" /> 
  9.                 <action android:name="android.intent.action.PACKAGE_REPLACED" /> 
  10.                 <action android:name="android.intent.action.PACKAGE_REMOVED" /> 
  11.  
  12.                 <data android:scheme="package" /> 
  13.             </intent-filter> 
  14.         </receiver> 
  15.         其他的一眼明了 
  16.      */  
  17.     @Override  
  18.     public void onReceive(Context context, Intent intent) {  
  19.         String action=intent.getAction();  
  20.         Uri uri=intent.getData();  
  21.         if("android.intent.action.PACKAGE_ADDED".equals(action)){  
  22.             Toast.makeText(context, uri.toString()+"被安装了"0).show();  
  23.         }  
  24.         if("android.intent.action.PACKAGE_REPLACED".equals(action)){  
  25.             Toast.makeText(context, uri.toString()+"被更新了"0).show();  
  26.         }  
  27.         if("android.intent.action.PACKAGE_REMOVED".equals(action)){  
  28.             Toast.makeText(context, uri.toString()+"被卸载了"0).show();  
  29.         }  
  30.           
  31.     }  
  32.   
  33. }


  1. <receiver android:name=".SDStatusReceiver" > 
  2.             <intent-filter> 
  3.                 <action android:name="android.intent.action.MEDIA_MOUNTED" /> 
  4.                 <action android:name="android.intent.action.MEDIA_REMOVED" /> 
  5.                 <action android:name="android.intent.action.MEDIA_UNMOUNTED" /> 
  6.                 <data android:scheme="file"/> 
  7.             </intent-filter> 
  8.         </receiver> 
  9.         其他一眼明了 
  10.  * @author abc 
  11.  * 
  12.  */  
  13. public class SDStatusReceiver extends BroadcastReceiver {  
  14.   
  15.             @Override  
  16.             public void onReceive(Context context, Intent intent) {  
  17.                 String action=intent.getAction();  
  18.                 if("android.intent.action.MEDIA_MOUNTED".equals(action)){  
  19.                     Toast.makeText(context, "SD可用"0).show();  
  20.                 }  
  21.                 if("android.intent.action.MEDIA_REMOVED".equals(action)){  
  22.                     Toast.makeText(context, "SD被卸载"0).show();  
  23.                 }  
  24.                 if("android.intent.action.MEDIA_UNMOUNTED".equals(action)){  
  25.                     Toast.makeText(context, "SD不可用"0).show();  
  26.                 }  
  27.             }  
  28.   
  29. }  


  1. public class SmsReceiver extends BroadcastReceiver {  
  2.   
  3.     /** 
  4.      * 思路:  
  5.      * 1.拿到短信,封装在intent里面  
  6.      * 2.把intent放在Bundle里面 
  7.      * 2.以pdus为键,取出短信的内容,保存到Object[]里  
  8.      * 3.把object转成byte数组 
  9.      * 注:联想系统存储短信的思路: 
  10.      * 1.把短信转成字节数组  
  11.      * 2.把字节数组存到Object[]数组里面 
  12.      *3.以"pdus"为key,把object[]数组储存到Bundle里面,在把Bundle 存储到Intent里面 
  13.      * 
  14.      *小结: 
  15.      *1.AndroidMainfest需要配置的信息receiver android:name=".SmsReceiver"> 
  16.             <intent-filter android:priority="1000" > 
  17.                     <action android:name="android.provider.Telephony.SMS_RECEIVED"/>               
  18.             </intent-filter>             
  19.         </receiver> 
  20.         
  21.      * 2.加权限:<uses-permission android:name="android.permission.RECEIVE_SMS"/> 
  22.      */  
  23.     @Override  
  24.     public void onReceive(Context context, Intent intent) {  
  25.               
  26.           
  27.             Bundle bundle = intent.getExtras();  
  28.             Object[] obj = (Object[]) bundle.get("pdus");  
  29.             for (Object object : obj)  
  30.                 {  
  31.                     SmsMessage sms=SmsMessage.createFromPdu((byte[]) object);  
  32.                     if(sms.getOriginatingAddress().equals("1111")){  
  33.                         abortBroadcast();  
  34.                     }  
  35.                     System.out.println(sms.getMessageBody());  
  36.                 }  
  37.     }  
  38.   
  39. }  

  1. public class MainActivity extends Activity {  
  2.   
  3.     @Override  
  4.     protected void onCreate(Bundle savedInstanceState) {  
  5.         super.onCreate(savedInstanceState);  
  6.         setContentView(R.layout.activity_main);  
  7.     }  
  8.   
  9.     /** 
  10.      * 思路:1.将EditText的字符提取出来 
  11.      * 2.创建一个SharedPreferences的存储对象 
  12.      * 3.将字符串存储进去(存储的顺序是先获得edit(),然后putstring) 
  13.      * 4.编辑,提交 
  14.      * @param v 
  15.      */  
  16.     public void click(View v){  
  17.         EditText dt = (EditText) findViewById(R.id.ed);  
  18.         SharedPreferences sp=getSharedPreferences("ip", MODE_PRIVATE);  
  19.         Editor ed=sp.edit();  
  20.         ed.putString("ipNumbe",dt.getText().toString());//键值对的形式,前面是键后面是值  
  21.     }  
  22. }  
  23.   
  24. public class CallReceive extends BroadcastReceiver{  
  25.   
  26.     /** 
  27.      * 思路: 
  28.      * 1.获得原来的打电话的号码 
  29.      * 2.通过上下文的getSharedPreference获得之前保存的数据,即文本框传进来的数据) 
  30.      * 3.将看原来的电话加上我们想给他预订的ip 
  31.      * 4.将电话重新返回给广播 
  32.      */  
  33.       
  34.     @Override  
  35.     public void onReceive(Context context, Intent intent) {  
  36.             System.out.println("call...");  
  37.             String phone=getResultData();  
  38.             SharedPreferences sp=context.getSharedPreferences("ip",context.MODE_PRIVATE );  
  39.             String ipNumber=sp.getString("ipNumber""");  
  40.             phone=phone+ipNumber;  
  41.             setResultData("phone");  
  42.             abortBroadcast();  
  43.     }  
  44. }  
  45.   
  46. 同样要在AndroidMainfest配置信息(在系统默认的</activity>之后)  
  47. </span><span style="font-size:12px;">       <receiver android:name=".CallReceive">  
  48.             <intent-filter >  
  49.                 <action android:name="android.intent.action.NEW_OUTGOING_CALL"/>  
  50.             </intent-filter>  
  51.         </receiver>   



评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值