Chinaunix首页 | 论坛 | 博客
  • 博客访问: 302371
  • 博文数量: 93
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 830
  • 用 户 组: 普通用户
  • 注册时间: 2016-02-25 10:44
个人简介

一杯茶,一台电脑

文章分类

全部博文(93)

文章存档

2018年(4)

2017年(57)

2016年(32)

分类: Java

2017-02-03 10:06:20

动态广播的例子:
        这个例子是接收系统网络变更的广播,如果系统的网络改变,那么就会弹出一个Toast的对话框出来下面这个例子是动态广播的例子:


源码这里下载:
关于动态广播的说明:
        Android当中的广播就两种,一种是动态广播就是本文中的示例代码,另外一种就是静态广播了,动态广播的使用一定记得,注册了就要记得释放掉!好借好还

  1. import android.content.BroadcastReceiver;
  2. import android.content.Context;
  3. import android.content.Intent;
  4. import android.content.IntentFilter;
  5. import android.support.v7.app.AppCompatActivity;
  6. import android.os.Bundle;
  7. import android.widget.Toast;

  8. public class MainActivity extends AppCompatActivity {

  9.     private IntentFilter intentFilter;
  10.     private NetworkChanageReceiver networkChanageReceiver;

  11.     @Override
  12.     protected void onCreate(Bundle savedInstanceState) {
  13.         super.onCreate(savedInstanceState);
  14.         setContentView(R.layout.activity_main);
  15.         intentFilter = new IntentFilter();
  16.         intentFilter.addAction("android.net.conn.CONNECTIVITY_CHANGE"); //这个字符串是系统定义的,我们可以查阅帮助文档可以获得更多相关系统广播字符串!!!!!记得要添加相应的权限!!!!
  17.         NetworkChanageReceiver networkChanageReceiver = new NetworkChanageReceiver();
  18.         registerReceiver(networkChanageReceiver,intentFilter);
  19.     }

  20.     @Override
  21.     protected void onDestroy() {
  22.         super.onDestroy();
  23.         unregisterReceiver(networkChanageReceiver);  //在当中活动当中释放掉我们注册的广播
  24.     }

  25.     class NetworkChanageReceiver extends BroadcastReceiver{
  26.         @Override
  27.         public void onReceive(Context context, Intent intent) {
  28.             Toast.makeText(MainActivity.this,"network chanage......",Toast.LENGTH_LONG).show();
  29.         }
  30.     }
  31. }



静态广播的例子:
      静态广播的是直接将接收广播的接收器直接写在Manifest文件当中去,和Activity写法差不多!只是在当中写的是要响应的广播代码,还有一点请注意权限问题,这里有涉及到系统相关的操作所以加上了:<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /> 这个例子是接收系统启动广播的例子,将程序启动后,将模拟器重新启动,就可以看到效果:


源码这里下载:

AndroidManifest.xml 文件:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <manifest xmlns:android=""
  3.     package="barneyx.com.bootcompletereceiver">

  4.     <application
  5.         android:allowBackup="true"
  6.         android:icon="@mipmap/ic_launcher"
  7.         android:label="@string/app_name"
  8.         android:supportsRtl="true"
  9.         android:theme="@style/AppTheme">
  10.         <activity android:name=".MainActivity">
  11.             <intent-filter>
  12.                 <action android:name="android.intent.action.MAIN" />
  13.                 <category android:name="android.intent.category.LAUNCHER" />
  14.             </intent-filter>
  15.         </activity>
  16.         
  17.         <receiver android:name=".BootCompleteReceiver">
  18.             <intent-filter>
  19.                 <action android:name="android.intent.action.BOOT_COMPLETED" />
  20.             </intent-filter>
  21.         </receiver>
  22.     </application>
  23.     <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
  24.     <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
  25. </manifest>

   
  1. BootCompleteReceiver.java文件:

    1. package barneyx.com.bootcompletereceiver;

    2. import android.content.BroadcastReceiver;
    3. import android.content.Context;
    4. import android.content.Intent;
    5. import android.widget.Toast;

    6. /**
    7.  * Created by Administrator on 2017/2/3.
    8.  */

    9. public class BootCompleteReceiver extends BroadcastReceiver {
    10.     @Override
    11.     public void onReceive(Context context, Intent intent) {
    12.         Toast.makeText(context,"Boot Complete ",Toast.LENGTH_LONG).show();
    13.     }
    14. }


自定义广播:
    前面的两个例子都是直接接收系统的广播,然后在我们程序当中做出相应的处理,下面的这个例子是由我们来自定义广播,然后由我们自已来处理,在这个例子当中我们自定义的一个广播字符串MY_BROADCAST,以后所有的接收器只要对这个广播感兴趣就可以直接继承BroadcastReceiver实现OnReceiver就可以处理自己的逻辑了!


源码这里下载:

