本文介绍了在Android发射主机部件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想提出一个发射器为Android和我被困在小部件的一部分。我一直在寻找了大约一个小时和互联网试图找出我如何可以承载部件在我的应用程序上的一半,但没有运气。

I am making a launcher for android, and I'm stuck at the widget part. I've been searching for about an hour and a half on the internet trying to figure out how can I host widgets in my application, but no luck.

我已经通过一些股票发射器和ADW启动codeS但是code两者只是英里去了,这是我第一次做一个发射器。

I have went through some stock launchers and ADW launcher codes but both just have miles of code and this is the first time I'm making a launcher.

可能有人请指导我通过我怎么能添加小工具到我的启动?或者至少售后的任何环节/教程?请解释一下,因为这是我的第一次。

Could someone please guide me through how can I add widgets into my launcher? Or at least post any links/tutorials? Please explain it since this is my first time.

推荐答案

试试这个:

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的列表。

All you've got to do is call the method selectWidget() whenever you want to show a list of AppWidgets installed on the device to pick from.

这code是位普通的Andr​​oid发射器的修改版本。

This code is a bit of a modified version of the stock Android launcher.

这篇关于在Android发射主机部件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-21 07:16