Android AppWidgetManager - top ranked examples from Open Source projects

These code examples were ranked by Codota’s semantic indexing as the best open source examples for Android AppWidgetManager class.

This code example shows how to use the following methods:bindAppWidgetIdIfAllowed, getAppWidgetIds
  } 
 
  @Test 
  public void bindAppWidgetIdIfAllowed_shouldRecordTheBinding() throws Exception { 
    ComponentName provider = new ComponentName("A", "B");
    appWidgetManager.bindAppWidgetIdIfAllowed(789, provider);
    assertArrayEquals(new int[]{789}, appWidgetManager.getAppWidgetIds(provider));
  } 
 
  @Test 
  public void bindAppWidgetId_shouldRecordAppWidgetInfo() throws Exception { 
    ComponentName provider = new ComponentName("abc", "123");
    AppWidgetProviderInfo providerInfo = new AppWidgetProviderInfo();
    providerInfo.provider = provider;
    shadowAppWidgetManager.addInstalledProvider(providerInfo);
 
    appWidgetManager.bindAppWidgetIdIfAllowed(90210, provider);
 
    assertSame(providerInfo, appWidgetManager.getAppWidgetInfo(90210));
  } 
7
CodeRank
This code example shows how to use the following methods:updateAppWidget
  @Override 
  public void onUpdate(Context ctxt, AppWidgetManager mgr,
                        int[] appWidgetIds) {
    ComponentName me=new ComponentName(ctxt, AppWidget.class);
       
    mgr.updateAppWidget(me, buildUpdate(ctxt, appWidgetIds));
  } 
   
  private RemoteViews buildUpdate(Context ctxt, int[] appWidgetIds) {
    RemoteViews updateViews=new RemoteViews(ctxt.getPackageName(),
                                            R.layout.widget); 
   
    Intent i=new Intent(ctxt, AppWidget.class);
     
    i.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
    i.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, appWidgetIds);
     
    PendingIntent pi=PendingIntent.getBroadcast(ctxt, 0 , i,
                                                PendingIntent.FLAG_UPDATE_CURRENT);
     
