码迷,mamicode.com
首页 > 移动开发 > 详细

Android 广播

时间:2016-01-25 17:15:54      阅读:171      评论:0      收藏:0      [点我收藏+]

标签:

广播:

  • 有序广播

    • 发出广播之后同一时刻只有一个广播接收器能够接收到广播,优先级高的先接受,可以截断正在传递的广播,后面的就无法收到广播消息了.
  • 动态创建广播:

    • 创建一个类继承BroadcastReceiver,重写onReceive,有广播来到就会执行onReceive方法,动态注册的一定要取消注册.

      public class MainActivity extends Activity {
      private IntentFilter intentFilter;
      private NetworkChangeReceiver networkChangeReceiver;
      
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
          intentFilter = new IntentFilter();
          intentFilter.addAction("android.net.conn.CONNECTIVITY_CHANGE");// 当系统发生变化时发出的是值为括号内的广播,想要监听什么广播就添加相应的action
          networkChangeReceiver = new NetworkChangeReceiver();
          registerReceiver(networkChangeReceiver, intentFilter);// 注册广播
      }
      
      protected void onDestroy() {
          super.onDestroy();
          // 动态注册的一定要取消注册
          unregisterReceiver(networkChangeReceiver);
      }
      
      class NetworkChangeReceiver extends BroadcastReceiver {
          // 每当网络发生变化就会执行此方法
          public void onReceive(Context arg0, Intent arg1) {
              //系统服务类,用来管理网络连接
              ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
              NetworkInfo networkInfo = connectivityManager
                      .getActiveNetworkInfo();
      
              if (networkInfo != null && networkInfo.isAvailable()) {
                  Toast.makeText(MainActivity.this, "网络可用", 0).show();
              }else{
                  Toast.makeText(MainActivity.this, "网络不可用", 0).show();
              }
      
          }
      
      }
      

      }

  • 静态注册实现开机自启

    • 创建一个类,继承自BroadcastReceiver,收到广播就会执行onReceive方法

          public class BootCompleteReceiver extends BroadcastReceiver {
      
      public void onReceive(Context arg0, Intent arg1) {
          Toast.makeText(arg0, "开机",0).show();
      
      }}
      
    • 清单文件注册:

      <receiver android:name=".BootCompleteReceiver" >
      <intent-filter>
          <action android:name="android.intent.action.BOOT_COMPLETED" />
      </intent-filter>
      

       

  • 发送自定义广播:

    • 首先构建一个intent对象,然后调用Context的sendBroadcast()方法把广播发送出去,这样所有监听这条广播的都会接受到消息

      Intent intent = new Intent("com.example.broadcasttest.MY_BROADCAST");
      sendBroadcast(intent);
      
  • 发送有序广播

    Intent intent = new Intent("com.example.broadcasttest.MY_BROADCAST");
    sendOrderedBroadcast(intent, null);
    
    • 在注册清单的intent-filter中添加android:priority="100"属性来设置优先级,优先级越高越先收到.可以使用abortBroadcast();将广播截断.
  • 本地广播

    • 在应用程序的内部进行传递,并且广播接收器只能接受来自本应用的广播

      public class MainActivity extends Activity {
          private LocalReceiver localReceiver;
          private IntentFilter intentFilter;
          private LocalBroadcastManager localBroadcastManager;
      
          protected void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.activity_main);
              localBroadcastManager = LocalBroadcastManager.getInstance(this);
      
              intentFilter = new IntentFilter();
              intentFilter.addAction("com.example.broadcasttest.LOCAL_BROADCAST");
              localReceiver = new LocalReceiver();
              localBroadcastManager.registerReceiver(localReceiver, intentFilter);
              //注册本地广播监听器
          }
      
      
          public void click(View v){
              Intent intent = new Intent("com.example.broadcasttest.LOCAL_BROADCAST");
              localBroadcastManager.sendBroadcast(intent);//发送本地广播
      
          }
          class LocalReceiver extends BroadcastReceiver {
      
              public void onReceive(Context context, Intent arg1) {
                  Toast.makeText(context, "received local broadcast",
                          Toast.LENGTH_SHORT).show();
              }
      
          }
          protected void onDestroy() {
              super.onDestroy();
              localBroadcastManager.unregisterReceiver(localReceiver);
          }
      
      }
      

- 系统级别的弹窗

    AlertDialog.Builder builder = new AlertDialog.Builder(context);
    builder.setTitle("强制下线");
    builder.setMessage("在他处登录,你被强制下线");
    builder.setCancelable(false);//设置不可取消
    builder.setPositiveButton("OK",new OnClickListener() {

        public void onClick(DialogInterface arg0, int arg1) {
            //TODO
        }
    });
    AlertDialog dialog = builder.create();
    dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
    dialog.show();

Android 广播

标签:

原文地址:http://www.cnblogs.com/muhao/p/5157773.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!