編輯:初級開發
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的。
1. 先定義XML布局文件,<SeekBar android:id=@+id/mySeek
58.List11 多選List 源碼就這些:注意第8行 final ListView listView = getListVIEw();獲得當前List&n
很多開發者考慮使自己的Android程序兼容多國語言,其實Google在設計Android時已經考慮了本地化問題,通過定義相關的資源可以自適應當前手機的語言來加載響應的
我首先從宏觀的角度觀察Binder,Service,Service Manager,並闡述各自的概念。從Linux的概念空間中,android的設計Activity托管