zoukankan      html  css  js  c++  java
  • BroadcastReceiver

    广播的概念:

    Android中,系统会产生某一个事件时发送广播,应用程序使用广播接受者接收这个广播,就知道系统产生了什么事件。Android系统在运行的过程中,会产生很多事件,比如:开机、电量改变、收发短信、拨打电话、屏幕解锁等。

    IP拨号器:接收拨打电话的广播

    布局文件:

     1 <?xml version="1.0" encoding="utf-8"?>
     2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     3     xmlns:tools="http://schemas.android.com/tools"
     4     android:id="@+id/activity_main"
     5     android:layout_width="match_parent"
     6     android:layout_height="match_parent"
     7     android:paddingBottom="@dimen/activity_vertical_margin"
     8     android:paddingLeft="@dimen/activity_horizontal_margin"
     9     android:paddingRight="@dimen/activity_horizontal_margin"
    10     android:paddingTop="@dimen/activity_vertical_margin"
    11     tools:context="com.ahu.lichang.ipcaller.MainActivity"
    12     android:orientation="vertical"
    13     >
    14 
    15     <EditText
    16         android:id="@+id/et"
    17         android:hint="请输入IP线路"
    18         android:layout_width="match_parent"
    19         android:layout_height="wrap_content" />
    20 
    21     <Button
    22         android:text="保存"
    23         android:onClick="click"
    24         android:layout_width="wrap_content"
    25         android:layout_height="wrap_content" />
    26 </LinearLayout>
    View Code

     MainActivity:

     1 package com.ahu.lichang.ipcaller;
     2 
     3 import android.content.SharedPreferences;
     4 import android.support.v7.app.AppCompatActivity;
     5 import android.os.Bundle;
     6 import android.view.View;
     7 import android.widget.EditText;
     8 
     9 public class MainActivity extends AppCompatActivity {
    10 
    11     @Override
    12     protected void onCreate(Bundle savedInstanceState) {
    13         super.onCreate(savedInstanceState);
    14         setContentView(R.layout.activity_main);
    15     }
    16     public void click(View view){
    17         EditText et = (EditText) findViewById(R.id.et);
    18         SharedPreferences sp = getSharedPreferences("ip",MODE_PRIVATE);
    19         sp.edit().putString("ipNumber",et.getText().toString()).commit();
    20     }
    21 }
    View Code

    CallerReceiver:

     1 package com.ahu.lichang.ipcaller;
     2 
     3 import android.content.BroadcastReceiver;
     4 import android.content.Context;
     5 import android.content.Intent;
     6 import android.content.SharedPreferences;
     7 
     8 /**
     9  * Created by ahu_lichang on 2017/3/22.
    10  */
    11 
    12 public class CallerReceiver extends BroadcastReceiver {
    13 
    14     @Override
    15     public void onReceive(Context context, Intent intent) {
    16         String number = getResultData();
    17         if(number.startsWith("0")){
    18             SharedPreferences sp = context.getSharedPreferences("ip",Context.MODE_PRIVATE);
    19             String ipNumber = sp.getString("ipNumber","");
    20             number = ipNumber + number;//在拨打电话前面加上ipNumber
    21             setResultData(number);
    22             abortBroadcast();//关闭广播
    23         }
    24     }
    25 }
    View Code

    清单文件:

     1 <?xml version="1.0" encoding="utf-8"?>
     2 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
     3     package="com.ahu.lichang.ipcaller">
     4     <!--接收打电话广播需要的权限-->
     5     <uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS"/>
     6     <application
     7         android:allowBackup="true"
     8         android:icon="@mipmap/ic_launcher"
     9         android:label="@string/app_name"
    10         android:supportsRtl="true"
    11         android:theme="@style/AppTheme">
    12         <activity android:name=".MainActivity">
    13             <intent-filter>
    14                 <action android:name="android.intent.action.MAIN" />
    15 
    16                 <category android:name="android.intent.category.LAUNCHER" />
    17             </intent-filter>
    18         </activity>
    19         <!--配置接收广播的类型-->
    20         <receiver android:name=".CallerReceiver">
    21             <intent-filter>
    22                 <action android:name="android.intent.action.NEW_OUTGOING_CALL"/>
    23             </intent-filter>
    24         </receiver>
    25     </application>
    26 
    27 </manifest>
    View Code

     监听应用的安装、卸载、更新

     1 public class APPStatusReceiver extends BroadcastReceiver {
     2 
     3     @Override
     4     public void onReceive(Context context, Intent intent) {
     5         // TODO Auto-generated method stub
     6         String action = intent.getAction();
     7         Uri uri = intent.getData();
     8         if("android.intent.action.PACKAGE_ADDED".equals(action)){
     9             Toast.makeText(context, uri.toString() + "被安装了", 0).show();
    10         }
    11         if("android.intent.action.PACKAGE_REPLACED".equals(action)){
    12             Toast.makeText(context, uri.toString() + "被升级了", 0).show();
    13         }
    14         if("android.intent.action.PACKAGE_REMOVED".equals(action)){
    15             Toast.makeText(context, uri.toString() + "被卸载了", 0).show();
    16         }
    17     }
    18 
    19 }
    View Code
     1         <receiver android:name="com.itheima.applistener.APPStatusReceiver">
     2             
     3             <intent-filter >
     4                 
     5                 <action android:name="android.intent.action.PACKAGE_ADDED"/>
     6                 
     7                 <action android:name="android.intent.action.PACKAGE_REPLACED"/>
     8                 
     9                 <action android:name="android.intent.action.PACKAGE_REMOVED"/>
    10                 
    11                 <data android:scheme="package"/>
    12             
    13             </intent-filter>
    14         
    15         </receiver>
    View Code

     发送自定义广播

    创建广播电台的步骤:

    //创建一个传递消息的意图对象
    Intent intent = new Intent();

    //设置要广播的事件类型
    intent.setAction("com.itheima.broadcast.HMSSDT");

    //设置广播的消息数据
    intent.putExtra("news", "新闻内容");

    //发送一个广播消息
    sendBroadcast(intent);

    广播的两种类型 

     1、无序广播

      所有跟广播的intent匹配的广播接收者都可以收到该广播,并且是没有先后顺序(同时收到)。

     2、有序广播

       所有跟广播的intent匹配的广播接收者都可以收到该广播,但是会按照广播接收者的优先级来决定接收的先后顺序。(有序广播可以被拦截,也可以被修改)

         优先级:-1000~1000

         最终接收者:所有广播接收者都接受到广播之后,他才接收,并且一定会接收(即便拦截也会收到)

         abortBroadCast:阻止其他接收者接收这条广播,类似拦截,只有有序广播可以被拦截

    发送有序广播:

    Intent intent = new Intent();

    intent.setAction("com.itheima.orderedbroadcast.ZYFFNTBT");
    //发送一个有序的广播
    //intent 意图
    //permission 指定接收者需要添加了权限
    //resultReceiver 指定哪个广播接收者最后接到消息
    //scheduler 消息处理器
    //initialCode 给消息指定初始代码
    //initialData 指定消息的数据
    //initialExtras 指定额外的参数
    sendOrderedBroadcast(intent, null, null, null, 1, "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);

    setResultData("400元");//修改

  • 相关阅读:
    bzoj1379 [Baltic2001]Postman
    bzoj1116 [POI2008]CLO
    bzoj1734 [Usaco2005 feb]Aggressive cows 愤怒的牛
    tyvj1086 Elevator
    2014.7.8模拟赛【聪明的打字员】
    2014.7.8模拟赛【笨笨当粉刷匠】|bzoj1296 [SCOI]粉刷匠
    2014.7.8模拟赛【笨笨的电话网络】
    2014.7.8模拟赛【词编码】
    bzoj1854 [Scoi2010]游戏
    2014.7.7 模拟赛【小K的农场】
  • 原文地址:https://www.cnblogs.com/ahu-lichang/p/6607276.html
Copyright © 2011-2022 走看看