簡體   English   中英

如何避免在主屏幕中創建多個小部件?

[英]How to avoid to creating multiple widgets in homescreen?

我正在創建一個應用程序,當前我的要求是避免用戶在主屏幕中創建多個App Widget。 我嘗試這樣

@Override
    public void onReceive(Context context, Intent intent) {
        switch (intent.getAction()){
            case AppWidgetManager.ACTION_APPWIDGET_PICK:
                Util.logD(TAG, "---APP WIDGET PICKED----");
                break;
            case AppWidgetManager.ACTION_APPWIDGET_DELETED:
                Util.logD(TAG, "---APP WIDGET DELETED----");

                break;
            case AppWidgetManager.ACTION_APPWIDGET_BIND:
                Util.logD(TAG, "---APP WIDGET BINDED----");
                break;
            case AppWidgetManager.ACTION_APPWIDGET_CONFIGURE:
                Util.logD(TAG, "---APP WIDGET CONFIGURED----");
                break;
            case AppWidgetManager.ACTION_APPWIDGET_ENABLED:
                Util.logD(TAG, "---APP WIDGET ENABLED----");
                break;
            case AppWidgetManager.ACTION_APPWIDGET_DISABLED:
                Util.logD(TAG, "---APP WIDGET DISABLED----");
                break;

            case AppWidgetManager.ACTION_APPWIDGET_UPDATE:
                Util.logD(TAG, "---APP WIDGET UPDATED----");
                int ids[] =intent.getIntArrayExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS);
                Util.logD(TAG, "---TOTAL APP WIDGET----" + ids.length);
                Util.logD(TAG, "---WIDGET VALUE----" + ids[0]);
                    int [] id = {ids[0]};
                   RemoteViews views = new RemoteViews(MainApplication.getsApplicationContext().getPackageName(), R.layout.widget_layout);
                    AppWidgetManager manger = (AppWidgetManager) MainApplication.getsApplicationContext().getSystemService(Context.APPWIDGET_SERVICE);
                Util.logD(TAG, "---MANAGER SERVICIE----" + manger);

                 manger.updateAppWidget(id, views);
break;
        }
    }

但是控制台正在打印

--TOTAL APP WIDGET---1
--WIDGET VALUE--43

而您可以在主屏幕中找到多個應用程序小部件。 請幫助我,謝謝!

您必須按照以下說明使用“應用程序小部件配置活動”:

http://developer.android.com/guide/topics/appwidgets/index.html

然后找出該活動中已經有多少個小部件,並將結果保留為setResult(RESULT_CANCELED); 如果您發現已經存在一個小部件。

例:

在您的appwidget xml中:

<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
    ...
    android:configure="com.example.AppWidgetConfigurationActivity" 
    ...
</appwidget-provider>

“應用程序小部件配置活動”:

public class AppWidgetConfigurationActivity extends Activity {
    int mAppWidgetId = AppWidgetManager.INVALID_APPWIDGET_ID;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setResult(RESULT_CANCELED);
        Intent intent = getIntent();
        Bundle extras = intent.getExtras();
        if (extras != null) {
            mAppWidgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID);
        }
        if (mAppWidgetId == AppWidgetManager.INVALID_APPWIDGET_ID) {
            finish();
        }

        AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(this);
        int[] appWidgetIDs = appWidgetManager.getAppWidgetIds(new ComponentName(this, YourAppWidgetProvider.class ));

        if (appWidgetIDs.length <= 1) {
            Intent resultValue = new Intent();
            resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mAppWidgetId);
            setResult(RESULT_OK, resultValue);
        } 
        finish();
    }
}

通過采用上述解決方案,AppWidgetConfigurationActivity進行了一些更改,

public class AppWidgetConfigurationActivity extends ActionBarActivity {
    int mAppWidgetId = AppWidgetManager.INVALID_APPWIDGET_ID;
    private static final String TAG = AppWidgetConfigurationActivity.class.getSimpleName();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Util.logD(TAG, "---MYAPPWIDGET---");
//        setResult(RESULT_CANCELED);

        Intent intent = getIntent();
        Bundle extras = intent.getExtras();
        if (extras != null) {
            mAppWidgetId = extras.getInt(
                    AppWidgetManager.EXTRA_APPWIDGET_ID,
                    AppWidgetManager.INVALID_APPWIDGET_ID);
        }

        AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(this);
        ComponentName name = new ComponentName(MainApplication.getsApplicationContext(), MyWidgetProvider.class);
        int[] appWidgetIDs = appWidgetManager.getAppWidgetIds(name);
        Util.logD(TAG, "---INSTALLED WIDGETS---" + appWidgetIDs.length);
        if (appWidgetIDs.length == 1) {
            RemoteViews views = new RemoteViews(MainApplication.getsApplicationContext().getPackageName(), R.layout.widget_layout);
            appWidgetManager.updateAppWidget(mAppWidgetId, views);
            Intent resultValue = new Intent();
            resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mAppWidgetId);
            setResult(RESULT_OK, resultValue);
        }else{
            /*Remove rest of the widgets using AppWidgetHost*/
            int hostID = 22; // Itc oould be any value
                AppWidgetHost host = new AppWidgetHost(MainApplication.getsApplicationContext(), hostID);
                host.deleteAppWidgetId(appWidgetIDs[1]);
                AppWidgetProviderInfo info = appWidgetManager.getAppWidgetInfo(appWidgetIDs[1]);

            Util.showToast("Widget Already in Home Screen");
        }
        finish();
    }
}

並且它將限制用戶在主屏幕中創建多個App Widget。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM