Android Mobile Application Development - Experiment 7 - Duckling Counting (Broadcast)

 Master the use of layout and controls
 Master the orderly broadcast mechanism, receive broadcasts according to the priority order of broadcast receivers
 Master the broadcast interception mechanism

The interface is built through a reasonable layout, and the interface effect is shown in the figure below. Use the orderly broadcast method, and set the priorities of the ducklings below to 1000, 800, and 600 respectively.
1. When the big speaker is clicked, the "Orderly Counting" will pop up, and the ducklings below will report "1", "2", and "3" sequentially from left to right; 2. Set the priority of the second duckling to
1000 , the registration position is adjusted to the front of the first duckling on the left. When the big speaker is clicked, the "orderly counting" will pop up. The duckling in the middle below will first count "1", then the duckling on the left will count "2", and finally the small duck on the right will The duck counts "3";
3. Design an interception mechanism. When the big speaker is clicked, the "orderly count" pops up. The duck in the middle below will count "1" first, and then the rest of the ducks will stop counting.
Encourage the use of ordered broadcast mechanism and broadcast interception mechanism, and independently design experimental cases with similar functions.

 

 


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>

This is the format of the TextView that sets the duck count.

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>

This is the background format called by 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);
    }
}

Guess you like

Origin blog.csdn.net/qq_64628470/article/details/130432922