編輯:高級開發
public class WordWidget extends AppWidgetProvider { //appWidget
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
context.startService(new Intent(context, UpdateService.class)); //避免ANR,所以Widget中開了個服務
}
public static class UpdateService extends Service {
@Override
public void onStart(Intent intent, int startId) {
// Build the widget update for today
RemoteViews updateVIEws = buildUpdate(this);
ComponentName thisWidget = new ComponentName(this, WordWidget.class);
AppWidgetManager manager = AppWidgetManager.getInstance(this);
manager.updateAppWidget(thisWidget, updateVIEws);
}
public RemoteVIEws buildUpdate(Context context) {
// Pick out month names from resources
Resources res = context.getResources();
String[] monthNames = res.getStringArray(R.array.month_names);
Time today = new Time();
today.setToNow();
String pageName = res.getString(R.string.template_wotd_title,
monthNames[today.month], today.monthDay);
RemoteViews updateVIEws = null;
String pageContent = "";
try {
SimpleWikiHelper.prepareUserAgent(context);
pageContent = SimpleWikiHelper.getPageContent(pageName, false);
} catch (ApIException e) {
Log.e("WordWidget", "Couldn't contact API", e);
} catch (ParseException e) {
Log.e("WordWidget", "Couldn't parse API response", e);
}
Pattern pattern = Pattern.compile(SimpleWikiHelper.Word_OF_DAY_REGEX); //正則表達式處理,有關定義見下面的SimpleWikiHelper類
Matcher matcher = pattern.matcher(pageContent);
if (matcher.find()) {
updateViews = new RemoteVIEws(context.getPackageName(), R.layout.widget_Word);
String WordTitle = matcher.group(1);
updateViews.setTextVIEwText(R.id.word_title, WordTitle);
updateViews.setTextVIEwText(R.id.Word_type, matcher.group(2));
updateViews.setTextVIEwText(R.id.definition, matcher.group(3).trim());
String definePage = res.getString(R.string.template_define_url,
Uri.encode(WordTitle));
Intent defineIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(definePage)); //這裡是打開相應的網頁,所以Uri是http的url,action是vIEw即打開web浏覽器
PendingIntent pendingIntent = PendingIntent.getActivity(context,
0 /* no requestCode */, defineIntent, 0 /* no flags */);
updateVIEws.setOnClickPendingIntent(R.id.widget, pendingIntent); //單擊Widget打開Activity
} else {
updateViews = new RemoteVIEws(context.getPackageName(), R.layout.widget_message);
CharSequence errorMessage = context.getText(R.string.widget_error);
updateViews.setTextVIEwText(R.id.message, errorMessage);
}
return updateVIEws;
}
@Override
public IBinder onBind(Intent intent) {
// We don't need to bind to this service
return null;
}
}
}
有關網絡通訊的實體類,以及一些常量定義如下:
public class SimpleWikiHelper {
private static final String TAG = "SimpleWikiHelper";
public static final String Word_OF_DAY_REGEX =
"(?s)\\{\\{wotd\\|(.+?)\\|(.+?)\\|([^#\\|]+).*?\\}\\}";
private static final String WIKTIONARY_PAGE =
"http://en.wiktionary.org/w/api.PHP?action=query&prop=revisions&titles=%s&" +
"rvprop=content&format=JSon%s";
private static final String WIKTIONARY_EXPAND_TEMPLATES =
"&rvexpandtemplates=true";
private static final int HTTP_STATUS_OK = 200;
private static byte[] sBuffer = new byte[512];
private static String sUserAgent = null;
public static class ApIException extends Exception {
public ApIException(String detailMessage, Throwable throwable) {
super(detailMessage, throwable);
}
public ApIException(String detailMessage) {
super(detailMessage);
}
}
public static class ParseException extends Exception {
public ParseException(String detailMessage, Throwable throwable) {
super(detailMessage, throwable);
}
}
public static void prepareUserAgent(Context context) {
try {
// Read package name and version number from manifest
PackageManager manager = context.getPackageManager();
PackageInfo info = manager.getPackageInfo(context.getPackageName(), 0);
sUserAgent = String.format(context.getString(R.string.template_user_agent),
info.packageName, info.versionName);
} catch(NameNotFoundException e) {
Log.e(TAG, "Couldn't find package information in PackageManager", e);
}
}
public static String getPageContent(String title, boolean expandTemplates)
throws ApIException, ParseException {
String encodedTitle = Uri.encode(title);
String expandClause = expandTemplates ? WIKTIONARY_EXPAND_TEMPLATES : "";
String content = getUrlContent(String.format(WIKTIONARY_PAGE, encodedTitle, expandClause));
try {
JSONObject response = new JSONObject(content);
JSONObject query = response.getJSONObject("query");
JSONObject pages = query.getJSONObject("pages");
JSONObject page = pages.getJSONObject((String) pages.keys().next());
JSONArray revisions = page.getJSONArray("revisions");
JSONObject revision = revisions.getJSONObject(0);
return revision.getString("*");
} catch (JSONException e) {
throw new ParseException("Problem parsing API response", e);
}
}
protected static synchronized String getUrlContent(String url) throws ApIException {
if (sUserAgent == null) {
throw new ApIException("User-Agent string must be prepared");
}
HttpClient client = new DefaultHttpClIEnt();
HttpGet request = new HttpGet(url);
request.setHeader("User-Agent", sUserAgent); //設置客戶端標識
try {
HttpResponse response = clIEnt.execute(request);
StatusLine status = response.getStatusLine();
if (status.getStatusCode() != HTTP_STATUS_OK) {
throw new ApIException("Invalid response from server: " +
status.toString());
}
HttpEntity entity = response.getEntity();
InputStream inputStream = entity.getContent(); //獲取HTTP返回的數據流
ByteArrayOutputStream content = new ByteArrayOutputStream();
int readBytes = 0;
while ((readBytes = inputStream.read(sBuffer)) != -1) {
content.write(sBuffer, 0, readBytes); //轉化為字節數組流
}
return new String(content.toByteArray()); //從字節數組構建String
} catch (IOException e) {
throw new ApIException("Problem communicating with API", e);
}
}
}
有關整個每日維基的widget例子比較簡單,主要是幫助大家積累常用代碼,了解android平台 JSON的處理方式,畢竟很多Server還是Java的。
片段(Fragments)是一個應用程序的用戶界面或行為活動,使活動更加模塊化設計,可以放置在一塊。一個片段是一種子活動。以下要點有關片段:片段都有自己的布局和規范自己的
Android操作系統是一個堆棧的軟件組件,它大致分為五個部分和四個主要層的體系結構,如下圖所示。Linux內核在層次的底部是 Linux - Linux 2.6。它提供
Service(服務)是一種在後台運行,執行長時間運行的操作,無需與用戶交互的組件。例如,一個服務可以在後台播放音樂,用戶在不同的應用程序或者可能通過網絡獲取數據,而不阻
android 3.0 Honeycomb(蜂巢)已經在2011年CES上發布,然後現在android 3.0 SDK最終預覽版也放了出來,過段時間2011年Googl