android中的appwidget理解巩固

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.appwidget.action.APPWIDGET_UPDATE" />
            </intent-filter>
            <intent-filter>
                <action android:name="cxd.appwidget.BUTTON_ONCLICK" />
            </intent-filter>

             <meta-data
                android:name="android.appwidget.provider"
                android:resource="@xml/appwidget01_info" />
        </receiver>

学了这么久,总结点appwidget的自己理解的东西记录下来!

执行顺序:

首先-->在桌面创建appwidget桌面小程序,

重点是  <intent-filter>
                <action android:name="cxd.appwidget.BUTTON_ONCLICK" />
            </intent-filter>

和代码里的Intent intent = new Intent();
intent.setAction(CLICK_ACTION);

PendingIntent pendingIntent = PendingIntent.getBroadcast(context, -1, intent, 0);和String actionString = intent.getAction();

intent.setAction(CLICK_ACTION);是将private static final String CLICK_ACTION="cxd.appwidget.BUTTON_ONCLICK";设置action来执行

然后和manifest里的 <intent-filter>
                <action android:name="cxd.appwidget.BUTTON_ONCLICK" />
            </intent-filter>比对,如果相匹配就为pendingIntent对象的广播事件设置这个action,所以在onReceive里面就用String actionString = intent.getAction();得到

在manifest匹配的action对象



还有种比较简单的使用getActivity(Context context, int requestCode, Intent intent, int flags);  创建PendingIntent对象

其中的intent对象是通过Intent  intent = new Intent(context,XXX.class);来创建的,可以开启一个activity



posted @ 2012-10-13 20:55  java程序员-c  阅读(153)  评论(0编辑  收藏  举报