我想创建一个在 textView 中提供电池状态 (%) 的 android 应用程序。我还有一个 ImageView,其中包含我想根据电池状态的值更改的图像。(我有不同的电池图像对应于不同的充电水平)。
我已经可以在 textView 中打印电池状态,但我不知道如何更改 Image View 中的图像。这是我的主要片段:
public class Battery extends Fragment{
private TextView contentTxt;
private ImageView battery;
int level;
int scale;
int test;
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View myFragmentView = inflater.inflate(R.layout.fragment_a, container, false);
contentTxt=(TextView) myFragmentView.findViewById(R.id.monospaceTxt);
getActivity().registerReceiver(this.mBatInfoReceiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
return myFragmentView;
}
private BroadcastReceiver mBatInfoReceiver = new BroadcastReceiver(){
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
int level = intent.getIntExtra("level", 0);
int scale = intent.getIntExtra("scale", 100);
contentTxt.setText(String.valueOf(level * 100 / scale) + "%");
}
};
}
BatteryService 源:
public class BatteryService extends Service {
private int APP_ID = 10000;
private int currentPercent = 0;
private Intent intentFrom;
public static boolean isRunning;
private BroadcastReceiver mBatInfoReceiver = new BroadcastReceiver(){
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
int level = intent.getIntExtra("level", 0);
int scale = intent.getIntExtra("scale", 100);
Log.d("BATTERY_STAT", intent.getClass().getName());
currentPercent = level * 100 / scale;
//Notification notification = new Notification(R.drawable.icon,
//context.getResources().getString(R.string.your_battery_status), System.currentTimeMillis());
// notification.number = currentPercent;
// notification.flags = Notification.FLAG_NO_CLEAR;
// Intent contentIntent = new Intent(context,Main.class);
// notification.setLatestEventInfo(context,
// context.getResources().getString(R.string.battery_status) + " " + String.valueOf(notification.number) + "%",
// context.getResources().getString(R.string.your_battery_status),
// PendingIntent.getActivity(context, 0, contentIntent,Intent.FLAG_ACTIVITY_NEW_TASK));
// mManager.notify(APP_ID , notification);
}
};
private NotificationManager mManager;
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
@Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
Log.d("BATTERY_STAT", "battery service onCreate");
BatteryService.isRunning = true;
mManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
this.registerReceiver(this.mBatInfoReceiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
}
@Override
public void onDestroy() {
// TODO Auto-generated method stub
this.unregisterReceiver(this.mBatInfoReceiver);
BatteryService.isRunning = false;
removeServiceNotification();
super.onDestroy();
}
@Override
public void onStart(Intent intent, int startId) {
// TODO Auto-generated method stub
intentFrom = intent;
super.onStart(intent, startId);
}
private int getPercentBatt(){
return currentPercent;
}
private final IBattery.Stub binder=new IBattery.Stub() {
public int getPercent() {
return(getPercentBatt());
}
public void stopNotification(){
removeServiceNotification();
}
};
public void removeServiceNotification(){
mManager.cancel(APP_ID);
}
}
和我的 xml 文件:
LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="@drawable/fond_correct" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" Battery Status"
android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_marginTop="80px"
android:layout_marginLeft="10px"
android:layout_gravity="center"
android:textStyle="bold"
android:textSize="30sp"
/>
<TextView
android:id="@+id/monospaceTxt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="100%"
android:textSize="50px"
android:typeface="monospace"
android:layout_gravity="center_horizontal"
android:layout_marginTop="35px" />
<ImageView
android:id="@+id/battery"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="center"
android:src="@drawable/battery1"
android:layout_gravity="center_horizontal"/>
</LinearLayout>
在私有 BroadcastReceiver mBatInfoReceive 中,我尝试了一个“if”条件,但它不起作用,请帮我解决这个问题。
谢谢