WEB开发网
开发学院手机开发Android 开发 android中的appwidget理解 阅读

android中的appwidget理解

 2012-10-13 21:09:58 来源:WEB开发网   
核心提示: public class MyAppWidget extends AppWidgetProvider{private static final String CLICK_ACTION="cxd.appwidget.BUTTON_ONCLICK";@Overridepublic void onDel

 public class MyAppWidget extends AppWidgetProvider{


private static final String CLICK_ACTION="cxd.appwidget.BUTTON_ONCLICK";
@Override
public void onDeleted(Context context, int[] appWidgetIds) {
// TODO Auto-generated method stub
System.out.println("-------------onDeleted----------------");
super.onDeleted(context, appWidgetIds);
}


//当最后一个appwidget删除后
@Override
public void onDisabled(Context context) {
// TODO Auto-generated method stub
System.out.println("-------------onDisabled----------------");
super.onDisabled(context);
}


//当第一次被创建时
@Override
public void onEnabled(Context context) {
// TODO Auto-generated method stub
System.out.println("-------------onEnabled----------------");
super.onEnabled(context);
}


//接收广播
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub

String actionString = intent.getAction();
System.out.println("-------------onReceive----------------"+actionString);
if(CLICK_ACTION.equals(actionString)){
System.out.println("===============");
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.appwidget01);
remoteViews.setTextViewText(R.id.text01, context.getResources().getString(R.string.hello_world));
remoteViews.setViewVisibility(R.id.button01, View.INVISIBLE);
Toast.makeText(context, "success", Toast.LENGTH_SHORT).show();
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
ComponentName componentName = new ComponentName(context, MyAppWidget.class);
appWidgetManager.updateAppWidget(componentName, remoteViews);
}else{
super.onReceive(context, intent);
}

}


//用于更新操作
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
// TODO Auto-generated method stub
System.out.println("-------------onUpdate----------------");

Intent intent = new Intent();
intent.setAction(CLICK_ACTION);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, -1, intent, 0);
RemoteViews rViews = new RemoteViews(context.getPackageName(), R.layout.appwidget01);
rViews.setOnClickPendingIntent(R.id.button01, pendingIntent);
appWidgetManager.updateAppWidget(appWidgetIds, rViews);

}

}
在manifest文件里面
<receiver android:name="MyAppWidget" android:exported="false">
<!-- 所需要满足的过滤器 -->
<intent-filter>
<action android:name="android

1 2  下一页

Tags:android appwidget 理解

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接