1

I created a android widget and when I added a configure activity the widget launch a kind of activity and close it but the widget don't show, Its obviously something wrong with the code in my Configure class:

package com.rb.widget;

import android.app.Activity;
import android.os.Bundle;

public class WidgetConfig extends Activity{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
    }



}

How should a configure class look like ?

4

2 に答える 2

1

onCreate関数では、呼び出す必要があります

/**
 * In onCreate() we have to ensure that if the user presses BACK or cancelled the activity,
 * then we should not add app widget.
 */
setResult(RESULT_CANCELED);

構成アクティビティを閉じると、ウィジェットがホームに追加されないようにします。次のコードは、ウィジェットを構成する方法を示しています。

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    /**
     * In onCreate() we have to ensure that if the user presses BACK or cancelled the activity,
     * then we should not add app widget.
     */
    setResult(RESULT_CANCELED);

    setContentView(R.layout.activity_widget_settings);

    Intent intent = getIntent();
    Bundle extras = intent.getExtras();
    if (extras != null){
        appWidgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID,
                AppWidgetManager.INVALID_APPWIDGET_ID);
    }
    if (appWidgetId == AppWidgetManager.INVALID_APPWIDGET_ID){
        finish();
        return;
    }
    appWidgetManager = AppWidgetManager.getInstance(this);
    views = new RemoteViews(this.getPackageName(), R.layout.my_app_widget);
}

設定したら、忘れずに次を呼び出してください。

Intent widgetIntent = new Intent();
widgetIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
setResult(RESULT_OK,widgetIntent);
appWidgetManager.updateAppWidget(appWidgetId, views);
于 2016-05-01T05:59:50.377 に答える