监控电池电量

时间:2016-01-11 10:57:08

标签: android broadcastreceiver battery batterylevel

我的程序中有一个广播接收器来监控电池电量,如下所示:

    ListView sourcelistview = null;
    ListView targetlistview = null;
    Point startPoint;

    public Window1()
    {
        InitializeComponent();
        lbOne.Items.Add("xxxx");
        lbOne.Items.Add("yyyy");
        lbOne.Items.Add("zzzz");
        lbOne.Items.Add("uuuu"); 
        lbOne.Items.Add("vvvv");
        lbOne.Items.Add("wwww"); 
        lbOne.Items.Add("rrrr");
        lbOne.Items.Add("ssss");
    }

    private void lbOne_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
    {
        startPoint = e.GetPosition(null);
    }

    private void lbTwo_Drop(object sender, DragEventArgs e)
    {

        targetlistview = (ListView)sender;
            var Sourcedata = sourcelistview.SelectedItems.Cast<object>().ToArray();
            foreach (string item in Sourcedata)
            {
                targetlistview.Items.Add(item);
                sourcelistview.Items.Remove(item);
            }            
    }        

    private void lbOne_MouseMove(object sender, MouseEventArgs e)
    {           
        Point mousePos = e.GetPosition(null);
        Vector diff = startPoint - mousePos;

        if (e.LeftButton == MouseButtonState.Pressed && Math.Abs(diff.X) > SystemParameters.MinimumVerticalDragDistance)
        {
            {
                sourcelistview = (ListView)sender;
                DragDrop.DoDragDrop(sourcelistview, sourcelistview.SelectedItems.ToString(), DragDropEffects.Copy);
            }            
        }      
    } 

然后我开始接收器:

private BroadcastReceiver batteryInformation= new BroadcastReceiver(){
    @Override
     public void onReceive(Context arg0, Intent intent) {
         int iLevel = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
         int iScale = intent.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
         int iTemperature = intent.getIntExtra(BatteryManager.EXTRA_TEMPERATURE, -1);
         int iVoltage = intent.getIntExtra(BatteryManager.EXTRA_VOLTAGE, -1);

         Float realLevel = ( (float) iLevel / (float) iScale * 100.f );
     }
}

它工作正常。这是监控水平,比例,温度和其他电池参数的正确方法。

有没有办法只监控水平变化?

0 个答案:

没有答案