Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> Android開發 >> 高級開發 >> Android Widget開發模板

Android Widget開發模板

編輯:高級開發

android上的Widget使用了Java語言開發比W3C的Widget運行效率提高了不少,可以做更多的事情調用系統的API,除了UI上的限制外,我們可以考慮幫助系統完善一些appWidget,android123給出大家一個開發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="http://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>

androidmanifest.xml上面提到的 \res\xml\widget.XML文件內容

<appwidget-provider XMLns:android="http://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="http://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>

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved