运行结果如图:
?
?
Step1:xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="当前电池的电量是:"
android:textSize="25dp"/>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
在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();
}
}
|