我创建了一个简单的主屏幕小部件,它在网格视图中使用预定义的应用程序图标显示当前时间和日期。我正在使用 OnReceive 事件的警报管理器更新日期和时间。当我单击时钟时,它会打开配置屏幕。
我的问题是,当我运行其他一些主要是游戏的应用程序时,应用程序图标从小部件中消失,并且小部件在单击时停止响应。但 OnReceive 方法正常工作并更新时钟。
我不明白为什么会这样。请帮我。
接收代码
@Override
public void onReceive(Context context, Intent intent) {
super.onReceive(context, intent);
// // Get shared prefs
myPref = new SharedPref(context);
try {
Log.d(LOG_TAG, "OnRecive Called : " + intent.getAction());
if (CLOCK_WIDGET_UPDATE.equals(intent.getAction())) {
Log.d(LOG_TAG, "Clock update");
// Get the widget manager and ids for this widget provider, then
// // call the shared // clock update method.
ComponentName thisAppWidget = new ComponentName(context.getPackageName(), getClass().getName());
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
int ids[] = appWidgetManager.getAppWidgetIds(thisAppWidget);
for (int appWidgetID : ids) {
updateAppWidget(context, appWidgetManager, appWidgetID);
}
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// mWidgetId = getWidgetId(intent);
}
更新代码
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
final int N = appWidgetIds.length;
PendingIntent pendingIntent = null;
myPref = new SharedPref(context);
mWidgetId = appWidgetIds[N - 1];
addWidget_Database_Detail(context);
// insert Add Button
// insertAddAppButton(context);
Log.d(LOG_TAG, "Updating Example Widgets.");
Random randomGenerator = new Random();
// Perform this loop procedure for each App Widget that belongs to this
// provider
for (int i = 0; i < N; i++) {
int appWidgetId = appWidgetIds[i];
// Create an Intent to launch ExampleActivity
Intent intent = new Intent(context, Configuration_Activity.class);
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
// Get the layout for the App Widget and attach an on-click listener
// to the button
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget_layout);
pendingIntent = PendingIntent.getActivity(context, randomGenerator.nextInt(100), intent, 0);
views.setOnClickPendingIntent(R.id.linearLayout_Time, pendingIntent);
setUpAppGrid(context, appWidgetManager, mWidgetId);
// Tell the AppWidgetManager to perform an update on the current app
// widget
appWidgetManager.updateAppWidget(appWidgetId, views);
// Update The clock label using a shared method
updateAppWidget(context, appWidgetManager, appWidgetId);
}
}