編輯:高級開發
package com.example.android.apis.appwidget;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.SystemClock;
import android.util.Log;
import android.widget.RemoteVIEws;
import Java.util.ArrayList;
import com.example.android.apis.R;
涉及到的文件有
ExampleAppWidgetConfigure.Java
ExampleBroadcastReceiver.Java
res/layout/appwidget_configure.XML
res/layout/appwidget_provider.XML
res/xml/appwidget_provider.XML
public class ExampleAppWidgetProvider extends AppWidgetProvider {
private static final String TAG = "ExampleAppWidgetProvider"; //logcat調試信息
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
Log.d(TAG, "onUpdate");
// - 創建一個RemoteVIEws 對象
// - 設置RemoteVIEws 對象的文本
// - 告訴AppWidgetManager 顯示 vIEws對象給widget.
final int N = appWidgetIds.length;
for (int i=0; i<N; i++) {
int appWidgetId = appWidgetIds[i];
String titlePrefix = ExampleAppWidgetConfigure.loadTitlePref(context, appWidgetId);
updateAppWidget(context, appWidgetManager, appWidgetId, titlePrefix);
}
}
public void onDeleted(Context context, int[] appWidgetIds) {
Log.d(TAG, "onDeleted");
// 當用戶刪除Widget時觸發該
final int N = appWidgetIds.length;
for (int i=0; i<N; i++) {
ExampleAppWidgetConfigure.deleteTitlePref(context, appWidgetIds[i]);
}
}
public void onEnabled(Context context) {
Log.d(TAG, "onEnabled");
// 當widget創建時注冊TIMEZONE_CHANGED 和 TIME_CHANGED改變的廣播獲取這些時間和區域的改變事件
PackageManager pm = context.getPackageManager();
pm.setComponentEnabledSetting(
new ComponentName("com.example.android.apis", ".appwidget.ExampleBroadcastReceiver"),
PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
PackageManager.DONT_KILL_APP);
}
public void onDisabled(Context context) {
Log.d(TAG, "onDisabled");
Class clazz = ExampleBroadcastReceiver.class;
PackageManager pm = context.getPackageManager();
pm.setComponentEnabledSetting(
new ComponentName("com.example.android.apis", ".appwidget.ExampleBroadcastReceiver"),
PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
PackageManager.DONT_KILL_APP);
}
static void updateAppWidget(Context context, AppWidgetManager appWidgetManager,
int appWidgetId, String titlePrefix) {
Log.d(TAG, "updateAppWidget appWidgetId=" + appWidgetId + " titlePrefix=" + titlePrefix);
CharSequence text = context.getString(R.string.appwidget_text_format,
ExampleAppWidgetConfigure.loadTitlePref(context, appWidgetId),
"0x" + Long.toHexString(SystemClock.elapsedRealtime()));
RemoteViews views = new RemoteVIEws(context.getPackageName(), R.layout.appwidget_provider);
views.setTextVIEwText(R.id.appwidget_text, text);
appWidgetManager.updateAppWidget(appWidgetId, vIEws);
}
}
package com.example.android.apis.appwidget;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.os.SystemClock;
import android.util.Log;
import android.widget.RemoteVIEws;
import Java.util.ArrayList;
import com.example.android.apis.R;
public class ExampleBroadcastReceiver extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) {
Log.d("ExmampleBroadcastReceiver", "intent=" + intent);
// For our example, we'll also update all of the widgets when the timezone
// changes, or the user or network sets the time.
String action = intent.getAction();
if (action.equals(Intent.ACTION_TIMEZONE_CHANGED)
|| action.equals(Intent.ACTION_TIME_CHANGED)) {
AppWidgetManager gm = AppWidgetManager.getInstance(context);
ArrayList<Integer> appWidgetIds = new ArrayList();
ArrayList<String> texts = new ArrayList();
ExampleAppWidgetConfigure.loadAllTitlePrefs(context, appWidgetIds, texts);
final int N = appWidgetIds.size();
for (int i=0; i<N; i++) {
ExampleAppWidgetProvider.updateAppWidget(context, gm, appWidgetIds.get(i), texts.get(i));
}
}
}
}
package com.example.android.apis.appwidget;
import android.app.Activity;
import android.appwidget.AppWidgetManager;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.VIEw;
import android.widget.EditText;
import Java.util.ArrayList;
import com.example.android.apis.R;
public class ExampleAppWidgetConfigure extends Activity {
static final String TAG = "ExampleAppWidgetConfigure";
private static final String PREFS_NAME
= "com.example.android.apis.appwidget.ExampleAppWidgetProvider";
private static final String PREF_PREFIX_KEY = "prefix_";
int mAppWidgetId = AppWidgetManager.INVALID_APPWIDGET_ID;
EditText mAppWidgetPrefix;
public ExampleAppWidgetConfigure() {
super();
}
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setResult(RESULT_CANCELED);
setContentVIEw(R.layout.appwidget_configure);
mAppWidgetPrefix = (EditText)findVIEwById(R.id.appwidget_prefix);
findVIEwById(R.id.save_button).setOnClickListener(mOnClickListener);
Intent intent = getIntent();
Bundle extras = intent.getExtras();
if (extras != null) {
mAppWidgetId = extras.getInt(
AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID);
}
if (mAppWidgetId == AppWidgetManager.INVALID_APPWIDGET_ID) {
finish();
}
mAppWidgetPrefix.setText(loadTitlePref(ExampleAppWidgetConfigure.this, mAppWidgetId));
}
View.OnClickListener mOnClickListener = new VIEw.OnClickListener() {
public void onClick(VIEw v) {
final Context context = ExampleAppWidgetConfigure.this;
String titlePrefix = mAppWidgetPrefix.getText().toString();
saveTitlePref(context, mAppWidgetId, titlePrefix);
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
ExampleAppWidgetProvider.updateAppWidget(context, appWidgetManager,
mAppWidgetId, titlePrefix);
Intent resultValue = new Intent();
resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mAppWidgetId);
setResult(RESULT_OK, resultValue);
finish();
}
};
static void saveTitlePref(Context context, int appWidgetId, String text) {
SharedPreferences.Editor prefs = context.getSharedPreferences(PREFS_NAME, 0).edit();
prefs.putString(PREF_PREFIX_KEY + appWidgetId, text);
prefs.commit();
}
static String loadTitlePref(Context context, int appWidgetId) {
SharedPreferences prefs = context.getSharedPreferences(PREFS_NAME, 0);
String prefix = prefs.getString(PREF_PREFIX_KEY + appWidgetId, null);
if (prefix != null) {
return prefix;
} else {
return context.getString(R.string.appwidget_prefix_default);
}
}
static void deleteTitlePref(Context context, int appWidgetId) {
}
static void loadAllTitlePrefs(Context context, ArrayList<Integer> appWidgetIds,
ArrayList<String> texts) {
}
}
Google的android 2.1之前曾經歷過跳票的痛苦,但現在的發布正好迎合了Nexus One的發展。看來谷歌繼續對抗蘋果的iPhone的戰爭,才剛剛開始。And
2011 年 2 月,Danger 時代結束。T Mobile 的新 CEO Philipp Humm 實施新計劃,確認了向現有的 Sidekick 用戶關閉 Dang
Google的開源android移動操作系統正在席卷全球智能手機市場,和蘋果不一樣,它對那些想將應用程序提交到iPhone App Store的開發人員有著嚴格的指導方
Google已經發布了android SDK 2.3版本,大家應該都在等android 2.3的升級吧?在51CTO獨家公布了android SDK 2.3下載地址後.