Java Code Examples for android.appwidget.AppWidgetManager#getAppWidgetIds()

The following examples show how to use android.appwidget.AppWidgetManager#getAppWidgetIds() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: WidgetProvider.java    From DongWeather with Apache License 2.0 6 votes vote down vote up
@Override
public void onReceive(Context context, Intent intent) {
    super.onReceive(context, intent);
    Log.d(TAG, "onReceive: ");
    if (TextUtils.equals(SKIP_COUNTY_WEATHER, intent.getAction())) {
        Bundle extras = intent.getExtras();
        int position = extras.getInt(ListViewService.INITENT_DATA);
        Log.d(TAG, "onReceive: position : " + position);
        Intent skipIntent = new Intent(context, WeatherActivity.class);
        skipIntent.putExtra("skipPosition", position);
        skipIntent.addFlags(FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(skipIntent);
        //AppWidgetManager.getInstance(context).updateAppWidget(mComponentName, mRemoteViews);
    }
    if (AppWidgetManager.ACTION_APPWIDGET_UPDATE.equals(intent.getAction())) {
        AppWidgetManager manager = AppWidgetManager.getInstance(context);
        int[] appWidgetId = manager.getAppWidgetIds(new ComponentName(context, WidgetProvider.class));
        if (appWidgetId.length > 0) {
            manager.notifyAppWidgetViewDataChanged(appWidgetId[0], R.id.widget_listview);
        }
    }
}
 
Example 2
Source File: MyWidgetProvider.java    From codeexamples-android with Eclipse Public License 1.0 6 votes vote down vote up
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
		int[] appWidgetIds) {

	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);
}
 
Example 3
Source File: AppWidgetUtils.java    From OmniList with GNU Affero General Public License v3.0 5 votes vote down vote up
public static void notifyAppWidgets(Context context) {
    // Home widgets
    AppWidgetManager mgr = AppWidgetManager.getInstance(context);
    int[] ids = mgr.getAppWidgetIds(new ComponentName(context, ListWidgetProvider.class));
    LogUtils.d("Notifies AppWidget data changed for widgets " + Arrays.toString(ids));
    mgr.notifyAppWidgetViewDataChanged(ids, R.id.widget_list);
}
 
Example 4
Source File: BookmarkWidgetProvider.java    From 365browser with Apache License 2.0 5 votes vote down vote up
/**
 *  Checks for any states that may have not received onDeleted.
 */
private void removeOrphanedStates(Context context) {
    AppWidgetManager wm = AppWidgetManager.getInstance(context);
    int[] ids = wm.getAppWidgetIds(getComponentName(context));
    for (int id : ids) {
        BookmarkWidgetService.deleteWidgetState(context, id);
    }
}
 
Example 5
Source File: BookmarkWidgetProvider.java    From AndroidChromium with Apache License 2.0 5 votes vote down vote up
/**
 *  Checks for any states that may have not received onDeleted.
 */
private void removeOrphanedStates(Context context) {
    AppWidgetManager wm = AppWidgetManager.getInstance(context);
    int[] ids = wm.getAppWidgetIds(getComponentName(context));
    for (int id : ids) {
        BookmarkWidgetService.deleteWidgetState(context, id);
    }
}
 
Example 6
Source File: LessWidgetProvider.java    From good-weather with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void onReceive(Context context, Intent intent) {
    switch (intent.getAction()) {
        case Constants.ACTION_FORCED_APPWIDGET_UPDATE:
            if(AppPreference.isUpdateLocationEnabled(context)) {
                context.startService(new Intent(context, LocationUpdateService.class));
            } else {
                context.startService(new Intent(context, LessWidgetService.class));
            }
            break;
        case Intent.ACTION_LOCALE_CHANGED:
            context.startService(new Intent(context, LessWidgetService.class));
            break;
        case Constants.ACTION_APPWIDGET_THEME_CHANGED:
            AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
            ComponentName componentName = new ComponentName(context, LessWidgetProvider.class);
            int[] appWidgetIds = appWidgetManager.getAppWidgetIds(componentName);
            onUpdate(context, appWidgetManager, appWidgetIds);
            break;
        case Constants.ACTION_APPWIDGET_UPDATE_PERIOD_CHANGED:
            AppWidgetProviderAlarm widgetProviderAlarm =
                    new AppWidgetProviderAlarm(context, LessWidgetProvider.class);
            if (widgetProviderAlarm.isAlarmOff()) {
                break;
            } else {
                widgetProviderAlarm.setAlarm();
            }
            break;
        default:
            super.onReceive(context, intent);
    }
}
 
