Created
October 15, 2018 07:54
-
-
Save LOCOSP/2a8fafd5bd1ce230394d6f659df587a2 to your computer and use it in GitHub Desktop.
how to auto refresh widget in Android Studio
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Implementation of App Widget functionality. | |
*/ | |
public class aiq_priceWidget extends AppWidgetProvider { | |
static String CLICK_ACTION = "Clicked!"; | |
static void updateAppWidget(Context context, AppWidgetManager appWidgetManager, | |
int appWidgetId) { | |
Intent intent = new Intent(context,aiq_priceWidget.class); | |
intent.setAction(CLICK_ACTION); | |
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0); | |
//current time | |
String timeString = | |
DateFormat.getTimeInstance(DateFormat.SHORT).format(new Date()); | |
//Paper Init | |
Paper.init(context); | |
//Reading context | |
String priceBtc = Paper.book().read("BTC"); | |
String priceUsd = Paper.book().read("USD"); | |
String change = Paper.book().read("change"); | |
String timeStamp = Paper.book().read("timeStamp"); | |
// Construct the RemoteViews object | |
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.aiq_price_widget); | |
views.setTextViewText(R.id.widget_tv_price_btc, priceBtc); | |
views.setTextViewText(R.id.widget_tv_price_usd, priceUsd); | |
views.setTextViewText(R.id.widget_tv_change, change); | |
views.setTextViewText(R.id.widget_tv_time_stamp, timeStamp); | |
views.setTextViewText(R.id.widget_tv_now_time, | |
context.getResources().getString( | |
R.string.time, timeString)); | |
views.setOnClickPendingIntent(R.id.wrapper, pendingIntent); | |
// Instruct the widget manager to update the widget | |
appWidgetManager.updateAppWidget(appWidgetId, views); | |
} | |
@Override | |
public void onReceive(Context context, Intent intent) { | |
super.onReceive(context, intent); | |
if(intent.getAction().equals(CLICK_ACTION)) | |
Toast.makeText(context, "Widget Clicked", Toast.LENGTH_SHORT).show(); | |
} | |
@Override | |
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { | |
// There may be multiple widgets active, so update all of them | |
for (int appWidgetId : appWidgetIds) { | |
updateAppWidget(context, appWidgetManager, appWidgetId); | |
Toast.makeText(context, "Widget has been updated! ", Toast.LENGTH_SHORT).show(); | |
} | |
} | |
@Override | |
public void onEnabled(Context context) { | |
// Enter relevant functionality for when the first widget is created | |
} | |
@Override | |
public void onDisabled(Context context) { | |
// Enter relevant functionality for when the last widget is disabled | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment