I'm using this code to schedule my widget
// Timer
Timer timer = new Timer();
// Schedule time
timer.scheduleAtFixedRate(new MyTime(context, appWidgetManager), 1, 60000);
private class MyTime extends TimerTask {
// RemoveViews
RemoteViews remoteViews;
// AppWidgetManager
AppWidgetManager appWidgetManager;
// ComponentName
ComponentName thisWidget;
public MyTime(Context context, AppWidgetManager appWidgetManager) {
this.appWidgetManager = appWidgetManager;
remoteViews = new RemoteViews(context.getPackageName(),
R.layout.main);
thisWidget = new ComponentName(context, WMTWidget.class);
}
#Override
public void run() {
//code to update textview
}
}
This updates a textview every minute which works fine
I'm wondering if it's possible to change this after the first run ?
it should update the textview the first time after 1 minute and then every 15 minutes
Is this possible?
Try This It may work
// Timer
int time=1; //Defined it global
Timer timer = new Timer();
// Schedule time
timer.scheduleAtFixedRate(new MyTime(context, appWidgetManager), time, 60000);
time=15; //update time for 15 minutes Now it will take 15 minute interval
private class MyTime extends TimerTask {
// RemoveViews
RemoteViews remoteViews;
// AppWidgetManager
AppWidgetManager appWidgetManager;
// ComponentName
ComponentName thisWidget;
public MyTime(Context context, AppWidgetManager appWidgetManager) {
this.appWidgetManager = appWidgetManager;
remoteViews = new RemoteViews(context.getPackageName(),
R.layout.main);
thisWidget = new ComponentName(context, WMTWidget.class);
}
#Override
public void run() {
//code to update textview
}
}
Related
I use this sample:
http://laaptu.wordpress.com/2013/07/19/android-app-widget-with-listview/
Everything is OK but I have two differences:
I use TimerTask and dynamic ArrayList.
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
Timer timer = new Timer();
timer.scheduleAtFixedRate(new TasksView(context, appWidgetManager),100, 5000);
remoteViews = new RemoteViews(context.getPackageName(), R.layout.appwidget);
}
private class TasksView extends TimerTask
{
public TasksView(Context context, AppWidgetManager appWidgetManager){
remoteViews = new RemoteViews(context.getPackageName(), R.layout.appwidget);
thisWidget = new ComponentName(context, Widget.class);
}
#Override
public void run()
{
// SOME operation
}
public void updateListView()
{
// HERE I need updateListView
}
}
With delivery of the list of objects will not be a problem because I'll do it by batches. But how to call update again in AppWidgetProvider -> TasksView extends TimerTask
I don't get the exact point of your question, but if you just want to refresh a listview you can use :
ListView.invalidateViews()
or use method notifyDataSetChanged of your Adapter
Adapter.notifyDataSetChanged()
(please!!Can any one help me )': )
Hi
i am developing a digital clock widget:
Herr is my code:
First on Enabled and on Receive overrides:
public void onEnabled(Context context) {
super.onEnabled(context);
PendingIntent anIntent=PendingIntent.getBroadcast(context, 0, new Intent("upp"),PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmMgr=(AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
alarmMgr.setRepeating(AlarmManager.RTC,System.currentTimeMillis(),1000, anIntent);
}
#Override
public void onReceive(Context context, Intent intent) {
if(intent.getAction().equals("upp")){
ComponentName componentName =new ComponentName(context,getClass().getName());
AppWidgetManager appWidgetManager=AppWidgetManager.getInstance(context);
int[] appWidgetIds=appWidgetManager.getAppWidgetIds(componentName);
onUpdate(context,appWidgetManager,appWidgetIds);
}
}
and on update:
Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
super.onUpdate(context, appWidgetManager, appWidgetIds);
and then inside of onUpdate first i have used the Time method to get the Hour and minute as string :
onEnabled(context);
Time today = new Time(Time.getCurrentTimezone());
today.setToNow();
int hour = today.hour;
String hh = Integer.toString(hour);
int minute = today.minute;
String mm = ":" + Integer.toString(minute);
and then i have showed those two strings inside of two image views with bitmap:
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget_layout);
views.setImageViewBitmap(R.id.imageView1, getFontBitmap(context, hh, Color.WHITE, 70));
views.setImageViewBitmap(R.id.imageView2, getFontBitmap1(context, mm, Color.WHITE, 70));
ComponentName componentName =new ComponentName(context,getClass().getName());
int[] appWidgetId=appWidgetManager.getAppWidgetIds(componentName);
appWidgetManager.updateAppWidget(appWidgetId, views);
}
my problem here is that when i add this widget to home screen there is no layout i mean my clock is gone but when i delete the on Receive override i can see my widget but it doesn't update ?
Thanks in advance
i am developing a digital clock widget for android and here is my code but my problem here is that my widget is gone i mean there is no layout so any help?
First on Enabled:
public void onEnabled(Context context) {
super.onEnabled(context);
PendingIntent anIntent=PendingIntent.getBroadcast(context, 0, new Intent("upp"),PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmMgr=(AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
alarmMgr.setRepeating(AlarmManager.RTC,System.currentTimeMillis(),1000, anIntent);
}
and then Receive override:
#Override
public void onReceive(Context context, Intent intent) {
if(intent.getAction().equals("upp")){
ComponentName componentName =new ComponentName(context,getClass().getName());
AppWidgetManager appWidgetManager=AppWidgetManager.getInstance(context);
int[] appWidgetIds=appWidgetManager.getAppWidgetIds(componentName);
onUpdate(context,appWidgetManager,appWidgetIds);
}
}
and on update:
Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
super.onUpdate(context, appWidgetManager, appWidgetIds);
and then inside of onUpdate first i have used the Time method to get the Hour and minute as string :
onEnabled(context);
Time today = new Time(Time.getCurrentTimezone());
today.setToNow();
int hour = today.hour;
String hh = Integer.toString(hour);
int minute = today.minute;
String mm = ":" + Integer.toString(minute);
and then i have showed those two strings inside of two image views with bitmap:
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget_layout);
views.setImageViewBitmap(R.id.imageView1, getFontBitmap(context, hh, Color.WHITE, 70));
views.setImageViewBitmap(R.id.imageView2, getFontBitmap1(context, mm, Color.WHITE, 70));
ComponentName componentName =new ComponentName(context,getClass().getName());
int[] appWidgetId=appWidgetManager.getAppWidgetIds(componentName);
appWidgetManager.updateAppWidget(appWidgetId, views);
so any help? Thanks in advance
I followed these steps: https://stackoverflow.com/a/18236800/824963 to make my broadcastreceiver work.
Actually all alarmmanagers are working fine, but each time onReceive is called all widget are updated with the same data
public class AlarmManagerBroadcastReceiver extends BroadcastReceiver
{
#Override
public void onReceive(final Context context, Intent intent)
{
final String address = intent.getStringExtra("mykey");
...
final RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget);
// downloading data
...
// decoding data
...
remoteViews.setTextViewText(R.id.mykey, newvalue);
ComponentName thiswidget = new ComponentName(context, AppWidgetProvider.class);
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
appWidgetManager.updateAppWidget(thiswidget, remoteViews);
}
}
Hi all I want to make a battery widget in android with animation. I think I will be able to animate but I want to know that how can I get the battery status again and again? Will it be OK to do it through thread? Or something else is required. Here is the simple code for animation. Please explain how will I get the data from battery again and again all the time.I know the functions but I don't know the mechanism to use them.
public class HelloWidget extends AppWidgetProvider{
RemoteViews remoteViews;
AppWidgetManager appWidgetManager;
ComponentName thisWidget;
ImageView img;
Bitmap icon, icon1;
int counter = 0;
#Override
public void onUpdate(Context context, final AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
this.appWidgetManager = appWidgetManager;
remoteViews = new RemoteViews(context.getPackageName(), R.layout.main);
thisWidget = new ComponentName(context, HelloWidget.class);
remoteViews.setImageViewResource(R.id.imgv, R.drawable.icon2);
appWidgetManager.updateAppWidget(thisWidget, remoteViews);
( new Thread()
{
public void run()
{
while(true)
{
if(counter%15>=0 && counter%15 <=7)
{
remoteViews.setImageViewResource(R.id.imgv, R.drawable.icon2);
}
else
{
remoteViews.setImageViewResource(R.id.imgv, R.drawable.icon);
}
appWidgetManager.updateAppWidget(thisWidget, remoteViews);
counter++;
}
}
}
).start();
}
Register broadcast receiver for battery events and don't query every time.
IntentFilter batteryLevelFilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
mContext.registerReceiver(batteryLevelReceiver, batteryLevelFilter);