Example usage for android.appwidget AppWidgetManager getInstance

List of usage examples for android.appwidget AppWidgetManager getInstance

Introduction

In this page you can find the example usage for android.appwidget AppWidgetManager getInstance.

Prototype

public static AppWidgetManager getInstance(Context context) 

Source Link

Document

Get the AppWidgetManager instance to use for the supplied android.content.Context Context object.

Usage

From source file:org.fairphone.peaceofmind.PeaceOfMindBroadCastReceiver.java

private void updateWidget(Context context) {
    AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
    int[] appWidgetIds = appWidgetManager.getAppWidgetIds(new ComponentName(context, WidgetProvider.class));
    if (appWidgetIds.length > 0) {
        new WidgetProvider().onUpdate(context, appWidgetManager, appWidgetIds);
    }/* www  . j  a  va  2 s . c  o m*/
}

From source file:barqsoft.footballscores.widget.WidgetListProvider.java

@Override
public void onReceive(@NonNull Context context, @NonNull Intent intent) {
    super.onReceive(context, intent);
    if (ScoresSyncAdapter.ACTION_DATA_UPDATED.equals(intent.getAction())) {
        AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
        int[] appWidgetIds = appWidgetManager.getAppWidgetIds(new ComponentName(context, getClass()));
        //This method will trigger WidgetListRemoteViewsService RemoteViewsFactory
        // .onDataChanged() and update the widget UI
        appWidgetManager.notifyAppWidgetViewDataChanged(appWidgetIds, R.id.widget_list);
    }// w w  w .j  a va 2s . c o  m
}

From source file:com.ultrafunk.network_info.receiver.MobileDataStatusReceiver.java

@Override
public void onReceive(Context context, Intent intent) {
    final String action = intent.getAction();

    //   Log.e(this.getClass().getSimpleName(), "onReceive(): " + action);

    updateMobileDataViews = true;/*from  w w  w.j a v a2  s.c o m*/

    telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
    dataState = telephonyManager.getDataState();
    isMobileDataEnabled = MobileDataUtils.isMobileDataEnabled(context);
    isAirplaneModeOn = MobileDataUtils.isAirplaneModeOn(context);
    isMobileOutOfService = NetworkStateService.isMobileOutOfService();
    isDataRoaming = isDataRoaming(context);
    networkOperatorAndServiceProvider = getNetworkOperatorAndServiceProvider(context);
    dataUsageBytes = NetworkStateService
            .setGetDataUsageBytes(TrafficStats.getMobileRxBytes() + TrafficStats.getMobileTxBytes());

    if (Constants.ACTION_DATA_CONNECTION_CHANGED.equals(action)
            || Constants.ACTION_DATA_STATE_CHANGED.equals(action)
            || Constants.ACTION_SERVICE_STATE_CHANGED.equals(action)) {
        // Needed to get around a known bug in Android 5.x: https://code.google.com/p/android/issues/detail?id=78924
        if ((dataState == TelephonyManager.DATA_CONNECTED) && (dataUsageBytes == 0)
                && !NetworkStateService.isWaitingForDataUsage()) {
            NetworkStateService.setWaitingForDataUsage(true);
            Intent serviceIntent = new Intent(context, NetworkStateService.class);
            serviceIntent.setAction(Constants.ACTION_DATA_CONNECTED);
            context.startService(serviceIntent);
        }

        partiallyUpdateWidgets(context);
    } else if (Constants.ACTION_DATA_USAGE_UPDATE.equals(action) || Intent.ACTION_SCREEN_ON.equals(action)) {
        partiallyUpdateWidgets(context);
    } else if (Constants.ACTION_UPDATE_WIDGET.equals(action)) {
        partiallyUpdateWidget(context, AppWidgetManager.getInstance(context),
                intent.getIntExtra(Constants.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID));
    }
}

From source file:com.johan.vertretungsplan.background.VertretungsplanService.java

