应用广播
发送标准广播的三步骤
发送标准广播:
//发送标准广播
Intent intent =new Intent("com.dongnaoedu.chapter09.standard");
sendBroadcast(intent);
定义广播接受者:
public class StanderdReceiver extends BroadcastReceiver {
public static final String STANDARD_ACTION="com.dongnaoedu.chapter09.standard";
//一旦收到广播就会触发onReceive的方法
@Override
public void onReceive(Context context, Intent intent) {
if(intent!=null &&intent.getAction().equals(STANDARD_ACTION)){
Log.d("ning","收到一个标准广播"); } } }
开关广播接受者:
protected void onStart()
{
super.onStart();
standerdReceiver = new StanderdReceiver();
//创建一个意图过滤器,只处理STANDARD_ACTION广播
IntentFilter filter = new IntentFilter(StanderdReceiver.STANDARD_ACTION);
registerReceiver(standerdReceiver,filter);
}
protected void onStop()
{
super.onStop();
//注销接收器
unregisterReceiver(standerdReceiver);
}
发送有序广播:(可截断广播abortBroadcast())
发送标准广播:
Intent intent=new Intent(ORDER_ACTION);
sendOrderedBroadcast(intent,null);
定义广播接受者:
public void onReceive(Context context, Intent intent) {
if(intent!=null&&intent.getAction().equals(BoradOraderActivity.ORDER_ACTION)){
Log.d("ning","接收器B收到一个有序广播");
}
}
public void onReceive(Context context, Intent intent) {
if(intent!=null&&intent.getAction().equals(BoradOraderActivity.ORDER_ACTION)){
Log.d("ning","接收器A收到一个有序广播");
}
}
开关广播接受者:
protected void OnStart()
{
super.onStart();
//注册广播A
orderAReceiver = new OrderAReceiver();
IntentFilter filterA=new IntentFilter(ORDER_ACTION);
filterA.setPriority(3);
registerReceiver(orderAReceiver,filterA);
//注册广播B
orderBReceiver = new OrderBReceiver();
IntentFilter filterB=new IntentFilter(ORDER_ACTION);
filterB.setPriority(6);
registerReceiver(orderAReceiver,filterB);
}
protected void OnStop()
{
super.onStop();
unregisterReceiver(orderAReceiver);
unregisterReceiver(orderBReceiver);
}
静态注册广播:(不推荐)
在AndroidManifest.xml中注册,为静态注册
权限:<uses-permission android:name="android.permission.VIBRATE"/>
注册部分:<intent-filter>
<action android:name="com.dongnaoedu.chapter09.shock"/>
</intent-filter>
广播定义: public void onReceive(Context context, Intent intent) {
if(intent!=null&&intent.getAction().equals(SHOCK_ACTION))
{
Log.d("ning","震动");
Vibrator vb= (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
vb.vibrate(500);
}
}
广播发送:
public void onClick(View view) {
String fullName="com.dongnaoedu.chapter09.shock";
Intent intent =new Intent("com.dongnaooedu.chapter09.shock");
//指定接收路径
ComponentName componentName=new ComponentName(this,fullName);
//设置组件意图
intent.setComponent(componentName);
sendBroadcast(intent);}
系统广播:
由系统发送的广播,故注册一个广播接受者即可。
系统分钟到达广播:
protected void onStart(){
super.onStart();
//注册一个分钟变更的广播接收器
timeReceiver = new TimeReceiver();
IntentFilter filter= new IntentFilter(Intent.ACTION_TIME_TICK);
registerReceiver(timeReceiver,filter);
}
protected void onStop(){
super.onStop();
//取消注册广播
unregisterReceiver(timeReceiver);
}
系统网络变更广播:
protected void onStart(){
super.onStart();
//注册网络变更的广播接收器
networkReceiver = new NetWorkReceiver();
IntentFilter filter= new IntentFilter("android.net.conn.CONNECTIVITY_CHANGE");
registerReceiver(networkReceiver,filter);
}
protected void onStop(){
super.onStop();
//取消注册广播
unregisterReceiver(networkReceiver);}
屏幕变更事件:
定时管理器:AlarmManager
①定义定时管理器AlarmReceiver
public class AlarmReceiver extends BroadcastReceiver {
public static final String ALARM_ACTION="com.dongnaoedu.chapter09.alarm";
private final Context mContext;
public AlarmReceiver(Context context){
super();
this.mContext=context; }
@Override
public void onReceive(Context context, Intent intent) {
if(intent!=null && intent.getAction().equals(ALARM_ACTION) )
{ Log.d("ning","收到闹钟广播"); } }
//发送闹钟广播的方法
public void sendAlarm()
{ //设置意图给延时意图使用
Intent intent= new Intent(ALARM_ACTION);
//设置用于广播的延迟意图
PendingIntent pendingIntent = PendingIntent.getBroadcast(mContext,0,intent,PendingIntent.FLAG_IMMUTABLE);
//从系统中拿到闹钟管理器
AlarmManager alarmManager=(AlarmManager) mContext.getSystemService(Context.ALARM_SERVICE);
//判断版本,Android6.0
if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.M){
alarmManager.setAndAllowWhileIdle(AlarmManager.RTC_WAKEUP,1000,pendingIntent);
}else
{
//设置闹钟管理器(android6.0,后该方法,在灭屏不保证发送广播)
alarmManager.set(AlarmManager.RTC_WAKEUP,1000,pendingIntent);
}
}
}
②注册定时管理器
public class AlarmActivity extends AppCompatActivity implements View.OnClickListener {
private AlarmReceiver alarmReceiver;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_alarm);
findViewById(R.id.btn_alarm).setOnClickListener(this);
}
@Override
public void onClick(View view) {alarmReceiver.sendAlarm(); }
protected void onStart(){
super.onStart();
alarmReceiver = new AlarmReceiver(getApplicationContext());
IntentFilter filter=new IntentFilter(AlarmReceiver.ALARM_ACTION);
registerReceiver(alarmReceiver,filter);
}
protected void onStop(){
super.onStop();
unregisterReceiver(alarmReceiver);
}
}
竖屏与横屏切换:
切换时会将之前的生命周期结束,重新创建一个新的生命周期。若想不重启,则
①在AndroidManifest.xml中,给activity节点增加Android:configChanges。
②修改活动页面java代码,重写活动的onConfigurationChanged方法。
在AndroidManifest.xml中配置Android:screenOrientation=”portrait”可以指定横屏或竖屏
回到桌面与切换任务列表:
通过Intent.ACTION_CLOSE_SYSTEM_DIALOGS可判断是否回到桌面,任务列表是否打开。
收到意图中reason字段,值为homekey时表示为回桌面,recentapps时为打开任务列表。
在AndroidManifest.xml中加入android:supportsPictureInPicture="true"支持画中画权限
public class ReturnDesktopActivity extends AppCompatActivity {
private DesktopRecevier desktopRecevier;
@Override//在创建函数中注册画中画
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_return_desktop);
desktopRecevier = new DesktopRecevier();
IntentFilter filter=new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
registerReceiver(desktopRecevier,filter);
}
//注销画中画
@Override
protected void onDestroy() {
super.onDestroy();
unregisterReceiver(desktopRecevier);
}
//进入画中画模式或退出画中画时触发
@Override
public void onPictureInPictureModeChanged(boolean isInPictureInPictureMode, Configuration newConfig) {
super.onPictureInPictureModeChanged(isInPictureInPictureMode, newConfig);
if(isInPictureInPictureMode){
Log.d("ning","进入画中画模式");
}else {
Log.d("ning","退出画中画模式");
}
}
//定义一个返回桌面的广播接收器
private class DesktopRecevier extends BoradOraderActivity{
public void onReceive(Context context, Intent intent){
if(intent!=null&&intent.getAction().equals(Intent.ACTION_CLOSE_SYSTEM_DIALOGS)){
String reason=intent.getStringExtra("reason");
if(!TextUtils.isEmpty(reason)&&(reason.equals("homekey")||reason.equals("recentapps"))){
//android 8.0后才有画中画判断一下
if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.O&&!isInPictureInPictureMode())
{ //创建画中画模式的参数构建器
PictureInPictureParams.Builder builder = new PictureInPictureParams.Builder();
Rational ratio = new Rational(10,5); //指定屏幕宽高比例
builder.setAspectRatio(ratio); enterPictureInPictureMode(builder.build()); //进入画中画 } } } } } }