Android Widget開發模板解析
Android Widget開發模板是本文要介紹的內容,主要是來了解並學習Android Widget開發應用,Android Widget中使用了Java語言開發比W3C的Widget運行效率提高了不少,可以做更多的事情調用係統的API,除了UI上的限製外,我們可以考慮幫助係統完善一些appWidget,Android 123給出大家一個開發Widget的模板。
- public class cwjWidget extends AppWidgetProvider {
- @Override
- public void onUpdate(Context context, AppWidgetManager appWidgetManager,
- int[] appWidgetIds) {
- context.startService(new Intent(context, UpdateService.class)); //這裏創建一個服務,防止出現等待超時對話框
- }
- public static class UpdateService extends Service { //這個內部的服務我們推薦新開一個線程操作一些容易阻塞的情況,比如網絡下載等等
- @Override
- public void onStart(Intent intent, int startId) {
- RemoteViews updateViews = buildUpdate(this);
- ComponentName thisWidget = new ComponentName(this, cwjWidget.class);
- AppWidgetManager manager = AppWidgetManager.getInstance(this);
- manager.updateAppWidget(thisWidget, updateViews);
- }
- public RemoteViews buildUpdate(Context context) {
- Resources res = context.getResources();
- RemoteViews updateViews = new RemoteViews(
- context.getPackageName(), R.layout.main); //主Widget的layout布局
- PendingIntent pendingIntent = PendingIntent.getActivity(context,
- 0 /* no requestCode */,
- new Intent(android.provider.Settings.ACTION_DEVICE_INFO_SETTINGS),
- 0 /* no flags */);
- updateViews.setOnClickPendingIntent(R.id.ui, pendingIntent); //單擊view打開intent,目標為係統信息,就是上麵的action位置
- updateViews.setTextViewText(R.id.info,
- android.os.Build.VERSION.CODENAME + " " +
- android.os.Build.ID); //這裏是API的獲取係統版本的方法
- updateViews.setTextViewText(R.id.changelist,
- android.os.Build.FINGERPRINT
- );
- return updateViews;
- }
- @Override
- public IBinder onBind(Intent intent) {
- return null;
- }
- }
- }
有關涉及到的 androidmanifest.xml內容
- xml version="1.0" encoding="utf-8"?>
- manifest xmlns:android="https://schemas.android.com/apk/res/android"
- package="com.android123.widget"
- android:versionCode="1"
- android:versionName="1.0">
- <uses-SDK android:minSdkVersion="3" />
- <application android:icon="@drawable/icon" android:label="@string/app_name">
- <receiver android:name=".BuildWidget" android:label="android123_cwj">
- <intent-filter>
- <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
- intent-filter>
- <meta-data android:name="android.appwidget.provider" android:resource="@xml/widget" />
- receiver>
- <service android:name=".cwjWidget$UpdateService" />
- application>
- /manifest>
android manifest.xml上麵提到的 \res\xml\widget.xml文件內容
- <appwidget-provider xmlns:android="https://schemas.android.com/apk/res/android"
- android:minWidth="150dip"
- android:minHeight="72dip"
- android:updatePeriodMillis="0"
- android:initialLayout="@layout/widget" />
有關 main.xml的內容為
- xml version="1.0" encoding="utf-8"?>
- LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
- android:id="@+id/ui"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:orientation="vertical"
- android:padding="6dip"
- >
- <TextView
- android:id="@+id/info"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:gravity="left"
- android:textSize="18dip"
- />
- <TextView
- android:id="@+id/changelist"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:gravity="left"
- android:layout_marginTop="4dip"
- android:textSize="9dip"
- />
- /LinearLayout>
小結:Android Widget開發模板解析的內容介紹完了,希望通過Android Widget開發內容的學習能對你有所幫助!
最後更新:2017-04-03 22:15:26