运行结果:
当电池电量大于等于20%的时候:
当电池电量小于20%的时候:
实现步骤:
1)修改res/layout.xml 文件夹中的main文件,添加一个按钮控件并设置一些属性。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.victor.a1000phone.day23_homework_batteryreceiver.MainActivity">
<Button
android:id="@+id/btn_battery"
android:layout_centerInParent="true"
android:onClick="onClick"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="查看电池电量" />
</RelativeLayout>
2)编写 BatteryReceiver 类,他继承了 BroadcastReceiver 类。 在该类中重写 onReceive()方法。
import android.app.AlertDialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.widget.Button;
import android.widget.Toast;
/**
* Created by Victor on 2016/9/22 0022.
*/
public class BatteryReceiver extends BroadcastReceiver {
@Override
public void onReceive(final Context context, Intent intent) {
String action = intent.getAction();
if(action.equals(Intent.ACTION_BATTERY_CHANGED))
{
int level = intent.getIntExtra("level",0);
int scale = intent.getIntExtra("scale",100);
if((level*100/scale) < 20 )
{
AlertDialog dialog = new AlertDialog.Builder(context).create();
dialog.setTitle("系统提示");
dialog.setIcon(R.mipmap.ic_launcher);
dialog.setMessage("当前剩余电量低于"+level*100/scale+"%"+",请及时充电!");
//添加确定按钮
dialog.setButton(DialogInterface.BUTTON_POSITIVE, "确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(context,"一定要记得哦!!",Toast.LENGTH_SHORT).show();
}
});
//添加取消按钮
dialog.setButton(DialogInterface.BUTTON_NEGATIVE, "取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(context,"滚到一边!",Toast.LENGTH_SHORT).show();
}
});
//添加中立按钮
dialog.setButton(DialogInterface.BUTTON_NEUTRAL, "忽略", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(context,"稍后提醒!",Toast.LENGTH_SHORT).show();
}
});
dialog.show();//这条语句不能少,否则不显示
//Toast.makeText(context,"当前剩余电量低于"+level*100/scale+"%"+",请及时充电!",Toast.LENGTH_SHORT).show();
}
else
{
AlertDialog dialog2 = new AlertDialog.Builder(context).create();
dialog2.setTitle("系统提示");
dialog2.setIcon(R.mipmap.ic_launcher);
dialog2.setMessage("当前剩余电量低于"+level*100/scale+"%");
//添加确定按钮
dialog2.setButton(DialogInterface.BUTTON_POSITIVE, "确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
dialog2.show();
//Toast.makeText(context,"当前剩余电量:"+level*100/scale+"%",Toast.LENGTH_SHORT).show();
}
}
}
}
3)编写 MainActivity 类,它获取了布局文件中定义的按钮,并为其添加事件监听器。
import android.content.Intent;
import android.content.IntentFilter;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
//为Button按钮添加单击事件
public void onClick(View view)
{
switch(view.getId())
{
case R.id.btn_battery:
registerReceiver(new BatteryReceiver(),new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
break;
default:
break;
}
}
}
运行结果:
- 点击“确定”按钮时:*
- 点击“忽略”按钮时:*
点击”取消“按钮时: