One of the four components: BroadcastReceiver

Standard broadcast

This broadcast will be received without any order. It can't be truncated

Intent intent=new Intent("custom broadcast");

//Send a standard broadcast
sendBroadcast(intent);

Ordered broadcasting

Order can be truncated

Intent intent=new Intent("custom broadcast");

//Send an orderly broadcast
sendOrderedBroadcast(intent,null);

The second parameter is to write null for permission

The broadcast can be intercepted in the onReceiver() method of receiving broadcast

abortBroadcast();

Local broadcasting

The broadcast can only be delivered in this program and the receiver can only receive the broadcast from this application

Dynamic registration charging state changes broadcast

public class MainActivity extends AppCompatActivity {

    private MyReceiver myReceiver;

    private IntentFilter intentFilter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //Create an instance of IntentFilter
        intentFilter = new IntentFilter();
        //Broadcast with change of charging state
        intentFilter.addAction(Intent.ACTION_BATTERY_CHANGED);
        //Registered broadcasting
        myReceiver = new MyReceiver();
        registerReceiver(myReceiver, intentFilter);
    }

    /***
     * Change of charging state execution
     */
    private class MyReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {

            //Charging
            int status = intent.getIntExtra(BatteryManager.EXTRA_STATUS, BatteryManager.BATTERY_STATUS_UNKNOWN);
            //Determine whether it is a state of charge
            if (status == BatteryManager.BATTERY_STATUS_CHARGING) {
                setCharging();
            } else {
                setNoBattery();
            }
        }
    }


    private void setNoBattery() {
        Toast.makeText(MainActivity.this,"Not charging",Toast.LENGTH_SHORT).show();
    }


    private void setCharging() {
        Toast.makeText(MainActivity.this,"There is charging.",Toast.LENGTH_SHORT).show();
    }

    /***
     * Cancellation of broadcasting
     */
    @Override
    protected void onDestroy() {
        if (myReceiver != null) {
            unregisterReceiver(myReceiver);
        }
        super.onDestroy();
    }
}

Static registration broadcast

New – Other – Broadcast Receiver

public class MyReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {

        Toast.makeText(context,"Restart",Toast.LENGTH_SHORT).show();
    }
}

View Android manifest.xml

Here is the static broadcast registration

 <receiver
            android:name=".MyReceiver"
            android:enabled="true"
            android:exported="true">
            <intent-filter>
                <!--System restart broadcast-->
                <action android:name="android.intent.action.BOOT_COMPLETED"/>
            </intent-filter>

        </receiver>

Add permission for restart

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>

Tags: Android xml

Posted on Mon, 06 Jan 2020 00:07:13 -0800 by linda_v