I am a novice to android and java development. Currently, I got a small task for informing battery information as widget.
I had searched around and found out a piece of source code for retrieve information from battery (http://www.tutorialforandroid.com/2009/01/getting-battery-information-on-android.html). I tried to adapt it to widget but I had a problem regarding to BroadcastReceiver. I tried to pass the value of level of battery from BroadcastReceiver to RemoteViews. I found method of getResultData() for BroadcastReceiver object but I always get Null value for it.
Therefore, I would like a suggestion to pass this BroadcastReceiver value to RemoteViews.
In order to make it clearer, I add piece of my code here.
public class MyBattery extends TimerTask {
RemoteViews remoteViews;
AppWidgetManager appWidgetmanager;
ComponentName thisWidget;
TextView contentTxt;
Context context;
public MyBattery(Context context, AppWidgetManager appWidgetManager){
this.appWidgetmanager = appWidgetManager;
remoteViews = new RemoteViews(context.getPackageName(),R.layout.main);
thisWidget = new ComponentName(context,widgetTest.class);
this.context = context;
}
private BroadcastReceiver mBatInfoReceiver = new BroadcastReceiver(){
@Override
public void onReceive(Context arg0, Intent intent){
int level = intent.getIntExtra("level", 0);
contentTxt.setText(String.valueOf(level)+"%");
this.setResultData(String.valueOf(level)+"%");
}
};
@Override
public void run() {
remoteViews.setTextViewText(R.id.battery_label,""+ this.mBatInfoReceiver.getResultData());
appWidgetmanager.updateAppWidget(thisWidget, remoteViews);
}
}
check this tutorial as it will help you in building your concept over BraodCastReceiver: