电池电量改变(Intent.ACTION_BATTERY_CHANGED、level,scale)

package com.example.huanglifeng.myapplication01;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.Uri;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;


public class MainActivity extends Activity {

public TextView tvBatteryChanged;
private BroadcastReceiver batteryChangedReceiver=new BroadcastReceiver()
{
@Override
public void onReceive(Context context,Intent intent)
{
if(Intent.ACTION_BATTERY_CHANGED.equals(intent.getAction()))
{
int level= intent.getIntExtra("level",0);
int scale= intent.getIntExtra("scale",100);
tvBatteryChanged.setText("电池用量:"+(level*100/scale)+"%");
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tvBatteryChanged=(TextView)findViewById(R.id.tvBatteryChanged);
registerReceiver(batteryChangedReceiver,new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
}
}

你可能感兴趣的:(intent)