android--(BroadcastReceiver)

这里写图片描述

这里写图片描述

这里写图片描述

这里写图片描述

//普通默认的广播:
1.广播接收器:

public class MyReceiver1 extends BroadcastReceiver {
    public MyReceiver1() {
    }


    //接收广播信息
    @Override
    public void onReceive(Context context, Intent intent) {


        String i = intent.getStringExtra("info");

        Toast.makeText(context, i, Toast.LENGTH_SHORT).show();

    }
}

2.activity类:
public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void sendNormal(View v) {

        Intent intent = new Intent("com.ke.action.MY_BROADCAST");//action字符相当于广播的频率一样
        intent.putExtra("info", "this is boradcast");//要发送广播的内容
        this.sendBroadcast(intent);//发送广播

    }
}

这里写图片描述

这里写图片描述

public class MyReceiver2 extends BroadcastReceiver {
    public MyReceiver2() {
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        Toast.makeText(context, "动态注册的广播接收器", Toast.LENGTH_SHORT).show();
    }
}
///在清单文件中要将MyReceiver2 的注册删除,这里才用代码注册的方式
public class MainActivity extends Activity {


    private MyReceiver2 myReceiver2 = new MyReceiver2();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

   //启动activity的时侯就进行注册了
    //在该方法进行广播注册
    @Override
    protected void onResume() {
        super.onResume();
        IntentFilter filter = new IntentFilter();
        filter.addAction("com.ke.action.MY_BROADCAST");

        registerReceiver(myReceiver2, filter);
    }


    //在该方法中进行解除广播注册
    @Override
    protected void onPause() {
        super.onPause();
        unregisterReceiver(myReceiver2);
    }
}

这里写图片描述

这里写图片描述

1.activity类
public class MainActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }


    public void sendOrderClick(View v) {

        Intent intent = new Intent("com.andorid.keke");

        //参数:intent ,接收权限:接收广播接收器需的权限
        this.sendOrderedBroadcast(intent, null);

    }


}


2.广播接收器
public class MyReceiver extends BroadcastReceiver {
    public MyReceiver() {
    }

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

        Toast.makeText(context, "有序广播接收器-1",Toast.LENGTH_SHORT).show();

        //中断有序广播
        //this.abortBroadcast();

        Bundle data = new Bundle();
        data.putString("info","广播1");

        this.setResultExtras(data);//将数据发送到下一个广播接收器中
    }
}


public class MyReceiver2 extends BroadcastReceiver {
    public MyReceiver2() {
    }

    @Override
    public void onReceive(Context context, Intent intent) {
       Bundle data =  getResultExtras(false);// false:没有的话就不创建
        Toast.makeText(context, data.getString("info").toString(), Toast.LENGTH_SHORT).show();
    }
}

3.配置清单文件:
 <receiver
            android:name=".MyReceiver"
            android:enabled="true"
            android:exported="true" >
            <intent-filter   android:priority="1000">
                <action android:name="com.andorid.keke"></action>

            </intent-filter>

        </receiver>
        <receiver
            android:name=".MyReceiver2"
            android:enabled="true"
            android:exported="true" >

            <intent-filter android:priority="200">
                <action android:name="com.andorid.keke"></action>

            </intent-filter>
        </receiver>

这里写图片描述

这里写图片描述

这里写图片描述

这里写图片描述

这里写图片描述


这里写图片描述

这里写图片描述

  public void mmsClick(View v){
        SmsManager sm = SmsManager.getDefault();

        String message = "一条短线";

       ArrayList<String> list =  sm.divideMessage(message);

        for (int i=0;i<list.size();i++){
            sm.sendTextMessage("5556",null,list.get(i),null,null);
        }

    }

这里写图片描述

public class MyReceiverR extends BroadcastReceiver {
    public MyReceiverR() {
    }

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

        Bundle bundle = intent.getExtras();

        if(bundle !=null){
            Object[] objects = (Object[]) bundle.get("pdus");

            SmsMessage[] smsMessages = new SmsMessage[objects.length];

            for (int i=0;i<objects.length;i++){
                smsMessages[i] = SmsMessage.createFromPdu((byte[]) objects[i]);

                //发送方的号码
              String number =   smsMessages[i].getDisplayOriginatingAddress();
                //获取短线的内容
               String cont =  smsMessages[i].getDisplayMessageBody();


                Toast.makeText(context,number +":"+cont,Toast.LENGTH_SHORT).show();
            }
        }

    }
}

//清单配置
        <receiver
            android:name=".MyReceiverR"
            android:enabled="true"
            android:exported="true" >

            <intent-filter>
                <action android:name="android.provider.Telephony.SMS_RECEIVED"></action>

            </intent-filter>

        </receiver>

这里写图片描述

这里写图片描述

这里写图片描述

这里写图片描述

activity:
    public  void startAlarmClick(View v){

        //获取到了系统的闹钟服务
        AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);

        //触发闹钟的时间
        long triggerTime =  System.currentTimeMillis()+3000;


        Intent intent  = new Intent(this,MyReceiverR.class);

// 可以get 广播,服务,activity 类型 
        PendingIntent op = PendingIntent.getBroadcast(this,0,intent,PendingIntent.FLAG_UPDATE_CURRENT);

        //只会执行一次的闹钟
      //  am.set(AlarmManager.RTC,triggerTime,op); //op : 启动这个闹钟的操作,也就是干什么事.

        //指定时间重复执行
        am.setRepeating(AlarmManager.RTC,triggerTime,2000,op); //2000 : 间隔时间,多长时间重复一次

        //am.cancel(op);//取消操作

    }

2.广播接收器:
public class MyReceiverR extends BroadcastReceiver {
    public MyReceiverR() {
    }

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

        Toast.makeText(context,"闹钟",Toast.LENGTH_SHORT).show();

    }
}

这里写图片描述

1.activity:
   public  void  startSet(View v){
        AlarmManager ams = (AlarmManager) getSystemService(Context.ALARM_SERVICE);

        long triggerTime = System.currentTimeMillis() + 3000;

        Intent intent = new Intent(this,MainActivity2Activity2.class);

        startActivity(intent);

    }


2.另一个activity,打开音乐播放器,并自动调用对话框:
public class MainActivity2Activity2 extends Activity {

    MediaPlayer mp; //媒体播放对象

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        //唤醒屏幕
        Window win = getWindow();
        win.addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD|
        WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);

        win.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON|
        WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);

        setContentView(R.layout.activity_main_activity2_activity2);

        mp = new MediaPlayer();
        try {
            mp.setDataSource(this, Uri.parse("/sdcard/music/nl.mp3"));
            mp.prepare(); //状态,预备状态
        } catch (IOException e) {
            e.printStackTrace();
        }

        mp.setLooping(true); //循环
        mp.start();  //启动
        alarmDialog();  //弹出对话框

    }


    @Override
    protected void onResume() {
        super.onResume();



    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        if(mp !=null){
            if(mp.isPlaying()){
                mp.stop();
            }

            mp.release(); //释放资源
        }
    }

    public  void alarmDialog(){

        AlertDialog.Builder builder = new AlertDialog.Builder(this);

        builder.setMessage("起床了"); //弹出框提示内容

        //弹出框按钮
        builder.setPositiveButton("再来一次", new DialogInterface.OnClickListener(){

            @Override
            public void onClick(DialogInterface dialog, int which) {
                alarm();//由闹钟服务来自动 启动对话窗口

                finish();//关闭窗口
            }
        });

        builder.setNegativeButton("关闭了", new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {

                finish();
            }
        });

        builder.show();
    }



    private  void alarm(){
        //获取到了系统的闹钟服务
        AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);

        //触发闹钟的时间
        long triggerTime =  System.currentTimeMillis()+5000;


        Intent intent  = new Intent(this,MainActivity2Activity2.class);

        PendingIntent op = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

        //只会执行一次的闹钟
         am.set(AlarmManager.RTC,triggerTime,op); //op : 启动这个闹钟的操作,也就是干什么事.


    }

}

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值