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
Related
My widget wont update if I change the size of it.
What am I doing wrong here? It seems like onAppWidgetOptionsChanged is not working properly.
It wont update if I update the width and height in the widget provider. I get the same width and height every time.
Here is my code for the class:
public class wimWidget extends AppWidgetProvider {
#Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
Toast.makeText(context, "onUpdate ", Toast.LENGTH_LONG).show();
RemoteViews remoteViews = new RemoteViews(context.getPackageName(),
R.layout.widget);
Intent intent = new Intent(context.getApplicationContext(),
UpdateWidgetService.class);
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, appWidgetIds);
Log.v("wim", "onUpdate-" + intent);
// To react to a click we have to use a pending intent as the
// onClickListener is
// excecuted by the homescreen application
PendingIntent pendingIntent = PendingIntent.getService(
context.getApplicationContext(), 0, intent,
PendingIntent.FLAG_UPDATE_CURRENT);
//remoteViews.setOnClickPendingIntent(R.id.imagewidget, pendingIntent);
remoteViews.setOnClickPendingIntent(R.id.widget116, pendingIntent);
// Finally update all widgets with the information about the click
// listener
ComponentName watchWidget;
watchWidget = new ComponentName( context, wimWidget.class );
// appWidgetManager.updateAppWidget(appWidgetIds, remoteViews);
appWidgetManager.updateAppWidget(watchWidget, remoteViews);
final int N = appWidgetIds.length;
for (int i=0; i<N; i++) {
int appWidgetId = appWidgetIds[i];
Bundle options = appWidgetManager.getAppWidgetOptions(appWidgetId);
if(options!=null){
//int nwidth = options.getInt(AppWidgetManager.OPTION_APPWIDGET_MIN_WIDTH);
onAppWidgetOptionsChanged(context, appWidgetManager, appWidgetId, options);
}
}
// Update the widgets via the service
context.startService(intent);
}
#Override
public void onAppWidgetOptionsChanged (Context context, AppWidgetManager
appWidgetManager, int appWidgetId, Bundle newOptions) {
super.onAppWidgetOptionsChanged(context, appWidgetManager, appWidgetId, newOptions);
RemoteViews remoteViews = new RemoteViews(context.getPackageName(),
R.layout.widget);
int minWidth = newOptions.getInt(AppWidgetManager.OPTION_APPWIDGET_MIN_WIDTH);
Toast.makeText(context, "Oppdatert - onAppWidgetOptionsChanged - minWidth: " + minWidth, Toast.LENGTH_LONG).show();
appWidgetManager.updateAppWidget(appWidgetId, remoteViews);
}
private void updateWidget(Context context, AppWidgetManager appWidgetManager,
int appWidgetId, Bundle options) {
Toast.makeText(context, "updateWidget", Toast.LENGTH_SHORT).show();
// Do whatever you need to do to the widget. Include any customization based
// on the size, included in the options. Be sure to have a graceful fallback if
// no valid size data is included in the bundle.
}
#Override
public void onDeleted(Context context, int[] appWidgetIds) {
//Toast.makeText(context, "onDeleted", Toast.LENGTH_SHORT).show();
Intent serviceIntent = new Intent(context.getApplicationContext(), UpdateWidgetService.class);
context.stopService(serviceIntent);
super.onDeleted(context, appWidgetIds);
}
}
and here is the provider:
<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider
xmlns:android="http://schemas.android.com/apk/res/android"
android:minWidth="50.0dp"
android:minHeight="110.0dp"
android:updatePeriodMillis="111600"
android:initialLayout="#layout/widget"
/>
(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
OK so I have an android widget and I've been having trouble with the android widget updating. I've used some simple hard coded setTextViewText but nothing happen when I debug the app..
My code:
public class Widget extends AppWidgetProvider {
RemoteViews views;
#Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
int N = appWidgetIds.length;
views = new RemoteViews(context.getPackageName(), R.layout.widget);
for (int i = 0; i < N; i++) {
int appWidgetId = appWidgetIds[i];
String alarm = Settings.System.getString(context.getContentResolver(),
Settings.System.NEXT_ALARM_FORMATTED);
views.setTextViewText(R.id.tvAlarm, alarm);
views.setTextViewText(R.id.tvNextAlarm, "qwertyuiop");
appWidgetManager.updateAppWidget(appWidgetId, views);
}
}
I know some of the code is wrong, such as the alarm and I am trying to incorporate a battery status somehow but I've been told that a BroadcastReceiver isn't the way forward so at the moment I've commented it out.
But the main problem is the updating the widget to change a TextView isn't working...
I've just tried this code in one of my widgets and it works fine:
#Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
// Get ids of all the instances of the widget
ComponentName widget = new ComponentName(context, MediaWidgerProvider.class);
int[] widgetIds = appWidgetManager.getAppWidgetIds(widget);
for (int widgetId : widgetIds) {
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.my_widget);
remoteViews.setTextViewText(R.id.widget_text, "Hello");
appWidgetManager.updateAppWidget(widgetId, 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);
So, I've got a widget, I want it to update every 60 seconds. When the widget is first added to the homescreen, it runs its update function just fine. Beyond that it's supposed to start an AlarmManager, which will rerun the update method every 60 seconds. That's the part that it doesn't seem to be actually doing. Here's my code:
public class ClockWidget extends AppWidgetProvider {
public static String CLOCK_WIDGET_UPDATE = "com.nickavv.cleanwidget.CLEANCLOCK_UPDATE";
#Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int appWidgetIds[]) {
final int N = appWidgetIds.length;
for (int i = 0; i < N; i++) {
int appWidgetId = appWidgetIds[i];
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.clocklayout);
appWidgetManager.updateAppWidget(appWidgetId, views);
updateAppWidget(context, appWidgetManager, appWidgetId);
}
}
public static void updateAppWidget(Context context, AppWidgetManager appWidgetManager, int appWidgetId) {
Log.d("log","Entered update cycle");
//Unimportant for these purposes
appWidgetManager.updateAppWidget(appWidgetId, views);
}
private PendingIntent createClockTickIntent(Context context) {
Intent intent = new Intent(CLOCK_WIDGET_UPDATE);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0,
intent, PendingIntent.FLAG_UPDATE_CURRENT);
return pendingIntent;
}
#Override
public void onEnabled(Context context) {
super.onEnabled(context);
Log.d("onEnabled","Widget Provider enabled. Starting timer to update widget every minute");
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC, System.currentTimeMillis(), 60000, createClockTickIntent(context));
}
#Override
public void onDisabled(Context context) {
super.onDisabled(context);
Log.d("onDisabled", "Widget Provider disabled. Turning off timer");
AlarmManager alarmManager = (AlarmManager) context
.getSystemService(Context.ALARM_SERVICE);
alarmManager.cancel(createClockTickIntent(context));
}
#Override
public void onReceive(Context context, Intent intent) {
super.onReceive(context, intent);
Log.d("onReceive", "Received intent " + intent);
if (CLOCK_WIDGET_UPDATE.equals(intent.getAction())) {
Log.d("onReceive", "Clock update");
// Get the widget manager and ids for this widget provider, then
// call the shared
// clock update method.
ComponentName thisAppWidget = new ComponentName(context.getPackageName(), getClass().getName());
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
int ids[] = appWidgetManager.getAppWidgetIds(thisAppWidget);
for (int appWidgetID: ids) {
updateAppWidget(context, appWidgetManager, appWidgetID);
}
}
}
}
It's the product of a few tutorials I've found on the matter, and my own knowledge of Android. According to my logcats, it never gets to the Log.d("onReceive", "Clock update"); line. And yes, my Manifest is set up with the clock update intent. Thanks!
EDIT: Additional info. I put a log line in the createClockTickIntent method, and it fires off. So I guess this means that my application is running the alarmManager.setRepeating line, no idea why is isn't actually repeating.
Arggghhh, it was a simple typo. The intent filter was "com.nickavv.cleanwidgets.CLEANCLOCK_UPDATE", and I had written "com.nickavv.cleanwidget.CLEANCLOCK_UPDATE"
What a pain, but hey, now I know.
So, moral of the story for anybody with a similar problem to me: Check your spelling! Check it twice, or ten times. On everything!
You have to make an appwidget-provider and set updatePeriodMillis on 0.6 second , then it will be update per 60 seconds.
<?xml version="1.0" encoding="utf-8" ?>
<appwidget-provider
xmlns:android="http://schemas.android.com/apk/res/android"
android:minWidth="146dp"
android:initialLayout="#layout/YourLayout"
android:updatePeriodMillis="0.6"
android:minHeight="144dp"/>