android中widgets的简单实现



桌面小部件,白一点就是一实现了广播接收的view.


分三部分:

第一部分


widget_info.xml
示例代码如下:
<?xml version="1.0" encoding="utf-8"?>
<!-- 创建AppWidgetProvider对象 -->
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
android:initialLayout="@layout/widget_main"
android:minHeight="210dp"
android:minWidth="320dp"
android:updatePeriodMillis="86400000" >
</appwidget-provider>




第二部分
widget_main.xml
示例代码如下:
<?xml version="1.0" encoding="utf-8"?>
<!-- AppWidget的初始布局文件 -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="0dp"
android:layout_height="0dp"
android:background="@drawable/*******"
android:orientation="vertical"
>


<!--widget内部要显示控件 (Widget的布局是基于RemoteViews对象,所以它并不能支持所


有的View)-->


<Button
android:id="@+id/widget_title_info"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:background="@drawable/*********"
android:gravity="left"
android:textColor="@color/widget_color" />
</LinearLayout>




第三部分TestProvider
示例代码如下:
public class TestProvider extends AppWidgetProvider {
private static SharedPreferences preferences;
private static Context mcontext;
private boolean canChange = true;


@Override
public void onEnabled(Context context) {
super.onEnabled(context);
mcontext = context;
}


@Override
public void onReceive(Context context, Intent intent) {
super.onReceive(context, intent);
mcontext = context;
}


@Override
public void onUpdate(Context context, AppWidgetManager


appWidgetManager, int[] appWidgetIds) {
super.onUpdate(context, appWidgetManager, appWidgetIds);
mcontext = context;
}


@Override
public void onDeleted(Context context, int[] appWidgetIds) {
super.onDeleted(context, appWidgetIds);
mcontext = context;
}


@Override
public void onDisabled(Context context) {
super.onDisabled(context);
mcontext = context;
}








/**
* 更新数据
*/
private class updateUI extends Thread {


@Override
public void run() {
AppWidgetManager appWidgetManager =


AppWidgetManager.getInstance(mcontext);
RemoteViews updateViews = null;
updateViews = buildUpdate(mcontext);
// 把更新信息推送到桌面的widget界面
if (updateViews != null) {
appWidgetManager.updateAppWidget(new


ComponentName(mcontext, TestProvider.class), updateViews);
}
canChange = true;
}


}



/**
* 更新布局
*/
public RemoteViews updateView(List<WidgetInfo> widgetInfos) {
System.out.println("update");


final int N = appWidgetIds.length;


for (int i=0; i<N; i++) {


int appWidgetId = appWidgetIds[i];


System.out.println("appWidgetId="+appWidgetId);


Intent intent = new Intent(context, AWTActivity.class);


PendingIntent pendingIntent = PendingIntent.getActivity(context, 0,


intent, 0);


RemoteViews views = new RemoteViews(context.getPackageName(),


R.layout.awl);


views.setOnClickPendingIntent(R.id.btn, pendingIntent);





appWidgetManager.updateAppWidget(appWidgetId, views);


}


}


}




第四部分
清单文件中注册广播
<!-- 注册实现类 -->
<receiver android:name=".TestProvider" >
<intent-filter >
<action


android:name="android.appwidget.action.APPWIDGET_UPDATE" />
<action android:name="com.jrm.network.state.change" />
<action android:name="android.provider.Telephony.SMS_RECEIVED"


/>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
</intent-filter>
<!-- 定义元数据 -->
<meta-data
android:name="android.appwidget.provider"
android:resource="@layout/widget_info" />
</receiver>

你可能感兴趣的:(android)