My app's widget is gone - android

(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

Related

Widget frozen after HomeScreen restart

I've created widget which can be multiple instantiated with different settings stored in SharedPreferences. After solving initial problem with non-updating widgets directly after Configuration Activity (Multiple, configurable widgets - update issue ) everything seems to be working fine. But. When Home Screen application is killed/rebooted (Trebuchet in my case, Android 4.3 & 4.2.2) widget is not refreshed and onClick listeners are gone (onUpdate action is not triggered). I see only non-responsive, empty WigdetView with default, non-updated layout from xml. What is more weird - after phone reboot or after add another instance of my widget - everything works fine again.
I've tried many different ways to solve this strange issue (like single reference to RemoteViews, use Service instead of AsyncTask etc) - but none of this ideas solved the problem..
There are a lot of important logic in code, shortcut:
AppWidgetProvider:
#Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
// ASYNCTASK WAY
new DownloadTask(context, appWidgetManager).execute("http://sampleurl/");
/*
// OR SERVICE WAY
// SERVICE LOGIC IS ALMOST THE SAME AS IN DownloadTask
ComponentName thisWidget = new ComponentName(context, MyWidget.class);
int[] allWidgetIds = appWidgetManager.getAppWidgetIds(thisWidget);
Intent intent = new Intent(context.getApplicationContext(), UpdateWidgetService.class);
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, allWidgetIds);
context.startService(intent);
*/
}
#Override
public void onReceive(Context context, Intent intent){
Bundle extras = intent.getExtras();
int mAppWidgetId=0;;
if (extras != null) {
mAppWidgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID);
}
String action = intent.getAction();
if (action.equals("PreferencesUpdated")) {
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget_lay);
views.setViewVisibility(R.id.menuLayout, View.GONE);
appWidgetManager.updateAppWidget(mAppWidgetId, views);
int[] appWidgetIds = new int[] {mAppWidgetId};
onUpdate(context, appWidgetManager, appWidgetIds);
}
else if (action.equals("ShowMenu")) {
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget_lay);
views.setViewVisibility(R.id.menuLayout, View.VISIBLE);
appWidgetManager.updateAppWidget(mAppWidgetId, views);
}
super.onReceive(context, intent);
}
DownloadTask
public DownloadTask(Context context, AppWidgetManager appWidgetManager) {
this.context = context;
this.appWidgetManager = appWidgetManager;
}
#Override
protected String doInBackground(String... url) {
// 1. DOWNLOAD FILE
// 2. PROCESS FILE, ADD TO DATABASE, ETC
// 3. UPDATE ALL WIDGETS
ComponentName thisWidget = new ComponentName(context, MyWidget.class);
int[] allWidgetIds = appWidgetManager.getAppWidgetIds(thisWidget);
for (int widgetId : allWidgetIds) {
SharedPreferences settings = context.getSharedPreferences(PREFS_NAME, 0);
int myPref= settings.getInt("myPref"+widgetId, -1);
if(myPref!= -1){
Log.d("DownloadTask", "widget id:"+widgetId);
updateWidget(db, widgetId, selectedStation);
}
}
db.close();
return null;
}
#Override
protected void onPreExecute() {
super.onPreExecute();
//SHOW PROGRESSBAR (LOADER) ON ALL WIDGETS
//ADD onClicks
ComponentName thisWidget = new ComponentName(context, MyWidget.class);
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget_lay);
int[] allWidgetIds = appWidgetManager.getAppWidgetIds(thisWidget);
for (int widgetId : allWidgetIds) {
views.setViewVisibility(R.id.progressLayout, View.VISIBLE);
views.setOnClickPendingIntent(R.id.linearLayout1, getMenuPendingIntent(context, widgetId));
views.setOnClickPendingIntent(R.id.refreshButton, getRefreshPendingIntent(context, widgetId));
views.setOnClickPendingIntent(R.id.settingsButton, getSettingsPendingIntent(context, widgetId));
appWidgetManager.updateAppWidget(widgetId, views);
}
}
#Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
//HIDE PROGRESSBAR (LOADER) ON ALL WIDGETS
ComponentName thisWidget = new ComponentName(context, MyWidget.class);
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget_lay);
views.setViewVisibility(R.id.progressLayout, View.INVISIBLE);
appWidgetManager.updateAppWidget(thisWidget, views);
if (exception){
Toast.makeText(context, context.getString(R.string.net_exc), Toast.LENGTH_LONG).show();
}
}
public void updateWidget(MySQLiteHelper db, int widgetId, int myPref){
// UPDATE SINGLE WIDGET
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget_lay);
views.setTextViewText(R.id.someText, "someText");
// ETC
appWidgetManager.updateAppWidget(widgetId, views);
}
I really hope that you guys can help me, cause I'm stuck.
ps. This is expected behaviour that all widgets are refreshed together.
After having had a similar problem, my widget not working after restart of the launcher/homescreen, here a possible solution:
Whenever accessing your RemoteViews, set the .setOnClickPendingIntent(...) again.
So basically speaking, in your onReceive() where you modify your RemoteViews add the corresponding lines to add the PendingIntents, too, and everywhere else where you might modify those.
PS:
You can drop your for loop iterating over ALL widgetIds and just use
void AppWidgetManager.updateAppWidget(ComponentName provider, RemoteViews views)
to update all your views at once.

android digital clock widget is gone

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

Android setTextViewText not working

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);
}
}

Battery Widget in Android

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);

AppWidget alarmmanager not updating

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"/>

Categories

Resources