在为选择窗口小部件删除窗口小部件时启动活动

时间:2014-09-25 17:48:34

标签: android android-activity widget android-widget android-appwidget

我需要为选择小部件制作活动

Widget1.java

public class Widget1 extends AppWidgetProvider{

Context context;

@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
        int[] appWidgetIds) {
    super.onUpdate(context, appWidgetManager, appWidgetIds);
    if (appWidgetIds != null) {
        int N = appWidgetIds.length;

        for (int mAppWidgetId : appWidgetIds) {

            Intent intent = new Intent(context, UpdateWidgetService.class);

            intent.putExtra(EXTRA_APPWIDGET_ID, mAppWidgetId);
            intent.setAction("FROM WIDGET PROVIDER");
            context.startService(intent);
        }

    }

}

public static class UpdateWidgetService extends IntentService {
    public UpdateWidgetService() {
        // only for debug purpose
        super("UpdateWidgetService");

    }

    @Override
    protected void onHandleIntent(Intent intent) {
        AppWidgetManager appWidgetManager = AppWidgetManager
                .getInstance(UpdateWidgetService.this);

        int incomingAppWidgetId = intent.getIntExtra(EXTRA_APPWIDGET_ID,
                INVALID_APPWIDGET_ID);

        if (incomingAppWidgetId != INVALID_APPWIDGET_ID) {
            try {
                updateNewsAppWidget(appWidgetManager, incomingAppWidgetId,
                        intent);
            } catch (NullPointerException e) {
            }

        }

    }

    public void updateNewsAppWidget(AppWidgetManager appWidgetManager,
            int appWidgetId, Intent intent) {
        Log.v("String package name", this.getPackageName());
        RemoteViews views = new RemoteViews(this.getPackageName(),
                R.layout.widget1);

        appWidgetManager.updateAppWidget(appWidgetId, views);
    }
}
}

Widget2.java

public class widget2 extends AppWidgetProvider{
Context context;

@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
        int[] appWidgetIds) {
    super.onUpdate(context, appWidgetManager, appWidgetIds);
    if (appWidgetIds != null) {
        int N = appWidgetIds.length;

        for (int mAppWidgetId : appWidgetIds) {

            Intent intent = new Intent(context, UpdateWidgetService.class);

            intent.putExtra(EXTRA_APPWIDGET_ID, mAppWidgetId);
            intent.setAction("FROM WIDGET PROVIDER");
            context.startService(intent);
        }

    }

}

public static class UpdateWidgetService extends IntentService {
    public UpdateWidgetService() {
        // only for debug purpose
        super("UpdateWidgetService");

    }

    @Override
    protected void onHandleIntent(Intent intent) {
        AppWidgetManager appWidgetManager = AppWidgetManager
                .getInstance(UpdateWidgetService.this);

        int incomingAppWidgetId = intent.getIntExtra(EXTRA_APPWIDGET_ID,
                INVALID_APPWIDGET_ID);

        if (incomingAppWidgetId != INVALID_APPWIDGET_ID) {
            try {
                updateNewsAppWidget(appWidgetManager, incomingAppWidgetId,
                        intent);
            } catch (NullPointerException e) {
            }

        }

    }

    public void updateNewsAppWidget(AppWidgetManager appWidgetManager,
            int appWidgetId, Intent intent) {
        Log.v("String package name", this.getPackageName());
        RemoteViews views = new RemoteViews(this.getPackageName(),
                R.layout.widget2);

        appWidgetManager.updateAppWidget(appWidgetId, views);
    }
}
    }

widget1.xml

<AnalogClock
    android:id="@+id/analogClock1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true" 
    android:dial="@drawable/hand_dial"
    android:hand_minute="@drawable/hand_minute"
    android:hand_hour="@drawable/hand_hour"/>

widget2.xml

    <AnalogClock
    android:id="@+id/analogClock2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true" 
    android:dial="@drawable/hand_dial2"
    android:hand_minute="@drawable/hand_minute2"
    android:hand_hour="@drawable/hand_hour2"/>

widget1_config.xml

    <appwidget-provider 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:minWidth="140dip" 
android:minHeight="140dip" 
android:previewImage="@drawable/preview"
android:initialLayout="@layout/widget1"
android:updatePeriodMillis="1800000" 
android:resizeMode="horizontal|vertical"/>

的AndroidManifest.xml

        <receiver android:name=".Widget1" android:label="Analog Clock">
        <intent-filter>
            <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
        </intent-filter>
        <meta-data android:name="android.appwidget.provider" android:resource="@xml/widget_config" />

    </receiver>
        <receiver android:name=".widget2" android:label="Analog Clock2">
    <intent-filter>
        <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
    </intent-filter>
    <meta-data android:name="android.appwidget.provider"
        android:resource="@xml/widget2_config" />

我可以在一个应用程序中为每个小部件制作十个小部件单,但这不是很好

所以我需要在为选择小部件删除小部件时启动活动

我该怎么做?

最后,我为我的英语不好而道歉

谢谢。

1 个答案:

答案 0 :(得分:2)

您需要将android:configure属性添加到窗口小部件提供程序XML。见https://github.com/slightfoot/android-note-widget/blob/master/res/xml/appwidget_note.xml

<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
    android:configure="com.demondevelopers.notewidget.ConfigActivity"
    android:initialKeyguardLayout="@layout/appwidget_note"
    android:initialLayout="@layout/appwidget_note"
    android:minHeight="110dp"
    android:minWidth="180dp"
    android:minResizeWidth="40dp"
    android:minResizeHeight="40dp"
    android:resizeMode="horizontal|vertical"
    android:widgetCategory="home_screen"
    android:updatePeriodMillis="0"
    />

有关完整示例,请查看我的项目: https://github.com/slightfoot/android-note-widget