@Override
protected void onHandleIntent(Intent intent) {
    context = this;
    settings = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
    extras = intent.getExtras();/*from  ww w  .j  a  va 2 s  . c o m*/
    Gson gson = new Gson();

    boolean autoSync;
    try {
        autoSync = extras.getBoolean("AutoSync");
    } catch (NullPointerException e) {
        autoSync = false;
    }

    ConnectivityManager conMan = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);

    //wifi
    State wifi = conMan.getNetworkInfo(1).getState();

    if (wifi == NetworkInfo.State.CONNECTED || autoSync == false
            || settings.getBoolean("syncWifi", false) == false) {

        Log.d("Vertretungsplan", "WiFi state: " + wifi);
        Log.d("Vertretungsplan", "autoSync: " + autoSync);
        Log.d("Vertretungsplan", "syncWifi: " + Boolean.valueOf(settings.getBoolean("syncWifi", false)));

        Log.d("Vertretungsplan", "Vertretungsplan wird abgerufen");

        try {
            BaseParser parser = ((VertretungsplanApplication) getApplication()).getParser();

            if (parser == null)
                return;

            Vertretungsplan v = parser.getVertretungsplan();
            settings.edit().putString("Vertretungsplan", gson.toJson(v)).commit();

            AppWidgetManager mgr = AppWidgetManager.getInstance(this);
            int[] ids = mgr.getAppWidgetIds(new ComponentName(this, VertretungsplanWidgetProvider.class));
            new VertretungsplanWidgetProvider().onUpdate(this, mgr, ids);
        } catch (IOException | JSONException e) {
            e.printStackTrace();
        } catch (VersionException e) {
        } catch (UnauthorizedException e) {
        }
    }
}

From source file:de.Maxr1998.xposed.maxlock.ui.MasterSwitchShortcutActivity.java

public void fireIntentAndFinish() {
    Intent intent = new Intent(this, MasterSwitchWidget.class);
    intent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
    int[] ids = AppWidgetManager.getInstance(getApplication())
            .getAppWidgetIds(new ComponentName(getApplication(), MasterSwitchWidget.class));
    intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, ids);
    sendBroadcast(intent);/*from ww  w .  j  a v  a 2 s  . c o m*/
    finish();
}

From source file:net.sourceforge.servestream.service.AppWidgetOneProvider.java

/**
 * Check against {@link AppWidgetManager} if there are any instances of this widget.
 *///from w w w. j a  v a  2  s  . c  o  m
private boolean hasInstances(Context context) {
    AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
    int[] appWidgetIds = appWidgetManager.getAppWidgetIds(new ComponentName(context, this.getClass()));
    return (appWidgetIds.length > 0);
}

From source file:org.solovyev.android.calculator.widget.CalculatorWidget.java

public void updateWidget(@Nonnull Context context, boolean partially) {
    final AppWidgetManager manager = AppWidgetManager.getInstance(context);
    final int[] widgetIds = manager.getAppWidgetIds(new ComponentName(context, CalculatorWidget.class));
    updateWidget(context, manager, widgetIds, partially);
}

From source file:com.android.mms.widget.MmsWidgetProvider.java

/**
 * Update the widget appWidgetId//from ww w . j  a va2 s.  com
 */
