我正在创建一个应用程序,目前我的要求是避免用户在主屏幕中创建多个App Widgets。 我是这样尝试的
@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
您可以在主屏幕中找到多个应用小部件。 请帮帮我,谢谢!
答案 0 :(得分:1)
您必须使用"应用小工具配置活动"如下所述:
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>
&#34;应用小工具配置活动&#34;:
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();
}
}
答案 1 :(得分:0)
通过接近上述解决方案,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。