studio programacion para móviles libro edición desarrollo desarrollar curso aprende aplicaciones android android-activity widget android-intent launch

programacion - Widget simple de Android que inicia actividad



manual de programacion android pdf (3)

Un par de implementaciones de widgets de aplicaciones que muestran la forma más fácil de hacerlo son visibles en https://github.com/commonsguy/cw-advandroid/tree/master/AppWidget .

Específicamente, https://github.com/commonsguy/cw-advandroid/blob/master/AppWidget/PairOfDice/src/com/commonsware/android/appwidget/dice/AppWidget.java muestra cómo usar un PendingIntent como objetivo onClick para un botón. Puedes hacer que tu PendingIntent comience una Actividad y deberías estar listo para comenzar.

Hola, nunca he trabajado con widgets, pero lo que quiero hacer es crear un widget muy simple; básicamente quiero hacer un widget de 1 por 1 que tenga solo un ícono, solo una imagen configurada como fondo, sin texto, nada solo un ícono pequeño y cuando se presiona el ícono, quiero abrir una actividad. Básicamente quiero hacer un segundo icono como en el cajón de la aplicación en un formulario de widget que abre otra actividad en lugar de la principal. Cualquier ayuda es muy apreciada


Mi proveedor terminó luciendo así después de mucha investigación y jugando

public class WidgetProvider extends AppWidgetProvider { @Override public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { final int N = appWidgetIds.length; for (int i=0; i<N; i++) { int appWidgetId = appWidgetIds[i]; Intent intent = new Intent(context, ClassToLaunchHere.class); PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0); RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget); views.setOnClickPendingIntent(R.id.widget, pendingIntent); appWidgetManager.updateAppWidget(appWidgetId, views); } } }


Declarar una variable

public static String YOUR_AWESOME_ACTION = "YourAwesomeAction";

luego agrega onUpdate y onReceive

@Override public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { ComponentName thisWidget = new ComponentName(context, DigitalClock.class); RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.digital_clock); for (int widgetId : appWidgetManager.getAppWidgetIds(thisWidget)) { remoteViews.setOnClickPendingIntent(R.id.imageView, getPendingSelfIntent(context, YOUR_AWESOME_ACTION)); appWidgetManager.updateAppWidget(thisWidget, remoteViews); } } @Override public void onReceive(Context context, Intent intent) { // TODO Auto-generated method stub super.onReceive(context, intent); if (YOUR_AWESOME_ACTION.equals(intent.getAction())) { AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context); RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.digital_clock); ComponentName watchWidget = new ComponentName(context, DigitalClock.class); appWidgetManager.updateAppWidget(watchWidget, remoteViews); Intent ntent = new Intent(context, MainActivity.class); ntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); context.startActivity(ntent); //Toast.makeText(context, YOUR_AWESOME_ACTION, Toast.LENGTH_SHORT).show(); } } protected PendingIntent getPendingSelfIntent(Context context, String action) { Intent intent = new Intent(context, getClass()); intent.setAction(action); return PendingIntent.getBroadcast(context, 0, intent, 0); }

y agregue actividad en Manifiesto

<activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity>

espero que esto ayude