The ninth week of Android development learning summary (2)

Write in front

The current time is 01:29:20, April 17, 2020, which is midnight. After a busy day of studying and handling homework, I finally have time to learn Android development knowledge. Today I am learning the broadcast of one of the four major components of Android. The broadcast content has been substantially modified by Google after Android 8.0, so this piece of content is not much to learn and not much to use.

Displaying power and other content through broadcast monitoring

It ’s still the old way, learn by example. Here we write a small demo to monitor the power. The interface is no longer displayed, mainly look at the writing of Activity:

   private void registerBatteryReceiver() {
        //第二步:我们要收听的频道是:电量变化
        IntentFilter intentFilter = new IntentFilter();
        //第三步:设置频道
        intentFilter.addAction(Intent.ACTION_BATTERY_CHANGED);
        mBatteryLevelReceiver = new BatteryLevelReceiver();
        //这种注册方式是动态注册
        //第四步:注册广播
        this.registerReceiver(mBatteryLevelReceiver,intentFilter);
    }

The above is that the registration of broadcast receivers (dynamic registration) after Android 8.0 is more convenient and easy to use. Static registration is restricted by Google, and many functions are no longer available.
The following is the processing of the received content

          if (intent.ACTION_BATTERY_CHANGED.equals(action)) {
                Log.d(TAG,"收到了电量变化的广播   action is  "+action);
                Log.d(TAG,"当前电量为:"+ intent.getIntExtra(BatteryManager.EXTRA_LEVEL,0));
                int currentLevel = intent.getIntExtra(BatteryManager.EXTRA_LEVEL,0);
                if(mBatteryLevelText!=null){
                    mBatteryLevelText.setText("当前电量为:"+ intent.getIntExtra(BatteryManager.EXTRA_LEVEL,0));
                }
                int maxLevel = intent.getIntExtra(BatteryManager.EXTRA_SCALE, 0);
                //拿到当前电量的最大值后,再除以最大值
                float percent = currentLevel*1.0f/maxLevel;
                Log.d(TAG,"当前电量百分比为:"+percent*100+"%");
            }

As you can see, the operation is still very simple. Similarly, we can add code to implement monitoring application installation, USB plugging, etc .:

        intentFilter.addAction(Intent.ACTION_POWER_CONNECTED);
        intentFilter.addAction(Intent.ACTION_POWER_DISCONNECTED);
        intentFilter.addAction(Intent.ACTION_PACKAGE_ADDED);
        intentFilter.addAction(Intent.ACTION_PACKAGE_REMOVED);
        intentFilter.addDataScheme("package");
else if(Intent.ACTION_POWER_CONNECTED.equals(action)){
                Log.d(TAG,"usb线连接上了...");
                mUsbStatusText.setText("usb连上了");
            }else if(Intent.ACTION_POWER_DISCONNECTED.equals(action)){
                Log.d(TAG,"usb线断开了...");
                mUsbStatusText.setText("usb断开了");
            }else if(Intent.ACTION_PACKAGE_ADDED.equals(action)){
                Log.d(TAG,"应用安装了....");
            }else if(Intent.ACTION_PACKAGE_REMOVED.equals(action)){
                Log.d(TAG,"应用卸载了...");
            }

(The above are written after Android 8.0)

Ordered broadcasting

The frequency of orderly broadcasting is relatively low, so I will only do it here. The so-called ordered broadcast means that the broadcasts sent are in order, and transmitted from high to low. In order to define orderly broadcasting in Android 8.0 and above, dynamic registration is required. The code is no longer posted here, so hurry.

postscript

Due to their weak foundation, it takes more effort than others to catch up. I should learn Android development in winter vacation, but I have used it to consolidate my java web foundation. Caused me to pull down a lot of content. Hard work is inevitable. If you don't struggle now, you will regret it sooner or later. Come on. good night.

Guess you like

Origin www.cnblogs.com/wushenjiang/p/12717141.html