(七) android基础介绍--广播接受者

01_为什么需要广播接受者

    广播:
    电台:发送一定频道的广播消息,50mhz,;
    收音机:调整到一定频道,接收广播消息;

    android应用程序里面的电台:系统内置的一个服务,会把事件(电量不足、电量充满、开机启动完成)作为一个广播消息发送其他的接收者;
    android应用程序里面的收音机:自己写的一个广播接收者的一个类。

02_广播接受者案例_ip拨号器(重点)


开发广播接收者的步骤:
      
    1、买个收音机:
        public class OutCallBroadCastReceiver extends BroadcastReceiver {
            public void onReceive(Context context, Intent intent) {
        }
    2、插上电池:
        <receiver android:name="com.itheima.ipcall.OutCallBroadCastReceiver" />
    3、调整到一个频道:
        <intent-filter >
           <action android:name="android.intent.action.NEW_OUTGOING_CALL" />
        </intent-filter>
     

03_广播接受者案例_短信监听器(重点)

    pdus : protocol data unit s 协议数据单元

    特点:
    即使广播接收者没有运行,当广播消息到达的时候,系统会自动启动广播接收者的进程,调用onReceive方法,接收消息。
    4.0版本之后为了安全考虑,要求应用程序必须要有界面,必须被用户运行过一次,广播接受者才会生效
    4.0版本的强行停止相当于冻结一个应用,一旦应用程序被用户强行停止了,广播接受者就不会生效了。直到用户手工打开这个应用程序为止。
    4.0版本之前没有这样的安全设计

步骤:

     1、买个收音机
     2、插上电池       
     3、调整到一个频道  

   配置文件:
    
    <uses-permission android:name="android.permission.RECEIVE_SMS"/>

     <receiver android:name="com.itheima.smslistener.SMSBroadCastReceiver">
            <intent-filter >
                <action android:name="android.provider.Telephony.SMS_RECEIVED" />
            </intent-filter>
        </receiver>
   代码:

  package com.itheima.smslistener;

    import android.content.BroadcastReceiver;
    import android.content.Context;
    import android.content.Intent;
    import android.telephony.gsm.SmsMessage;
    
    public class SMSBroadCastReceiver  extends BroadcastReceiver{
    
        @Override
        public void onReceive(Context context, Intent intent) {
            
            Object[] objs = (Object[]) intent.getExtras().get("pdus");
            for(Object obj : objs){
                SmsMessage sms = SmsMessage.createFromPdu((byte[]) obj);
                String content = sms.getMessageBody();
                
                String srcPhone = sms.getOriginatingAddress();
                
                System.out.println("content========"+content);
                System.out.println("srcPhone========"+srcPhone);
                
        }
    }

    }

04_广播接受者案例_sd卡状态监听(重点)

    测试的时使用2.3的模拟器,4.0之后版本没有卸载、挂载、移除SD卡的功能。

步骤:

     1、买个收音机
     2、插上电池
     3、调整到一个频道    

   配置文件:          
<receiver android:name="com.itheima.sdlistener.SDBroadCastReceiver">
                <intent-filter >
                    <action android:name="android.intent.action.MEDIA_MOUNTED" />
                    <action android:name="android.intent.action.MEDIA_UNMOUNTED" />
                    <action android:name="android.intent.action.MEDIA_REMOVED" />
                 <!-- 必须加上data这个属性 -->
                    <data android:scheme="file"/>
                </intent-filter>
            
        </receiver>
 代码:
 package com.itheima.sdlistener;

    import android.content.BroadcastReceiver;
    import android.content.Context;
    import android.content.Intent;
    import android.widget.Toast;
    
    public class SDBroadCastReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
         
        String action = intent.getAction();
        
        if("android.intent.action.MEDIA_MOUNTED".equals(action)){
            Toast.makeText(context, "已经插上了SD卡.................", 0).show();
        }
        if("android.intent.action.MEDIA_UNMOUNTED".equals(action)){
            Toast.makeText(context, "拔掉了SD卡.................", 0).show();
        }
        
        if("android.intent.action.MEDIA_REMOVED".equals(action)){
            Toast.makeText(context, " 移除了SD卡.................", 0).show();
        }


    
    }
}

