标签:信息 执行 manifest service enter 连接 问题 initial height
广播是任何应用均可接收的消息。系统将针对系统事件(例如:系统启动或设备开始充电时)传递各种广播。通过将 Intent
传递给 sendBroadcast()
、sendOrderedBroadcast()
或 sendStickyBroadcast()
,您可以将广播传递给其他应用。
Android提供了一套完整的API,允许应用程序自由地发送和接受广播。发送广播使用Intent,接受广播使用 广播接收器(Boradcast Receiver)。
广播分成两种类型:
广播接收器可以自由地对自己感兴趣的广播进行注册,这样当相应的广播发出时,广播接收器就可以收到这条广播,并在内部处理逻辑。
创建广播接收器的步骤:
1.新建一个类使其继承BroadcastReceiver
2.重写onReceive()方法
public class MyBroadCastReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { Toast.makeText(context,"network changes",Toast.LENGTH_LONG).show(); } }3.为接受器绑定要接受的广播
public class MainActivity extends AppCompatActivity { private IntentFilter intentFilter; /* Structured description of Intent values to be matched. An IntentFilter can match against actions, categories, and data (either via its type, scheme, and/or path) in an Intent. It also includes a "priority" value which is used to order multiple matching filters. */ private MyBroadCastReceiver myReceiver; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); intentFilter = new IntentFilter(); intentFilter.addAction("android.net.conn.CONNECTIVITY_CHANGE"); //需要广播器接受什么样的广播,就在这里添加什么样的action myReceiver = new MyBroadCastReceiver(); registerReceiver(myReceiver,intentFilter); //注册 } @Override protected void onDestroy() { unregisterReceiver(myReceiver); //动态注册的广播一定要取消注册才行 super.onDestroy(); }
获取精确的网络状态:
1.修改接收器代码
public class MyBroadCastReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); //这是一个系统服务类,专门用于管理网络连接 NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo(); if(networkInfo!=null&&networkInfo.isAvailable()) { Toast.makeText(context,"获得网络连接",Toast.LENGTH_LONG).show(); } else { Toast.makeText(context,"失去网络连接",Toast.LENGTH_LONG).show(); } } }3.声明程序权限
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.zy.dealhelper"> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/> <application ..... </application> </manifest>
说明:涉及用户隐私的状态信息必须要到AndroidManifest.xml中进行声明
说明:静态注册最大的特点就是程序未启用的情况下便可接受到广播。
使用AS自动生成广播接收器
1.新建一个广播接收器
2.配置广播接收器
public class BootCompleteReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { Toast.makeText(context,"Boot Complete",Toast.LENGTH_LONG).show(); } }3.手动注册
1.首先定义了自己的广播接收器并且配置了广播名
public class MyReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) {
Toast.makeText(context,"收到我自己发送的广播"+intent.getExtras().getString("info"),Toast.LENGTH_SHORT).show();
} }
<receiver android:name=".broadcast.MyReceiver" android:enabled="true" android:exported="true"> <intent-filter> <action android:name="com.expample.zy.MY_BROADCAST"></action> </intent-filter> </receiver>
2.发送广播
Intent broadCastIntent = new Intent("com.example.zy.MY_BROADCAST"); broadCastIntent.putExtra("info","你好啊"); sendBroadcast(broadCastIntent);
0.设置接收器的优先等级
<intent-filter android:priority="100">
<action android:name="com.example.zy.MY_BROADCAST"></action>
</intent-filter>
1.发送有序广播
sendOrderedBroadcast(intent,null);
2.截获
public class MyReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context,"收到我自己发送的广播"+intent.getExtras().getString("info"),Toast.LENGTH_SHORT).show();
abortBroadcast();
}
}
为什么使用本地广播?
前面我们接收和发送的广播全部属于系统全局广播,即发出的广播可以被其他任何应用程序接收到,并且我们也可以接收到来自其他应用程序的广播,这样就容易引起安全性问题。为此Android引入了一套本地广播机制,使用这个机制发出的广播仅仅能在应用程序内部进行传递。
public class MainActivity extends AppCompatActivity { private LocalBroadcastManager localBroadcastManager; private IntentFilter intentFilter; private MyBroadCastReceiver myReceiver; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); localBroadcastManager = LocalBroadcastManager.getInstance(this); //1.获取本地管理器实例 setContentView(R.layout.activity_main); intentFilter = new IntentFilter(); intentFilter.addAction("android.net.conn.CONNECTIVITY_CHANGE"); myReceiver = new MyBroadCastReceiver(); localBroadcastManager.registerReceiver(myReceiver,intentFilter); //2.使用本地广播管理器进行动态注册 }); } @Override protected void onDestroy() { localBroadcastManager.unregisterReceiver(myReceiver); //3.使用本地广播管理器动态卸载 super.onDestroy(); } }
标签:信息 执行 manifest service enter 连接 问题 initial height
原文地址:http://www.cnblogs.com/MrSaver/p/6895396.html