【问题标题】:Hosting widgets in an android launcher在 android 启动器中托管小部件
【发布时间】:2012-07-13 11:56:09
【问题描述】:

我正在为 android 制作一个启动器,但我被困在小部件部分。我在互联网上搜索了大约一个半小时,试图弄清楚如何在我的应用程序中托管小部件,但没有运气。

我已经查看了一些库存启动器和 ADW 启动器代码,但两者都只有数英里的代码,这是我第一次制作启动器。

有人可以指导我如何将小部件添加到我的启动器中吗?或者至少发布任何链接/教程?请解释一下,因为这是我第一次。

【问题讨论】:

标签: java android widget launcher


【解决方案1】:

试试这个:

final int APPWIDGET_HOST_ID = 2048;
final int REQUEST_PICK_APPWIDGET = 0;
final int REQUEST_CREATE_APPWIDGET = 5;

AppWidgetManager appWidgetManager;
AppWidgetHost appWidgetHost;

// Let user pick a widget from the list of intalled AppWidgets
public void selectWidget()
{
    int appWidgetId = this.appWidgetHost.allocateAppWidgetId();
    Intent pickIntent = new Intent(AppWidgetManager.ACTION_APPWIDGET_PICK);
    pickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
    addEmptyData(pickIntent);
    startActivityForResult(pickIntent, REQUEST_PICK_APPWIDGET);
}

// For some reason you have to add this empty data, else it won't work
public void addEmptyData(Intent pickIntent)
{
    ArrayList<AppWidgetProviderInfo> customInfo = 
        new ArrayList<AppWidgetProviderInfo>();
    pickIntent.putParcelableArrayListExtra(
        AppWidgetManager.EXTRA_CUSTOM_INFO, customInfo);
    ArrayList<Bundle> customExtras = new ArrayList<Bundle>();
    pickIntent.putParcelableArrayListExtra(
        AppWidgetManager.EXTRA_CUSTOM_EXTRAS, customExtras);
};

@Override
protected void onActivityResult(int requestCode, int resultCode, 
                                Intent data) {
    if (resultCode == RESULT_OK ) {
        if (requestCode == REQUEST_PICK_APPWIDGET) {
            configureWidget(data);
        }
        else if (requestCode == REQUEST_CREATE_APPWIDGET) {
            createWidget(data);
        }
    }
    else if (resultCode == RESULT_CANCELED && data != null) {
        int appWidgetId = 
            data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
        if (appWidgetId != -1) {
            appWidgetHost.deleteAppWidgetId(appWidgetId);
        }
    }
}

// Show configuration activity of the widget picked by the user
private void configureWidget(Intent data) {
    Bundle extras = data.getExtras();
    int appWidgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
    AppWidgetProviderInfo appWidgetInfo = 
            appWidgetManager.getAppWidgetInfo(appWidgetId);
    if (appWidgetInfo.configure != null) {
        Intent intent = 
            new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
        intent.setComponent(appWidgetInfo.configure);
        intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
        startActivityForResult(intent, REQUEST_CREATE_APPWIDGET);
    } else {
        createWidget(data);
    }
}

// Get an instance of the selected widget as a AppWidgetHostView
public void createWidget(Intent data) {
    Bundle extras = data.getExtras();
    int appWidgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
    AppWidgetProviderInfo appWidgetInfo = appWidgetManager.getAppWidgetInfo(appWidgetId);

    AppWidgetHostView hostView = appWidgetHost.createView(this, appWidgetId, appWidgetInfo);
    hostView.setAppWidget(appWidgetId, appWidgetInfo);
    // Add  it on the layout you want
    myLayout.addView(hostView);
}

// Call this when you want to remove one from your layout
public void removeWidget(AppWidgetHostView hostView) {
    appWidgetHost.deleteAppWidgetId(hostView.getAppWidgetId());

    // Remove from your layout
    myLayout.removeView(hostView);
}

@Override
protected void onStart() {
    super.onStart();
    appWidgetManager = AppWidgetManager.getInstance(this);
    appWidgetHost = new AppWidgetHost(this, APPWIDGET_HOST_ID);

    // Start listening to pending intents from the widgets
    appWidgetHost.startListening();
}

@Override
protected void onStop() {
    super.onStop();
    appWidgetHost.stopListening();
}

您只需调用方法selectWidget() 即可显示设备上安装的AppWidgets 列表以供选择。

此代码有点像普通 Android 启动器的修改版本。

编辑

要绑定小部件 ID,即更新小部件并响应用户交互,请参阅此处的解决方案:Widgets don't respond when re-added through code

【讨论】:

  • 这是一个即插即用的代码。如有疑问,可以提问。
  • 在调试模式下运行它并尝试找出你在“resultCode”和“requestCode”中得到的值。尝试相应地更改“REQUEST_PICK_APPWIDGET”、“REQUEST_CREATE_APPWIDGET”和“RESULT_OK”的值。
  • @Kamran 谢谢,效果很好。知道为什么当我使用启动器小部件选择器时它不显示所有显示的小部件吗?
  • 不应该这样...我希望您指的不是仅出现在特定启动器中的小部件,例如“Go SMS”和类似的东西,是吗?
  • 我几乎不好意思发布这个,但如果其他人无法弄清楚如何处理你的布局,你需要添加一个属性 'android:id="@+id/mainLayout "' 到XML中的LinearLayout,此时可以在代码中做LinearLayout myLayout = (LinearLayout)findViewById(R.id.mainLayout)。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-03-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-05-25
相关资源
最近更新 更多