activity_main.xml 布局文件:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout xmlns:android=""
  3.     xmlns:tools=""
  4.     android:id="@+id/activity_main"
  5.     android:layout_width="match_parent"
  6.     android:layout_height="match_parent"
  7.     tools:context="barneyx.com.mybroadcastreceiver.MainActivity">

  8.     <Button
  9.         android:id="@+id/button"
  10.         android:text="MyBroadcast!!"
  11.         android:layout_width="match_parent"
  12.         android:layout_height="wrap_content" />
  13. </RelativeLayout>
 

MainActivity.java文件:

  1. package barneyx.com.mybroadcastreceiver;

  2. import android.content.Intent;
  3. import android.support.v7.app.AppCompatActivity;
  4. import android.os.Bundle;
  5. import android.view.View;
  6. import android.widget.Button;

  7. public class MainActivity extends AppCompatActivity implements View.OnClickListener {

  8.     @Override
  9.     protected void onCreate(Bundle savedInstanceState) {
  10.         super.onCreate(savedInstanceState);
  11.         setContentView(R.layout.activity_main);
  12.         ((Button)findViewById(R.id.button)).setOnClickListener(this);
  13.     }

  14.     @Override
  15.     public void onClick(View v) {
  16.         switch (v.getId()){
  17.             case R.id.button:
  18.                 Intent intent = new Intent("MY_BROADCAST")
  19.                 sendBroadcast(intent);
  20.                 break;
  21.             default:
  22.                 break;
  23.         }
  24.     }
  25. }

MyBroadcastReceiver.java文件:

  1. package barneyx.com.mybroadcastreceiver;

  2. import android.content.BroadcastReceiver;
  3. import android.content.Context;
  4. import android.content.Intent;
  5. import android.widget.Toast;

  6. /**
  7.  * Created by Administrator on 2017/2/3.
  8.  */

  9. public class MyBroadcastReceiver extends BroadcastReceiver {
  10.     @Override
  11.     public void onReceive(Context context, Intent intent) {
  12.         Toast.makeText(context,"receiver MyBroadcastReceiver...",Toast.LENGTH_LONG).show();
  13.     }
  14. }



        上面使用都是全局的广播,如果我们开发一个涉及到用户隐私的APP那么这么做,是很危险的,为什么呢?如果用上面这种方法写,其他的APP也是可以接收我们APP发送的广播!有木有!!!!所以Android还提供了另一种广播方式叫做:本地广播 看名知意,利用这个技术,我们使用广播只限在我们APP内部进行广播,而不会广播到外面去了!这样,我们程序安全性就有了很大的改观了!
        
        要使用本地广播就得使用Android系统为我们提供的一个本地广播的管理器:LocalBroadcastManager 就是这个类,使用起来也不是很难,相比前面几个只是难了辣么一丢丢!



示例代码:

  1. package barneyx.com.localbroadcasttest;

  2. import android.content.BroadcastReceiver;
  3. import android.content.Context;
  4. import android.content.Intent;
  5. import android.content.IntentFilter;
  6. import android.support.v4.content.LocalBroadcastManager;
  7. import android.support.v7.app.AppCompatActivity;
  8. import android.os.Bundle;
  9. import android.view.View;
  10. import android.widget.Button;
  11. import android.widget.Toast;

  12. public class MainActivity extends AppCompatActivity implements View.OnClickListener {
  13.     private IntentFilter intentFilter;
  14.     private LocalReceiver localReceiver;
  15.     private LocalBroadcastManager localBroadcastManager;


  16.     @Override
  17.     protected void onCreate(Bundle savedInstanceState) {
  18.         super.onCreate(savedInstanceState);
  19.         setContentView(R.layout.activity_main);

  20.         //获取当前实例
  21.         localBroadcastManager = LocalBroadcastManager.getInstance(this);
  22.         ((Button)findViewById(R.id.button)).setOnClickListener(this);

  23.         intentFilter = new IntentFilter();
  24.         intentFilter.addAction("MY_BROADCAST");
  25.         localReceiver = new LocalReceiver();
  26.         localBroadcastManager.registerReceiver(localReceiver,intentFilter);

  27.     }

  28.     @Override
  29.     public void onClick(View v) {
  30.         switch(v.getId()){
  31.             case R.id.button:
  32.                 Intent intent = new Intent("MY_BROADCAST");
  33.                 localBroadcastManager.sendBroadcast(intent);
  34.                 break;
  35.             default:
  36.                 break;
  37.         }
  38.     }

  39.     @Override
  40.     protected void onDestroy() {
  41.         super.onDestroy();
  42.         unregisterReceiver(localReceiver);
  43.     }

  44.     class LocalReceiver extends BroadcastReceiver{
  45.         @Override
  46.         public void onReceive(Context context, Intent intent) {
  47.             Toast.makeText(context, "received local broadcast123123123",Toast.LENGTH_SHORT).show();
  48.         }
  49.     }
  50. }



阅读(1158) | 评论(0) | 转发(0) |
0

上一篇:Java中参数传递

下一篇:Android ViewPager例子

给主人留下些什么吧!~~