I am building a static android widget, which means the widget doesn't need to be updated at all. The only thing that widget need to handle is open link from browser when it is triggered.
In the AppWidgetProviderInfo Metadata xml file, there is one attribute called "updatePeriodMillis", and should I set any value there? or just don't add this attribute?
Thanks
Why not just override onUpdate() in your AppWidgetProvider class and have it do nothing.
public class MyWidget extends AppWidgetProvider {
#Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
//do nothing
}
}
in your widgetprovider file write this method:
#Override
public void onReceive(Context context, Intent intent) {
ComponentName thisAppWidget = new ComponentName(context.getPackageName(), getClass().getName());
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
int ids[] = appWidgetManager.getAppWidgetIds(thisAppWidget);
int idd[];
idd = appWidgetManager.getAppWidgetIds(thisAppWidget);
// call updateAppWidget for each widget
for (int appWidgetID : ids) {
updateAppWidget(context, appWidgetManager, appWidgetID);
}
super.onReceive(context, intent);
}
public void updateAppWidget(Context context,AppWidgetManager appWidgetManager, int appWidgetId){
RemoteViews updateViews = new RemoteViews(context.getPackageName(),R.layout.widget_layout1_1);
updateViews.setTextViewText(R.id.txt_days, "" + tmp_diffrentDays);
appWidgetManager.updateAppWidget(appWidgetId, updateViews);
}
try above code it will work for me
Related
Here i have made one widget and added its click listener where i'm starting one service.It works perfectly.But once user force stops application from appinfo my widget's click is not responding.I m not getting why this is happening.Help on this will be appreciated.Thank you
My app widget class code:
MyWidget.java:
public class MyWidget extends AppWidgetProvider {
static Context cont;
static SharedPreferences preferences;
static void updateAppWidget(Context context, AppWidgetManager appWidgetManager,
int appWidgetId) {
cont = context;
Intent intent2 = new Intent();
intent2.setAction("....");
PendingIntent pendingIntent = PendingIntent.
getBroadcast(context, 0,
intent2, PendingIntent.FLAG_UPDATE_CURRENT);
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.my_widget);
views.setOnClickPendingIntent(R.id.appwidget_text, pendingIntent);
appWidgetManager.updateAppWidget(appWidgetId, views);
}
#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);
}
}
#Override
public void onReceive(Context context, Intent intent) {
super.onReceive(context, intent);
context.startService(new Intent(context, MyService.class));
}
#Override
public void onEnabled(Context context) {
.................. }
#Override
public void onDisabled(Context context) {
.................}
Always use an explicit Intent whenever possible. Your code would not work on Android 8.0 and higher, where implicit broadcasts are banned. And an explicit Intent can move your app out of the stopped state.
I am trying to create a simple widget. I wanted that, when touchet, to show a toast. But it does not show anything. I don't know where I'm wrong, it looks quite well(maybe i have forgotten something i didn't notice).
Code:
static String clAc = "CLICKED";
static void updateAppWidget(Context context, AppWidgetManager appWidgetManager,
int appWidgetId) {
Intent intent = new Intent(context, LocationWidget.class);
intent.setAction(clAc);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context,0, intent,0);
Paper.init(context);
String title = Paper.book().read("content");
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.location_widget);
views.setTextViewText(R.id.appwidget_text, title);
views.setOnClickPendingIntent(R.id.layout_wrapper, pendingIntent);
appWidgetManager.updateAppWidget(appWidgetId, views);
}
#Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
for (int appWidgetId : appWidgetIds) {
updateAppWidget(context, appWidgetManager, appWidgetId);
}
}
#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
}
#Override
public void onReceive(Context context, Intent intent) {
super.onReceive(context, intent);
if(intent.getAction().contentEquals(clAc))
Toast.makeText(context, "clicked",Toast.LENGTH_LONG).show();
}
Try to update not each appWidgetId, but all appWidgetIds with your logic.
I need to create Widgets in my android app similar work flow of contact widgets in android. But the problem facing is if there are n widgets of the same application each widgets have different functionality. how to differentiate the button click on each widgets.?
public class MyWidgetActivity extends AppWidgetProvider {
private static final String MyOnClick = "myOnClickTag";
static void updateAppWidget(Context context, AppWidgetManager appWidgetManager,
int appWidgetId) {
CharSequence widgetText = MyWidgetActivityConfigureActivity.loadTitlePref(context, appWidgetId);
// Construct the RemoteViews object
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.simple_widget);
views.setTextViewText(R.id.appwidget_text, widgetText);
views.setOnClickPendingIntent(R.id.actionButton, getPendingSelfIntent(context, MyOnClick,views.getLayoutId()));
// Instruct the widget manager to update the widget
appWidgetManager.updateAppWidget(appWidgetId, views);
}
#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);
// Tell the AppWidgetManager to perform an update on the current app
// widget
}
}
#Override
public void onDeleted(Context context, int[] appWidgetIds) {
// When the user deletes the widget, delete the preference associated with it.
for (int appWidgetId : appWidgetIds) {
MyWidgetActivityConfigureActivity.deleteTitlePref(context, appWidgetId);
}
}
#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
}
#Override
public void onReceive(Context context, Intent intent) {
super.onReceive(context, intent);
if (MyOnClick.equals(intent.getAction()))
{
int appWidgetId = intent.getIntExtra(EXTRA_APPWIDGET_ID,0);
Log.e("KOKOKOKO :: ",""+appWidgetId);
MyWidgetActivityConfigureActivity.loadIdPrefCallQuick(context,appWidgetId);
//your onClick action is here
Toast.makeText(context,"Quick Action Triggered",Toast.LENGTH_SHORT).show();
}
super.onReceive(context, intent);
}
static PendingIntent getPendingSelfIntent(Context context, String action, int appWidgtId)
{
// if we brodcast than definetly we have to define ONRECEIVE
Intent intent = new Intent(context, MyWidgetActivity.class);
intent.putExtra(EXTRA_APPWIDGET_ID,appWidgtId);
intent.setAction(action);
return PendingIntent.getBroadcast(context, 0, intent, 0);
}
}
Instead of this code (static PendingIntent getPendingSelfIntent) :
return PendingIntent.getBroadcast(context, 0, intent, 0);
You should do this :
return PendingIntent.getBroadcast(context, appWidgtId, intent, PendingIntent.FLAG_UPDATE_CURRENT);
This way you should get unique intent and also unique appWidgtId.
It is important to set requestCode (2nd attribute of getBroadcast), so it wouldn't conflict.
For below code...
below code is for the android widget
the same code is on this link Clickhere
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
for (int widgetId : appWidgetIds)
{
updateAppWidget(context, appWidgetManager, widgetId);
}
}
protected static void updateAppWidget(Context context, AppWidgetManager appWidgetManager, int appWidgetId)
{
//Inflate layout.
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.time_weight_layout);
//Update UI.
remoteViews.setTextViewText(R.id.tvTime, Utility.settemp());
remoteViews.setTextViewText(R.id.tvTitle, Utility.gettitle()); //When user click on the label, update ALL the instances of the widget.
Intent labelIntent = get_ACTION_APPWIDGET_UPDATE_Intent(context);
labelIntent.setData(Uri.withAppendedPath(Uri.parse("abc" + "://widget/id/"), String.valueOf(appWidgetId)));
PendingIntent labelPendingIntent = PendingIntent.getBroadcast(context, appWidgetId, labelIntent, PendingIntent.FLAG_UPDATE_CURRENT);
remoteViews.setOnClickPendingIntent(R.id.tvTime, labelPendingIntent);
Log.d("updateAppWidget", "Updated ID: " + appWidgetId);
//Call the Manager to ensure the changes take effect.
appWidgetManager.updateAppWidget(appWidgetId, remoteViews);
}
protected static Intent get_ACTION_APPWIDGET_UPDATE_Intent(Context context)
{
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
ComponentName thisAppWidget = new ComponentName(context.getPackageName(), TimeWidgetProvider.class.getName());
int[] appWidgetIds = appWidgetManager.getAppWidgetIds(thisAppWidget);
Intent intent = new Intent(context, MainActivity.class);
intent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, appWidgetIds);
return intent;
}
here Main activity is configure activity I want to start MainActivity on click of widget
Here's a youtube link for the same.
It's only 6 min tut.
I am trying to manually update my ListView in the appwidget when I make some data changed in the activity, and I can do it successfully when I first time install the app on the phone.
But after I leave the app and re-enter it again, I can't update my remoteviews.
I call the AppWidgetUpdater to help me update the remoteviews:
AppWidgetUpdater appWidgetUpdater = new AppWidgetUpdater(context);
appWidgetUpdater.updateAppWidget();
AppWidgetUpdater:
public void updateAppWidget(){
ComponentName appWidget = new ComponentName(context, SimpleTODOAppWidgetProvider.class);
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
int[] appWidgetIds = appWidgetManager.getAppWidgetIds(appWidget);
Intent intent = new Intent(context, SimpleTODOAppWidgetProvider.class);
intent.setAction("android.appwidget.action.APPWIDGET_UPDATE");
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS,appWidgetIds);
context.sendBroadcast(intent);
}
AppWidgetProvider:
public class SimpleTODOAppWidgetProvider extends AppWidgetProvider {
private Context context;
private AppWidgetManager appWidgetManager;
private int[] appWidgetIds;
#Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
super.onUpdate(context, appWidgetManager, appWidgetIds);
final int N = appWidgetIds.length;
for (int i=0; i<N; i++) {
RemoteViews remoteViews = updateWidgetListView(context,
appWidgetIds[i]);
appWidgetManager.updateAppWidget(appWidgetIds[i], remoteViews);
Log.d("TEST", appWidgetIds.toString() + " from provider");
}
}
#SuppressWarnings("deprecation")
private RemoteViews updateWidgetListView(Context context, int appWidgetId) {
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.appwidget_main);
Intent svcIntent = new Intent(context, ListViewAdaptorService.class);
svcIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
svcIntent.setData(Uri.parse(svcIntent.toUri(Intent.URI_INTENT_SCHEME)));
remoteViews.setRemoteAdapter(appWidgetId, R.id.appwidget_listview,
svcIntent);
remoteViews.setEmptyView(R.id.appwidget_listview, R.id.appwidget_addbutton);
return remoteViews;
}
}
Finally I find what the problem is.
I should use:
appWidgetManager.notifyAppWidgetViewDataChanged(appWidgetIds[i], R.id.appwidget_listview);
instead of:
appWidgetManager.updateAppWidget(appWidgetIds[i], remoteViews);
to update my ListView. I should have done some work to get familiar with ListViewAdaptor before I used Adapter in the AppWidget...