Pergunta

I want to create my widget on the home page after I hit a button in my app. The class of my widget is called Widget_p and my main layout is :

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:mm="http://millennialmedia.com/android/schema"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/background"
    tools:context=".MainActivity" >

    <ImageView
        android:id="@+id/to"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:onClick="someMethod"
        android:background="@drawable/more" />

</RelativeLayout>


 @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {

        final int n = appWidgetIds.length;
        view = new RemoteViews(context.getPackageName(), R.layout.widgetlayout);

        for (int i = 0; i < n; i++) {  
               int appWidgetId = appWidgetIds[i];  

               Intent intent = new Intent(context, FlashWidget.class);
               intent.setAction(ACTION_WIDGET_RECEIVER);

               PendingIntent pend = PendingIntent.getBroadcast(context, 0, intent, 0);

               view.setOnClickPendingIntent(R.id.Button, pend);


               appWidgetManager.updateAppWidget(appWidgetId, view);
        }


    }

Can anybody provide some code for that use or give me a reference URL?

Foi útil?

Solução

Follow these guidelines and documentation and code for your understanding at : http://developer.android.com/guide/topics/appwidgets/index.html . It has enough details in detail step-by-step to dig in

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top