private static void updateWidget(Context context, int appWidgetId) {
    if (Log.isLoggable(LogTag.WIDGET, Log.VERBOSE)) {
        Log.v(TAG, "updateWidget appWidgetId: " + appWidgetId);
    }
    RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget);
    PendingIntent clickIntent;

    // Launch an intent to avoid ANRs
    final Intent intent = new Intent(context, MmsWidgetService.class);
    intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
    intent.setData(Uri.parse(intent.toUri(Intent.URI_INTENT_SCHEME)));
    remoteViews.setRemoteAdapter(appWidgetId, R.id.conversation_list, intent);

    remoteViews.setTextViewText(R.id.widget_label, context.getString(R.string.sms_app_label));

    // Open Mms's app conversation list when click on header
    final Intent convIntent = new Intent(context, ConversationList.class);
    clickIntent = PendingIntent.getActivity(context, 0, convIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    remoteViews.setOnClickPendingIntent(R.id.widget_header, clickIntent);

    // On click intent for Compose
    final Intent composeIntent = new Intent(context, ComposeMessageActivity.class);
    composeIntent.setAction(Intent.ACTION_SENDTO);
    clickIntent = PendingIntent.getActivity(context, 0, composeIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    remoteViews.setOnClickPendingIntent(R.id.widget_compose, clickIntent);

    // On click intent for Conversation
    TaskStackBuilder taskStackBuilder = TaskStackBuilder.create(context);
    taskStackBuilder.addParentStack(ComposeMessageActivity.class);
    Intent msgIntent = new Intent(Intent.ACTION_VIEW);
    msgIntent.setType("vnd.android-dir/mms-sms");
    taskStackBuilder.addNextIntent(msgIntent);
    remoteViews.setPendingIntentTemplate(R.id.conversation_list,
            taskStackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT));

    AppWidgetManager.getInstance(context).updateAppWidget(appWidgetId, remoteViews);
}

From source file:com.shanet.relayremote.Background.java

protected void onPostExecute(ArrayList<BasicNameValuePair> states) {
    // Dismiss the dialog and cancel the timer
    if (!isWidget) {
        dialog.dismiss();/*from  w  w  w. j av  a2s  .  com*/
        tt.cancel();
    }

    // If the context is an instance of the main activity, update the state of the relays in the listview
    if (!isWidget && (Activity) context instanceof Main && states.size() > 1) {
        ((Main) context).setRelaysAndGroupsStates(states);
        // If a widget, update the indicator light and states map 
    } else if (isWidget) {
        for (int i = 1; i < states.size(); i++) {
            if (pin == Integer.valueOf(states.get(i).getName())) {
                // Update the indicator image
                RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget);
                views.setImageViewResource(R.id.widgetIndicator,
                        (states.get(i).getValue().charAt(0) == Constants.CMD_ON) ? R.drawable.widget_on
                                : R.drawable.widget_off);
                AppWidgetManager.getInstance(context).updateAppWidget(appWidgetId, views);

                // Set the state of the widget in the widget class
                Widget.setState(appWidgetId,
                        (states.get(i).getValue().charAt(0) == Constants.CMD_ON) ? Widget.STATE_ON
                                : Widget.STATE_OFF);
            }
        }
    }
}

From source file:dk.cafeanalog.AnalogWidget.java

private void handleIsOpen(final Context mContext, final OpeningStatus openingStatus) {
    final AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(mContext);
    final int[] appWidgetIds = appWidgetManager
            .getAppWidgetIds(new ComponentName(mContext, AnalogWidget.class));
    final RemoteViews views = new RemoteViews(mContext.getPackageName(), R.layout.analog_widget);
    views.setTextViewText(R.id.appwidget_text, mContext.getText(R.string.refreshing_analog));
    views.setTextColor(R.id.appwidget_text,
            ContextCompat.getColor(mContext, android.R.color.primary_text_dark));
    // Instruct the widget manager to update the widget
    for (int appWidgetId : appWidgetIds) {
        appWidgetManager.updateAppWidget(appWidgetId, views);
    }/*from   ww w. jav a  2s. c  o m*/

    CharSequence widgetText;
    if (openingStatus.open) {
        widgetText = mContext.getString(R.string.widget_open_analog);
        views.setTextColor(R.id.appwidget_text, ContextCompat.getColor(mContext, R.color.openColor));
    } else {
        widgetText = mContext.getString(R.string.widget_closed_analog);
        views.setTextColor(R.id.appwidget_text, ContextCompat.getColor(mContext, R.color.closedColor));
    }

    views.setTextViewText(R.id.appwidget_text, widgetText);
    views.setOnClickPendingIntent(R.id.appwidget_text, getPendingSelfIntent(mContext));
    // Instruct the widget manager to update the widget
    for (int appWidgetId : appWidgetIds) {
        appWidgetManager.updateAppWidget(appWidgetId, views);
    }
}