Example 7
Source File: MyWidgetProviderSimple.java    From codeexamples-android with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
		int[] appWidgetIds) {

	// Get all ids
	ComponentName thisWidget = new ComponentName(context,
			MyWidgetProviderSimple.class);
	int[] allWidgetIds = appWidgetManager.getAppWidgetIds(thisWidget);

	for (int widgetId : allWidgetIds) {
		// Create some random data
		int number = (new Random().nextInt(100));

		RemoteViews remoteViews = new RemoteViews(context.getPackageName(),
				R.layout.widgetsimple_layout);
		Log.w("WidgetExample", String.valueOf(number));
		// Set the text to the view with the id R.id.update
		// instead of -1
		remoteViews.setTextViewText(R.id.update, String.valueOf(number));

		// Register an onClickListener
		Intent intent = new Intent(context, MyWidgetProviderSimple.class);

		intent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
		intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, allWidgetIds);

		PendingIntent pendingIntent = PendingIntent.getBroadcast(context,
				0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
		remoteViews.setOnClickPendingIntent(R.id.update, pendingIntent);
		appWidgetManager.updateAppWidget(widgetId, remoteViews);
	}
}
 
Example 8
Source File: UnreadNotificationsService.java    From ghwatch with Apache License 2.0 5 votes vote down vote up
protected void updateWidgets() {
  if (PreferencesUtils.getBoolean(context, PreferencesUtils.PREF_WIDGET_UNREAD_EXISTS, false)) {
    AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
    int[] ids = appWidgetManager.getAppWidgetIds(new ComponentName(context, UnreadAppWidgetProvider.class));
    Intent intent = new Intent(context, UnreadAppWidgetProvider.class);
    intent.setAction("android.appwidget.action.APPWIDGET_UPDATE");
    intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, ids);
    context.sendBroadcast(intent);
    Log.d(TAG, "Widget update Intent fired");
  }
}
 
Example 9
Source File: BaseAppWidget.java    From Music-Player with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Check against {@link AppWidgetManager} if there are any instances of this
 * widget.
 */
protected boolean hasInstances(final Context context) {
    final AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
    final int[] mAppWidgetIds = appWidgetManager.getAppWidgetIds(new ComponentName(context,
            getClass()));
    return mAppWidgetIds.length > 0;
}
 
Example 10
Source File: MptWidgetProvider.java    From android with Apache License 2.0 5 votes vote down vote up
protected void updateWithError(Context context, String error) {
    AppWidgetManager mgr = AppWidgetManager.getInstance(context);
    ComponentName cn = new ComponentName(context, getWidgetClass());
    int[] appWidgetIds = mgr.getAppWidgetIds(cn);

    if (appWidgetIds.length == 0) return;

    for (int appWidgetId : appWidgetIds) {
        RemoteViews layout = buildErrorLayout(mgr, context, appWidgetId, error);
        mgr.updateAppWidget(appWidgetId, layout);
    }
}
 
Example 11
Source File: TrackWidgetProvider.java    From mytracks with Apache License 2.0 5 votes vote down vote up
/**
 * Updates all app widgets.
 * 
 * @param context the context
 * @param trackId track id
 */
private static void updateAllAppWidgets(Context context, long trackId) {
  AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
  int[] appWidgetIds = appWidgetManager.getAppWidgetIds(
      new ComponentName(context, TrackWidgetProvider.class));
  for (int appWidgetId : appWidgetIds) {
    updateAppWidget(context, appWidgetManager, appWidgetId, trackId);
  }
}
 
Example 12
Source File: BootReceiver.java    From Orin with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void onReceive(Context context, Intent intent) {
    final AppWidgetManager widgetManager = AppWidgetManager.getInstance(context);

    // Start music service if there are any existing widgets
    if (widgetManager.getAppWidgetIds(new ComponentName(context, AppWidgetBig.class)).length > 0 ||
            widgetManager.getAppWidgetIds(new ComponentName(context, AppWidgetClassic.class)).length > 0 ||
            widgetManager.getAppWidgetIds(new ComponentName(context, AppWidgetSmall.class)).length > 0) {
        final Intent serviceIntent = new Intent(context, MusicService.class);
        context.startService(serviceIntent);
    }
}
 
Example 13
Source File: PirateBoxWidget.java    From AndroidPirateBox with MIT License 5 votes vote down vote up
@Override
public void onReceive(Context context, Intent intent) {
	AppWidgetManager manager = AppWidgetManager.getInstance(context);
       int[] widgetIds = manager.getAppWidgetIds(new ComponentName(context, PirateBoxWidget.class));
  
       if(intent.getAction().equals(WIDGET_INTENT_CLICKED)) {
       	Intent serviceIntent = new Intent(context, PirateBoxService.class);
       	
       	boolean serverRunning = PirateBoxService.isRunning();
       	boolean startingUp = PirateBoxService.isStartingUp();
       	
       	// Only start if not running an not starting up
       	if(!serverRunning && !startingUp) {
       		showIntermediateImage(context);
       		context.startService(serviceIntent);
       	}
       	// Only stop service, if service is running
       	else if(serverRunning) {
       		showIntermediateImage(context);
       		context.stopService(serviceIntent);
       	}
       }
       else {
       	showWidget(context, manager, widgetIds, PirateBoxService.isRunning());
       }
       
       
}
 
Example 14
Source File: BootReceiver.java    From Phonograph with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void onReceive(final Context context, Intent intent) {
    final AppWidgetManager widgetManager = AppWidgetManager.getInstance(context);

    // Start music service if there are any existing widgets
    if (widgetManager.getAppWidgetIds(new ComponentName(context, AppWidgetBig.class)).length > 0 ||
            widgetManager.getAppWidgetIds(new ComponentName(context, AppWidgetClassic.class)).length > 0 ||
            widgetManager.getAppWidgetIds(new ComponentName(context, AppWidgetSmall.class)).length > 0 ||
            widgetManager.getAppWidgetIds(new ComponentName(context, AppWidgetCard.class)).length > 0) {
        final Intent serviceIntent = new Intent(context, MusicService.class);
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) { // not allowed on Oreo
            context.startService(serviceIntent);
        }
    }
}
 
Example 15
Source File: PirateBoxWidget.java    From AndroidPirateBox with MIT License 5 votes vote down vote up
/**
 * Shows the intermediate image
 * 
 * @param context	Context to use
 */
private void showIntermediateImage(Context context) {
	AppWidgetManager manager = AppWidgetManager.getInstance(context);
       int[] widgetIds = manager.getAppWidgetIds(new ComponentName(context, PirateBoxWidget.class));
	
	// Set intermediate image
   	RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget_layout);
       views.setImageViewResource(R.id.widgetCanvas, R.drawable.widget_limbo);
       manager.updateAppWidget(widgetIds, views);
}
 
Example 16
Source File: WidgetHelper.java    From iSCAU-Android with GNU General Public License v3.0 4 votes vote down vote up
private void updateRemoteViews() {
    ComponentName componentName = new ComponentName(app, WidgetProvider.class);
    AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(app);
    int[] appWidgetIds = appWidgetManager.getAppWidgetIds(componentName);
    appWidgetManager.updateAppWidget(appWidgetIds,remoteViews);
}
 
Example 17
Source File: DSubWidgetProvider.java    From Popeens-DSub with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Check against {@link AppWidgetManager} if there are any instances of this widget.
 */
private boolean hasInstances(Context context) {
    AppWidgetManager manager = AppWidgetManager.getInstance(context);
    int[] appWidgetIds = manager.getAppWidgetIds(new ComponentName(context, getClass()));
    return (appWidgetIds.length > 0);
}
 
