Android Widget实例代码解析

移动开发
Android Widget实例代码解析是本文要介绍的内容,主要是来了解并学习Android Widget实例的应用,来看内容详解。

Android Widget实例代码解析是本文要介绍的内容,主要是来了解并学习Android Widget实例的应用,通过代码来实现内容的讲解,具体内容的实现来看本文详解。

下面的代码分为3个文件可以清楚的看到整个框架,主要是AppWidgetProvider类中的onUpdate、onDeleted、onEnabled、onDisabled和updateAppWidget方法之间的状态改变,使用Logcat仔细分析一个widget的生命周期。

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 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 appWidgetIds = new ArrayList();  
ArrayList texts = new ArrayList();    
ExampleAppWidgetConfigure.loadAllTitlePrefs(context, appWidgetIds, texts);    
final int N = appWidgetIds.size();  
for (int i=0; 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 appWidgetIds,  
ArrayList texts) {  
}  
}  
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83.
  • 84.
  • 85.
  • 86.
  • 87.
  • 88.
  • 89.
  • 90.
  • 91.
  • 92.
  • 93.
  • 94.
  • 95.
  • 96.
  • 97.
  • 98.
  • 99.
  • 100.
  • 101.
  • 102.
  • 103.
  • 104.
  • 105.
  • 106.
  • 107.
  • 108.
  • 109.
  • 110.
  • 111.
  • 112.
  • 113.
  • 114.
  • 115.
  • 116.
  • 117.
  • 118.
  • 119.
  • 120.
  • 121.
  • 122.
  • 123.
  • 124.
  • 125.
  • 126.
  • 127.
  • 128.
  • 129.
  • 130.
  • 131.
  • 132.
  • 133.
  • 134.
  • 135.
  • 136.
  • 137.
  • 138.
  • 139.
  • 140.
  • 141.
  • 142.
  • 143.
  • 144.
  • 145.
  • 146.
  • 147.
  • 148.
  • 149.
  • 150.
  • 151.
  • 152.
  • 153.
  • 154.
  • 155.
  • 156.
  • 157.
  • 158.
  • 159.
  • 160.
  • 161.
  • 162.
  • 163.
  • 164.
  • 165.
  • 166.
  • 167.
  • 168.
  • 169.
  • 170.
  • 171.
  • 172.

小结:Android Widget实例代码解析的内容介绍完了,希望通过Android Widget实例内容的学习能对你有所帮助。

责任编辑:zhaolei 来源: 互联网
相关推荐

2011-09-07 17:54:40

Android Wid开发

2011-09-09 13:23:17

Widget

2011-09-09 10:00:20

Android Wid开发

2011-02-28 13:04:27

RelativeLayAndroid Wid

2011-09-07 13:42:36

Android Wid实例

2011-09-08 13:11:07

Android Wid实例

2011-09-07 14:39:47

Android Wid设计

2011-09-08 15:40:45

Android Wid组件

2011-09-07 14:25:53

Android Wid设计

2011-09-07 13:30:48

Android WidTabWidget

2011-09-08 14:01:01

Android Wid实例

2011-09-08 13:36:26

Android Wid播放器

2011-09-09 17:59:26

QT Widget

2011-09-07 14:34:55

Android Wid控件

2011-09-08 14:21:37

jQueryWidget

2011-09-09 19:23:52

Widget

2011-09-08 16:17:45

Widget

2010-07-13 09:08:27

Widget开发

2013-02-20 15:29:00

JSONAndroid开发

2011-09-07 14:20:42

Android Wid组件
点赞
收藏

51CTO技术栈公众号