05_广播接受者案例_开机启动(重点)

步骤:

     1、买个收音机
     2、插上电池
     3、调整到一个频道        
    要做的事情:让软件开启后关闭不了:
    禁用返回键和最小化键(小房子键);
示例代码   
     配置文件:
  <receiver android:name="com.itheima.lesuo.BootCompletedBroadCastReceiver">
            
            <intent-filter >
                <action android:name="android.intent.action.BOOT_COMPLETED" />
            </intent-filter>
        </receiver>
    </application>
    代码:
    
package com.itheima.lesuo;

    import android.content.BroadcastReceiver;
    import android.content.Context;
    import android.content.Intent;
    
    public class BootCompletedBroadCastReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        
        //开启mainactivity
        Intent i = new Intent(context,MainActivity.class);
        //告诉activity自己来维护任务栈,如果任务栈没有当前任务,就会重新创建一个任务放入任务栈
        i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        
        context.startActivity(i);
        System.out.println("***********88888888888启动完成*********************************");
        }

    }

06_广播接受者案例_卸载安装(重点)

示例代码:

    配置文件:        
 <receiver android:name="com.itheima.azxz.AZXZBroadCastReceiver">
            
            <intent-filter >
                <action android:name="android.intent.action.PACKAGE_INSTALL" />
                <action android:name="android.intent.action.PACKAGE_REMOVED" />
                <action android:name="android.intent.action.PACKAGE_ADDED" />
               <!--   必须添加这个属性 -->
                <data android:scheme="package"/>
            </intent-filter>
        </receiver>
    代码:  
 package com.itheima.azxz;

    import android.content.BroadcastReceiver;
    import android.content.Context;
    import android.content.Intent;
    import android.widget.Toast;
    
    public class AZXZBroadCastReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        

        String action = intent.getAction();
        
        if("android.intent.action.PACKAGE_INSTALL".equals(action)){
            Toast.makeText(context, "安装了一个新软件...................", 0).show();
        }
        
        if("android.intent.action.PACKAGE_REMOVED".equals(action)){
            Toast.makeText(context, "卸载了一个软件...................", 0).show();
        }
        
        if("android.intent.action.PACKAGE_REPLACED".equals(action)){
            Toast.makeText(context, "重新安装了一个软件...................", 0).show();
        }
        
    }
    }

07_发送自定义广播

    创建广播电台的步骤:

        //创建一个传递消息的意图对象
        Intent intent = new Intent();
        
        //设置要广播的事件类型
        intent.setAction("com.itheima.broadcast.HMSSDT");
        
        //设置广播的消息数据
        intent.putExtra("news", "黑马49期,晚上12点半准时开播.........");
        
        //发送一个广播消息
        sendBroadcast(intent);

08_有序广播和无序广播(重点)

无序广播:
        广播接收者只要注册接收相应的事件类型,就能接收到的广播;

          //发送一个广播消息(无序广播)
        sendBroadcast(intent);
有序广播:
        当广播把消息发送出去后,消息会根据广播接收者的优先级从高到低一级一级地下发消息。
        可以拦截消息,也可以修改消息。
   发送有序广播:
        Intent intent = new Intent();        
        intent.setAction("com.itheima.orderedbroadcast.ZYFFNTBT");
        //发送一个有序的广播
        //intent 意图
        //permission 指定接收者需要添加了权限
        //resultReceiver 指定哪个广播接收者最后接到消息
        //scheduler 消息处理器
        //initialCode 给消息指定初始代码
        //initialData 指定消息的数据
        //initialExtras 指定额外的参数
        sendOrderedBroadcast(intent, null, null, null, 1, "国务院开始发放2014年农田补贴:900元", null);
  广播接收者的配置文件:
     <receiver android:name="com.itheima.zf.ProvinceBroadCastReceiver">
            <intent-filter android:priority="1000" >
                <action  android:name="com.itheima.orderedbroadcast.ZYFFNTBT"/>
            </intent-filter>
        </receiver>
  广播接收者的代码:   
  String info = getResultData();
        System.out.println("---------我是省级人民政府,已经接收到了中央发的消息:"+info);
        
       //Toast.makeText(context, "我是省级人民政府,已经接收到了中央发的消息:"+info, 0).show();
        setResultData("国务院开始发放2014年农田补贴:400元");

