掌握布局与控件的使用方法
掌握有序广播机制,根据广播接收者的优先级顺序接收广播
掌握广播拦截机制
通过合理布局来搭建界面,界面效果如下图所示。采用有序广播方式,将下方小鸭子优先级分别设置1000、800、600。
1、当点击大喇叭后弹出“有序报数”,下方小鸭子从左到右依次报数“1”、“2”、“3”;
2、将第二个小鸭子优先级设置为1000,注册位置调整到左边第一个小鸭子前边,当点击大喇叭后弹出“有序报数”,下方中间小鸭子首先报数“1”,之后左边小鸭子报数“2”,最后右边小鸭子报数“3”;
3、设计拦截机制,当点击大喇叭后弹出“有序报数”,下方中间小鸭子首先报数“1”,之后其余小鸭子不再报数。
鼓励使用有序广播机制和广播拦截机制,自主设计类似功能的实验案例。
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/count_ducks_bg"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:layout_marginTop="60dp">
<ImageButton
android:id="@+id/horn"
android:layout_width="180dp"
android:layout_height="150dp"
android:layout_centerHorizontal="true"
android:background="@drawable/horn" />
<TextView
android:id="@+id/content_left"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="@+id/horn"
android:background="@drawable/content_left_bg"
android:gravity="center"
android:text="有序报数"
android:textColor="@color/white"
android:visibility="gone" />
</RelativeLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="150dp"
android:orientation="horizontal">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="40dp"
android:layout_marginRight="20dp"
android:orientation="vertical">
<TextView
style="@style/tittle_style"
android:id="@+id/duck_1"
/>
<ImageView
android:layout_width="100dp"
android:layout_height="100dp"
android:background="@drawable/duck" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="20dp"
android:orientation="vertical">
<TextView
style="@style/tittle_style"
android:id="@+id/duck_2"
/>
<ImageView
android:layout_width="100dp"
android:layout_height="100dp"
android:background="@drawable/duck" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="20dp"
android:orientation="vertical">
<TextView
style="@style/tittle_style"
android:id="@+id/duck_3"
/>
<ImageView
android:layout_width="100dp"
android:layout_height="100dp"
android:background="@drawable/duck" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
style.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="tittle_style">
<item name="android:background">@drawable/tittle</item>
<item name="android:layout_width">20dp</item>
<item name="android:layout_height">20dp</item>
<item name="android:visibility">gone</item>
<item name="android:gravity">center</item>
<item name="android:layout_marginLeft">15dp</item>
<item name="android:textColor">@color/white</item>
</style>
</resources>
这是设置鸭子报数的TextView的格式。
tittle.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<!-- 设置渐变颜色-->
<gradient
android:endColor="#fe451d"
android:startColor="#fe957f"
android:type="linear"/>
<!-- 设置圆角-->
<corners android:radius="180dp"/>
</shape>
这是style调用的背景格式。
MainActivity.java
package com.example.shiyan7;
import androidx.appcompat.app.AppCompatActivity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
TextView duck1, duck2, duck3;
ImageButton horn;
TextView content_left;
MyBroadCastReceiver1 myBroadCastReceiver1;
MyBroadCastReceiver2 myBroadCastReceiver2;
MyBroadCastReceiver3 myBroadCastReceiver3;
private int num = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
register_Receiver();
}
private void init() {
duck1 = findViewById(R.id.duck_1);
duck2 = findViewById(R.id.duck_2);
duck3 = findViewById(R.id.duck_3);
horn = findViewById(R.id.horn);
content_left = findViewById(R.id.content_left);
horn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
content_left.setVisibility(View.VISIBLE);
horn.setClickable(false);
Intent intent = new Intent();
intent.setAction("Count_Duck");
sendOrderedBroadcast(intent,null);
}
});
}
private void register_Receiver() {
IntentFilter intentFilter1=new IntentFilter();
intentFilter1.addAction("Count_Duck");
intentFilter1.setPriority(1000);
// intentFilter1.setPriority(800);
myBroadCastReceiver1=new MyBroadCastReceiver1();
registerReceiver(myBroadCastReceiver1,intentFilter1);
IntentFilter intentFilter2=new IntentFilter();
intentFilter2.addAction("Count_Duck");
intentFilter2.setPriority(800);
// intentFilter2.setPriority(1000);
myBroadCastReceiver2=new MyBroadCastReceiver2();
registerReceiver(myBroadCastReceiver2,intentFilter2);
IntentFilter intentFilter3=new IntentFilter();
intentFilter3.addAction("Count_Duck");
intentFilter3.setPriority(600);
myBroadCastReceiver3=new MyBroadCastReceiver3();
registerReceiver(myBroadCastReceiver3,intentFilter3);
}
class MyBroadCastReceiver1 extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
num=num+1;
duck1.setVisibility(View.VISIBLE);
duck1.setText(num+"");
Log.i("duck1","我收到了广播。");
stop();
}
}
class MyBroadCastReceiver2 extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
num=num+1;
duck2.setVisibility(View.VISIBLE);
duck2.setText(num+"");
Log.i("duck2","我收到了广播。");
// abortBroadcast();
// Log.i("duck2","我拦截了广播。");
stop();
}
}
class MyBroadCastReceiver3 extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
num=num+1;
duck3.setVisibility(View.VISIBLE);
duck3.setText(String.valueOf(num));
Log.i("duck3","我收到了广播。");
stop();
}
}
private void stop() {
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
@Override
protected void onDestroy() {
super.onDestroy();
unregisterReceiver(myBroadCastReceiver1);
unregisterReceiver(myBroadCastReceiver2);
unregisterReceiver(myBroadCastReceiver3);
}
}