栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

android动态注册广播获取电量信息

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

android动态注册广播获取电量信息

运行结果如图:

 

 

Step1:xml




    

        
    

在MainActivity中通过意图过滤器来注册广播. IntentFilter 是对意图进行过滤的,就是我想要接收哪些广播,不想接收哪些广播,主要过滤Intent中Action, Data和Category的字段。intentFilter.addAction(Intent.ACTION_ BATTERY_ _CHANGED);中Intent的参数ACTION_ _BATTERY_ _CHANGED.通过看源码知这是一一个系统级别广播,作用就是获取当明电池的信息;
 

Step2:MainActivity

package com.example.broadcastapplication1;

import androidx.appcompat.app.AppCompatActivity;

import android.app.Activity;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.widget.TextView;

public class MainActivity extends Activity implements MyReceiver.Mylistener {
    MyReceiver myReceiver;
    private TextView textView;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textView=(TextView)findViewById(R.id.textView);
        IntentFilter intentFilter=new IntentFilter();
        intentFilter.addAction(Intent.ACTION_BATTERY_CHANGED);
        myReceiver=new MyReceiver();
        registerReceiver(myReceiver,intentFilter);
        myReceiver.setMylistener(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(myReceiver);
    }

    @Override
    public void onlistener(String level) {
        textView.setText("当前电池的电量是:"+level+"%");
    }
}

Step3:定义一个MyReceiver继承BroadcastReceiver,重写onReceive()方法,在方法中获取电量的信息。

​
package com.example.broadcastapplication1 ;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Toast;

public class MyReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        int level=intent.getIntExtra("level",0);
        mylistener.onlistener(level+"");
        System.out.println("当前电池的电量是:"+level+"%");
        Bundle bundle=intent.getExtras();
        int current=bundle.getInt("level");
        int total=bundle.getInt("scale");
        if (current*1.0/total<0.5){
            Toast.makeText(context,"电池电量不足60%,请充电",Toast.LENGTH_SHORT).show();
        }else {

            Toast.makeText(context,"电池电量大于60%",Toast.LENGTH_SHORT).show();
        }
//        if (level<60){
//            Toast.makeText(context,"电池电量不足60%,请充电!",Toast.LENGTH_LONG).show();
//        }
    }
    public Mylistener mylistener;
    public interface Mylistener{
        public void onlistener(String level);
    }
   public void setMylistener(Mylistener mylistener) {
       this.mylistener = mylistener;
   }
}

​

判断电量代码如下:

 System.out.println("当前电池的电量是:"+level+"%");
        Bundle bundle=intent.getExtras();
        int current=bundle.getInt("level",0);
        int total=bundle.getInt("scale",0);
        if (current*1.0/total<0.6){
            Toast.makeText(context,"电池电量不足60%,请充电",Toast.LENGTH_SHORT).show();
        }else {

            Toast.makeText(context,"电池电量大于60%",Toast.LENGTH_SHORT).show();
        }
    }

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/631539.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号