7
CodeRank
This code example shows how to use the following methods:getInstance
 
    /** 
     *  Checks for any state files that may have not received onDeleted 
     */ 
    void removeOrphanedFiles(Context context) {
        AppWidgetManager wm = AppWidgetManager.getInstance(context);
        int[] ids = wm.getAppWidgetIds(getComponentName(context));
        BookmarkThumbnailWidgetService.removeOrphanedStates(context, ids);
    } 
 
    private void performUpdate(Context context,
            AppWidgetManager appWidgetManager, int[] appWidgetIds) {
        PendingIntent launchBrowser = PendingIntent.getActivity(context, 0,
                new Intent(BrowserActivity.ACTION_SHOW_BROWSER, null, context,
                    BrowserActivity.class), 
                PendingIntent.FLAG_UPDATE_CURRENT);
        for (int appWidgetId : appWidgetIds) {
            Intent updateIntent = new Intent(context, BookmarkThumbnailWidgetService.class);
            updateIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
            updateIntent.setData(Uri.parse(updateIntent.toUri(Intent.URI_INTENT_SCHEME)));
7
CodeRank
This code example shows how to use the following methods:getInstance
 
    @Override 
    public void onAppWidgetOptionsChanged(Context context, AppWidgetManager appWidgetManager,
            int appWidgetId, Bundle newOptions) {
        // scale the fonts of the clock to fit inside the new size 
        float ratio = WidgetUtils.getScaleRatio(context, newOptions, appWidgetId);
        AppWidgetManager widgetManager = AppWidgetManager.getInstance(context);
        updateClock(context, widgetManager, appWidgetId, ratio);
    } 
 
    static ComponentName getComponentName(Context context) {
        return new ComponentName(context, DigitalAppWidgetProvider.class);
    } 
 
    private void updateClock( 
            Context c, AppWidgetManager appWidgetManager, int appWidgetId, float ratio) {
        RemoteViews widget = new RemoteViews(c.getPackageName(), R.layout.digital_appwidget);
        // launch clock when clicking on the time in the widget only if not a lock screen widget 
        Bundle newOptions = appWidgetManager.getAppWidgetOptions(appWidgetId);
        if (newOptions != null &&
6
CodeRank
This code example shows how to use the following methods:getInstance
 
    @Override 
    public void onReceive(Context context, Intent intent) {
        super.onReceive(context, intent);
         
        Settings settings = Settings.getInstance(context);
        boolean xmpp = settings.getBoolean("forward_xmpp", true);
        boolean email = settings.getBoolean("forward_email", true);
        boolean web = settings.getBoolean("forward_web", true);
        if (TOGGLE_EMAIL.equals(intent.getAction())) {
            AppWidgetManager.getInstance(context).updateAppWidget(new ComponentName(context, WidgetProvider.class), getRemoteViews(context, xmpp, !email, web));
            ServiceHelper.updateSettings(context, xmpp, !email, web, null);
        } 
        else if (TOGGLE_XMPP.equals(intent.getAction())) {
            AppWidgetManager.getInstance(context).updateAppWidget(new ComponentName(context, WidgetProvider.class), getRemoteViews(context, !xmpp, email, web));
            ServiceHelper.updateSettings(context, !xmpp, email, web, null);
        } 
        else if (TOGGLE_WEB.equals(intent.getAction())) {
            AppWidgetManager.getInstance(context).updateAppWidget(new ComponentName(context, WidgetProvider.class), getRemoteViews(context, xmpp, email, !web));
            ServiceHelper.updateSettings(context, xmpp, email, !web, null);

7
CodeRank
This code example shows how to use the following methods:getAppWidgetIds
 
		Log.w(LOG, "onUpdate method called");
		// Get all ids 
		ComponentName thisWidget = new ComponentName(context,
				MyWidgetProvider.class); 
		int[] allWidgetIds = appWidgetManager.getAppWidgetIds(thisWidget);
 
		// Build the intent to call the service 
		Intent intent = new Intent(context.getApplicationContext(),
				UpdateWidgetService.class); 
		intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, allWidgetIds);
 
		// Update the widgets via the service 
		context.startService(intent);
	} 
} 
6
CodeRank
This code example shows how to use the following methods:getAppWidgetIds
public class KarmaWidget extends AppWidgetProvider {
	 
	@Override 
	public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
		if (appWidgetIds == null) {
			appWidgetIds = appWidgetManager.getAppWidgetIds(
					new ComponentName(context, KarmaWidget.class));
		} 
 
		UpdateService.requestUpdate(appWidgetIds);
		context.startService(new Intent(context, UpdateService.class));
	} 
	 
	@Override 
	public void onDeleted(Context context, int[] appWidgetIds) {
		final int N = appWidgetIds.length;
		for (int i=0; i<N; i++) {
			KarmaWidgetConfigurationActivity.deleteUsername(context, appWidgetIds[i]);
		} 
	} 
6
CodeRank
This code example shows how to use the following methods:updateAppWidget
	} 
 
	static void updateAppWidget(Context context, AppWidgetManager appWidgetManager,
			int appWidgetId) {
		RemoteViews views = buildAppWidget(context, appWidgetManager, appWidgetId);
		if (views != null) appWidgetManager.updateAppWidget(appWidgetId, views);
	} 
	 
    static void unblurAppWidget(Context context, AppWidgetManager appWidgetManager,
            int appWidgetId) {
        SharedPreferences prefs = context.getSharedPreferences("widget_prefs", 0);  
        Editor e = prefs.edit();
        e.putBoolean("widget_unblurred_"+appWidgetId, true);
        e.commit();
 
        RemoteViews views = buildAppWidget(context, appWidgetManager, appWidgetId);
        if (views != null) {
            views.setViewVisibility(R.id.imgBalanceblur, View.GONE);
            views.setViewVisibility(R.id.txtWidgetAccountnameBlur, View.GONE);
            views.setViewVisibility(R.id.txtWidgetAccountbalance, View.VISIBLE);
6
CodeRank
This code example shows how to use the following methods:updateAppWidget, setOnClickPendingIntent, setTextViewText
	 */ 
	protected abstract ComponentName getComponentName();
	 
	@Override 
	public void onStart(Intent intent, int startId) {
		RemoteViews updateViews = new RemoteViews(this.getPackageName(), getLayoutID());
		updateViews.setTextViewText(getButtonID(), getNetInfoText(this));
 
		Intent settingIntent = new Intent(Settings.ACTION_WIRELESS_SETTINGS);
		PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,
				settingIntent, 0);
		updateViews.setOnClickPendingIntent(getButtonID(), pendingIntent);
		AppWidgetManager.getInstance(this).updateAppWidget(getComponentName(),
				updateViews);
		super.onStart(intent, startId);
	} 
 
	@Override 
	public IBinder onBind(Intent arg0) {
		return null; 
6
CodeRank
This code example shows how to use the following methods:getAppWidgetIds
	public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
 
		final int N = appWidgetIds.length;
		ComponentName thisWidget = new ComponentName(context,
				SimpleWidgetAppWidgetProvider.class); 
		int[] allWidgetIds = appWidgetManager.getAppWidgetIds(thisWidget);
 
		// Build the intent to call the service 
		Intent intent = new Intent(context.getApplicationContext(),
				UpdateWidgetService.class); 
		intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, allWidgetIds);
 
		// Update the widgets via the service 
		context.startService(intent);
	} 
} 
6
CodeRank

Related Android AppWidgetManager Questions & Answers:

Other Android AppWidgetManager Examples

12:	
13:	  @Override
14:	  public void onUpdate(Context context, AppWidgetManager appWidgetManager,
15:	      int[] appWidgetIds) {
16:

Read More

See Code Examples for Android AppWidgetManager Methods: