写了一个简单的widget应用
但在横竖屏切换时按键就不能响应了,猜测是View刷新引起的;
关键函数如下,求指正:
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
//创建一个Intent对象
for(int i=0;i<appWidgetIds.length;i++)
{
Intent intent = new Intent();
//为Intent对象设置Action
intent.setAction(UPDATE_ACTION);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.activity_main);
remoteViews.setOnClickPendingIntent(R.id.btn, pendingIntent);
appWidgetManager.updateAppWidget(appWidgetIds, remoteViews);
super.onUpdate(context, appWidgetManager, appWidgetIds);
}
}
public void onReceive(Context context, Intent intent) {
// super.onReceive(context, intent);
String action = intent.getAction();
RemoteViews remoteViews = new RemoteViews(context.getPackageName(),R.layout.activity_main);
Log.d(TAG, "onReceive()->on action:" + action);
spp = new exampleHelper(context, "contacts");
curState = spp.getValue(COLUMN_KEY);
String curState = spp.getValue(COLUMN_KEY);
if(curState == null)
{
spp.putValue(COLUMN_KEY, "2");
} else {
Log.d(TAG, "data base is exist--cur value:" + curState);
} else {
Log.d(TAG, "system not get infor.");
}
// 更新UI
AppWidgetManager appWidgetManager=AppWidgetManager.getInstance(context);
ComponentName componentName=new ComponentName(context,MainActivity.class);
// int[] appW = appWidgetManager.getAppWidgetIds(new ComponentName(context,MainActivity.class));