Example 18
Source File: MediaWidgetProvider.java    From deskcon-android with GNU General Public License v3.0 4 votes vote down vote up
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
		int[] appWidgetIds) {
	
    // Get all ids
    ComponentName thisWidget = new ComponentName(context,
        MediaWidgetProvider.class);
    int[] allWidgetIds = appWidgetManager.getAppWidgetIds(thisWidget);
    SharedPreferences prefs = context.getSharedPreferences("widget_preferences", 0);
    
    for (int widgetId : allWidgetIds) {	
    	// load widget preferences
    	String hostname = prefs.getString("hostname"+widgetId, "Host");
    	Long uuid = prefs.getLong("uuid"+widgetId, 0);
    	String player = prefs.getString("player"+widgetId, "Default");
    	
    	// Build Widget
        RemoteViews remoteViews = new RemoteViews(context.getPackageName(),
	            R.layout.media_widget);
		remoteViews.setTextViewText(R.id.widgethosttextView, hostname);
		remoteViews.setTextViewText(R.id.widgetplayertextView, player);
        // Register an onClickListener
        Intent intent = new Intent(context, MediaWidgetProvider.class);
        intent.setAction("PLAY");
        intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, widgetId);
        intent.putExtra("uuid", uuid);
        intent.putExtra("player", player);

        PendingIntent pendingIntent = PendingIntent.getBroadcast(context,
            0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
        remoteViews.setOnClickPendingIntent(R.id.PlayButton, pendingIntent);
        
        intent.setAction("NEXT");
        pendingIntent = PendingIntent.getBroadcast(context,
                0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
        remoteViews.setOnClickPendingIntent(R.id.NextButton, pendingIntent);
        
        intent.setAction("PREVIOUS");
        pendingIntent = PendingIntent.getBroadcast(context,
                0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
        remoteViews.setOnClickPendingIntent(R.id.PrevButton, pendingIntent);
        
        appWidgetManager.updateAppWidget(widgetId, remoteViews);	    	
    }
    
}
 
Example 19
Source File: TaskListWidgetProvider.java    From opentasks with Apache License 2.0 4 votes vote down vote up
@Override
public void onReceive(Context context, Intent intent)
{
    super.onReceive(context, intent);

    AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
    int[] appWidgetIds = appWidgetManager.getAppWidgetIds(getComponentName(context));
    String action = intent.getAction();
    if (action.equals(Intent.ACTION_PROVIDER_CHANGED))
    {
        appWidgetManager.notifyAppWidgetViewDataChanged(appWidgetIds, R.id.task_list_widget_lv);
    }
    else if (action.equals(ACTION_CREATE_TASK))
    {
        int widgetId = intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, 0);
        WidgetConfigurationDatabaseHelper configHelper = new WidgetConfigurationDatabaseHelper(context);
        SQLiteDatabase db = configHelper.getReadableDatabase();
        ArrayList<Long> widgetLists = WidgetConfigurationDatabaseHelper.loadTaskLists(db, widgetId);
        db.close();
        ArrayList<Long> writableLists = new ArrayList<>();
        String authority = AuthorityUtil.taskAuthority(context);
        if (!widgetLists.isEmpty())
        {
            Cursor cursor = context.getContentResolver().query(
                    TaskLists.getContentUri(authority),
                    new String[] { TaskLists._ID },
                    TaskLists.SYNC_ENABLED + "=1 AND " + TaskLists._ID + " IN (" + TextUtils.join(",", widgetLists) + ")",
                    null,
                    null);
            if (cursor != null)
            {
                while (cursor.moveToNext())
                {
                    writableLists.add(cursor.getLong(0));
                }
                cursor.close();
            }
        }
        Intent editTaskIntent = new Intent(Intent.ACTION_INSERT);
        editTaskIntent.setData(Tasks.getContentUri(authority));
        editTaskIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        if (!writableLists.isEmpty())
        {
            Long preselectList;
            if (writableLists.size() == 1)
            {
                // if there is only one list, then select this one
                preselectList = writableLists.get(0);
            }
            else
            {
                // if there are multiple lists, then select the most recently used
                preselectList = RecentlyUsedLists.getRecentFromList(context, writableLists);
            }
            Log.d(getClass().getSimpleName(), "create task with preselected list " + preselectList);
            ContentSet contentSet = new ContentSet(Tasks.getContentUri(authority));
            contentSet.put(Tasks.LIST_ID, preselectList);
            Bundle extraBundle = new Bundle();
            extraBundle.putParcelable(EditTaskActivity.EXTRA_DATA_CONTENT_SET, contentSet);
            editTaskIntent.putExtra(EditTaskActivity.EXTRA_DATA_BUNDLE, extraBundle);
        }
        context.startActivity(editTaskIntent);
    }
}
 
Example 20
Source File: WidgetHelper.java    From javaide with GNU General Public License v3.0 4 votes vote down vote up
private static int[] findAppWidgetIds(Context context) {
    AppWidgetManager manager = AppWidgetManager.getInstance(context);
    ComponentName widget = new ComponentName(context, RecordingWidgetProvider.class);
    return manager.getAppWidgetIds(widget);

}