Abhishek
Abhishek

Reputation: 1345

How to avoid to creating multiple widgets in homescreen?

I am creating an application, and currently my requirement is to avoid user to create more than one App Widgets in Home Screen. I was trying like this

@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;
        }
    }

But the console is printing

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

Whereas you can find more than one app widget in the home screen. please help me, Thanks!

Upvotes: 0

Views: 1064

Answers (2)

Abhishek
Abhishek

Reputation: 1345

By Approaching the above solution, a little change in the 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();
    }
}

And it will restrict user to create more than one App Widget in Home Screen.

Upvotes: 1

ci_
ci_

Reputation: 8774

You have to use an "App Widget Configuration Activity" as described here:

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

and then figure out how many widgets there already are in this activity and leave the result as setResult(RESULT_CANCELED); if you discover there is already a widget present.

Example:

In your appwidget xml:

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

The "App Widget Configuration Activity":

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();
    }
}

Upvotes: 1

Related Questions