如何调用我的小部件?

时间:2014-01-02 14:42:06

标签: java android android-layout widget android-appwidget

我想在我的应用中点击按钮后在主页上创建我的小部件。我的小部件的类叫做Widget_p,我的主要布局是:

<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);
        }


    }

任何人都可以为该用途提供一些代码或给我一个参考网址吗?

1 个答案:

答案 0 :(得分:0)

请遵循以下指南和文档及代码,以便您了解:http://developer.android.com/guide/topics/appwidgets/index.html。它有详细的详细信息,一步一步地深入挖掘