补间动画     

    /**
     * 透明度变化的动画
     * @param view
     */
    public void alpha(View view) {
        AlphaAnimation aa = new AlphaAnimation(0, 1.0f);
        //动画播放的时间
        aa.setDuration(2000);
        //重复次数
        aa.setRepeatCount(2);
        //设置重复的模式
        aa.setRepeatMode(Animation.REVERSE);
        iv.startAnimation(aa);
    }
    /**
     * 旋转变化的动画
     * @param view
     */
    public void rotate(View view) {
        
        RotateAnimation ra = new RotateAnimation(0, 360, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
        //动画播放的时间
        ra.setDuration(2000);
                //重复次数
        ra.setRepeatCount(2);
        ra.setRepeatMode(Animation.REVERSE);
        iv.startAnimation(ra);
    }
    /**
     * 位移变化的动画
     * @param view
     */
    public void trans(View view) {
        TranslateAnimation ta = new TranslateAnimation(Animation.RELATIVE_TO_PARENT, -0.5f, Animation.RELATIVE_TO_PARENT, 0.5f, 
                Animation.RELATIVE_TO_PARENT, -0.5f, Animation.RELATIVE_TO_PARENT, 0.5f);
        ta.setDuration(2000);
        //重复次数
        ta.setRepeatCount(2);
        ta.setRepeatMode(Animation.REVERSE);
        iv.startAnimation(ta);
        
    }
    /**
     * 缩放变化的动画
     * @param view
     */
    public void scale(View view) {
        ScaleAnimation sa = new ScaleAnimation(0.1f, 2.0f, 0.1f, 2.0f, 
                Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_SELF, 0f);
        sa.setDuration(2000);
        //重复次数
        sa.setRepeatCount(2);
        sa.setRepeatMode(Animation.REVERSE);
        sa.setFillAfter(true);//设置填充after的效果
        iv.startAnimation(sa);
    }
    /**
     * 动画集合
     * @param view
     */
    public void set(View view){
        //动画插入器
        AnimationSet set = new AnimationSet(false);
        RotateAnimation ra = new RotateAnimation(0, 360, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
        //动画播放的时间
        ra.setDuration(2000);
                //重复次数
        ra.setRepeatCount(2);
        ra.setRepeatMode(Animation.REVERSE);
        TranslateAnimation ta = new TranslateAnimation(Animation.RELATIVE_TO_PARENT, -0.3f, Animation.RELATIVE_TO_PARENT, 0.3f, 
                Animation.RELATIVE_TO_PARENT, -0.3f, Animation.RELATIVE_TO_PARENT, 0.3f);
        ta.setDuration(2000);
        //重复次数
        ta.setRepeatCount(2);
        ta.setRepeatMode(Animation.REVERSE);
        ScaleAnimation sa = new ScaleAnimation(0.1f, 2.0f, 0.1f, 2.0f, 
                Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_SELF, 0f);
        sa.setDuration(2000);
        //重复次数
        sa.setRepeatCount(2);
        sa.setRepeatMode(Animation.REVERSE);
        set.addAnimation(ra);
        set.addAnimation(sa);
        set.addAnimation(ta);
        iv.startAnimation(set);
    }


猜你喜欢

转载自blog.csdn.net/wangxueqing52/article/details/80266643
今日推荐