【问题标题】:How can you create a widget inside an android application? (Use App Widget Host class?)如何在 android 应用程序中创建小部件? (使用 App Widget Host 类?)
【发布时间】:2014-02-19 21:19:20
【问题描述】:

我需要创建一个包含多个小部件的应用程序。这些不是桌面小部件。我需要能够与这些小部件进行交互,就好像它们是桌面小部件一样,但它们需要封装在更大的应用程序中。 (每个小部件在单击时都有自己的功能和行为。)

这在android中可能吗?或者我是否需要创建一个应用程序并创建每个我想像小部件一样实际作为视图的对象?

例如。父应用程序适用于汽车。 “应用内”小部件的示例包括:换油历史(显示最近三个换油日期的列表,单击日期将打开收据扫描等)、胎压监测器、圈速历史(显示最近四圈, 捏和展开会显示多于四个)等。

我可以将这些对象中的每一个都制作成小部件吗?还是必须是应用内的视图?

编辑:Android 开发人员的App Widget Host page 提到:“AppWidgetHost 为希望在其 UI 中嵌入应用小部件的应用(例如主屏幕)提供与 AppWidget 服务的交互。” 有没有人创建了自己的 App Widget Host 或直接使用这个类?

【问题讨论】:

标签: android-widget widget android android-appwidget


【解决方案1】:

正常创建你的appwidget

然后在您的活动中添加此代码 调用 selectWidget() 以打开弹出窗口以选择可用的小部件

//init
mAppWidgetManager = AppWidgetManager.getInstance(this);
mAppWidgetHost = new AppWidgetHost(this, R.id.APPWIDGET_HOST_ID);

//select widget
void selectWidget() {
    int appWidgetId = this.mAppWidgetHost.allocateAppWidgetId();
    Intent pickIntent = new Intent(AppWidgetManager.ACTION_APPWIDGET_PICK);
    pickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
    addEmptyData(pickIntent);
    startActivityForResult(pickIntent, R.id.REQUEST_PICK_APPWIDGET);
}
void addEmptyData(Intent pickIntent) {
    ArrayList customInfo = new ArrayList();
    pickIntent.putParcelableArrayListExtra(AppWidgetManager.EXTRA_CUSTOM_INFO, customInfo);
    ArrayList customExtras = new ArrayList();
    pickIntent.putParcelableArrayListExtra(AppWidgetManager.EXTRA_CUSTOM_EXTRAS, customExtras);
};

//Configure the widget
@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) {
            mAppWidgetHost.deleteAppWidgetId(appWidgetId);
        }
    }
}

private void configureWidget(Intent data) {
    Bundle extras = data.getExtras();
    int appWidgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
    AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.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);
    }
}

//adding it to you view
public void createWidget(Intent data) {
    Bundle extras = data.getExtras();
    int appWidgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
    AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
    AppWidgetHostView hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
    hostView.setAppWidget(appWidgetId, appWidgetInfo);
    layout.addView(hostView);
}

//Update widget
@Override
protected void onStart() {
    super.onStart();
    mAppWidgetHost.startListening();
}
@Override
protected void onStop() {
    super.onStop();
    mAppWidgetHost.stopListening();
}

//Now to remove it call this
public void removeWidget(AppWidgetHostView hostView) {
    mAppWidgetHost.deleteAppWidgetId(hostView.getAppWidgetId());
    layout.removeView(hostView);
}

希望对你有帮助

【讨论】:

    【解决方案2】:

    如果你想在你的应用中嵌入一个特定的小部件,并且知道包名和类名:

    public boolean createWidget(View view, String packageName, String className) {
        // Get the list of installed widgets
        AppWidgetProviderInfo newAppWidgetProviderInfo = null;
        List<AppWidgetProviderInfo> appWidgetInfos;
        appWidgetInfos = mAppWidgetManager.getInstalledProviders();
        boolean widgetIsFound = false;
        for(int j = 0; j < appWidgetInfos.size(); j++)
        {
            if (appWidgetInfos.get(j).provider.getPackageName().equals(packageName) && appWidgetInfos.get(j).provider.getClassName().equals(className))
            {
                // Get the full info of the required widget
                newAppWidgetProviderInfo = appWidgetInfos.get(j);
                widgetIsFound = true;
                break;
            }
        }
    
        if (!widgetIsFound) {
            return false;
        } else {
            // Create Widget
            int appWidgetId = mAppWidgetHost.allocateAppWidgetId();
            AppWidgetHostView hostView = mAppWidgetHost.createView(getApplicationContext(), appWidgetId, newAppWidgetProviderInfo);
            hostView.setAppWidget(appWidgetId, newAppWidgetProviderInfo);
    
            // Add it to your layout
            LinearLayout widgetLayout = view.findViewById(R.id.widget_view);
            widgetLayout.addView(hostView);
    
            // And bind widget IDs to make them actually work
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
                boolean allowed = mAppWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId, newAppWidgetProviderInfo.provider);
    
                if (!allowed) {
                    // Request permission - https://stackoverflow.com/a/44351320/1816603
                    Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
                    intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
                    intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, newAppWidgetProviderInfo.provider);
                    final int REQUEST_BIND_WIDGET = 1987;
                    startActivityForResult(intent, REQUEST_BIND_WIDGET);
                }
            }
    
            return true;
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2013-02-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-09
      • 1970-01-01
      相关资源
      最近更新 更多