Android Studio 学习 广播
Posted lancelee98
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android Studio 学习 广播相关的知识,希望对你有一定的参考价值。
动态注册监听网络变化
- 创建intentFilter 并addAction 代表了监听哪个广播
- 然后使用registerReceiver()方法 将intentFilter 与 自己创建的监听器 传进去
- 动态注册的 需要unregisterReciver()
- 权限需要在Mainfest中声明 <uses-permission android :name "android.permission.ACCESS_NETWORK.STATE"/>
public class MainActivity extends AppCompatActivity {
private IntentFilter intentFilter;
private NetworkChangedReceiver networkChangedReceiver;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
intentFilter = new IntentFilter();
intentFilter.addAction("android.net.conn.CONNECTIVITY_CHANGE");
networkChangedReceiver = new NetworkChangedReceiver();
registerReceiver(networkChangedReceiver,intentFilter);
}
@Override
protected void onDestroy() {
super.onDestroy();
unregisterReceiver(networkChangedReceiver);
}
class NetworkChangedReceiver extends BroadcastReceiver
{
@Override
public void onReceive (Context context,Intent intent)
{
ConnectivityManager connectivityManager =
(ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
if(networkInfo!=null && networkInfo.isAvailable())
{
Toast.makeText(context,"good",Toast.LENGTH_LONG).show();
}
else
{
Toast.makeText(context,"bad",Toast.LENGTH_LONG).show();
}
}
}
}
静态注册实现开机启动
- new-> other->broadcast receiver 手动创建广播
- exported 允许此广播接收本程序外的广播
之后再Mainfest中声明权限
并说明要监听的广播
发送广播
- Intent intent = new Intent("com....")
- sendBroadcast(intent);
sendOrderedBroadcast(intent,null); 发送有序广播
- <intent- filter android :priority="100">//设置广播优先级
abortBroadcast() 截断广播
本地广播
public class MainActivity extends AppCompatActivity {
private IntentFilter intentFilter;
private LocalReceiver localReceiver;
private LocalBroadcastManager localBroadcastManager;
@Override
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);
//发送本地广播
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent("com.example.broadcasttest.LOCAL_BROADCAST");
localBroadcastManager.sendBroadcast(intent);
}
});
}
@Override
protected void onDestroy() {
super.onDestroy();
//取消动态网络变化广播接收器的注册
localBroadcastManager.unregisterReceiver(localReceiver);
}
class LocalReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "received local broadcast", Toast.LENGTH_SHORT).show();
}
}
}
以上是关于Android Studio 学习 广播的主要内容,如果未能解决你的问题,请参考以下文章
Android课程---Android Studio使用小技巧:提取方法代码片段
错误记录Android Studio 编译报错 ( Could not determine java version from ‘11.0.8‘. | Android Studio 降级 )(代码片段
错误记录Android Studio 编译报错 ( Could not determine java version from ‘11.0.8‘. | Android Studio 降级 )(代码片段
Android 逆向Android 进程注入工具开发 ( Visual Studio 开发 Android NDK 应用 | Visual Studio 中 SDK 和 NDK 安装位置 )(代码片段