1、 ,使用cwjManager.getActiveNetworkInfo().isAvailable(); 来返回是否有效,如果为True则表示当前Android手机已经联网,可能是WiFi或GPRS、HSDPA等等,具体的可以通过ConnectivityManager 类的getActiveNetworkInfo() 方法判断详细的接入方式,需要注意的是有关调用需要加入 这个权限,android开发网提醒大家在真机上Market和Browser程序都使用了这个方法,来判断是否继续,同时在一些网络超时的时候也可以检查下网络连接是否存在,以免浪费手机上的电力资源。5. Drawable、Bitma
2、p、Canvas和Paint的关系 很多网友刚刚开始学习Android平台,对于Drawable、Bitmap、Canvas和Paint它们之间的概念不是很清楚,其实它们除了Drawable外早在Sun的J2ME中就已经出现了,但是在Android平台中,Bitmap、Canvas相关的都有所变化。 首先让我们理解下Android平台中的显示类是View,但是还提供了底层图形类android.graphics,今天所说的这些均为graphics底层图形接口。Bitmap - 称作位图,一般位图的文件格式后缀为bmp,当然编码器也有很多如RGB565、RGB888。作为一种逐像素的显示对象执行效
3、率高,但是缺点也很明显存储效率低。我们理解为一种存储对象比较好。Drawable - 作为Android平下通用的图形对象,它可以装载常用格式的图像,比如GIF、PNG、JPG,当然也支持BMP,当然还提供一些高级的可视化对象,比如渐变、图形等。Canvas - 名为画布,我们可以看作是一种处理过程,使用各种方法来管理Bitmap、GL或者Path路径,同时它可以配合Matrix矩阵类给图像做旋转、缩放等操作,同时Canvas类还提供了裁剪、选取等操作。 Paint - 我们可以把它看做一个画图工具,比如画笔、画刷。他管理了每个画图工具的字体、颜色、样式。如果涉及一些Android游戏开发、显
4、示特效可以通过这些底层图形类来高效实现自己的应用。6. Activity切换导致的onCreate重复执行 部分网友会发现Activity在切换到后台或布局从横屏LANDSCAPE切换到PORTRAIT,会重新切换Activity会触发一次onCreate方法,我们可以在androidmanifest.xml中的activit元素加入这个属性android:configChanges=orientation|keyboardHidden 即可,比如 activity android:.android123 android:label=string/app_name同时在Activity的Jav
5、a文件中重载onConfigurationChanged(Configuration newConfig)这个方法,这样就不会在布局切换或窗口切换时重载onCreate等方法。代码如下: Override public void onConfigurationChanged(Configuration newConfig) super.onConfigurationChanged(newConfig);if (this.getResources().getConfiguration().orientation = Configuration.ORIENTATION_LANDSCAPE)/lan
6、delse if (this.getResources().getConfiguration().orientation = Configuration.ORIENTATION_PORTRAIT)/port 7. Android的ImageButton问题 很多网友对Android提供的ImageButton有个疑问,当显示Drawable图片时就不会再显示文字了,其实解决的方法有两种,第一种就是图片中就写入文字,但是这样解决会增加程序体积,同时硬编码方式会影响多国语言的发布。第二种解决方法很简单,通过分析可以看到ImageButton的layout,我们可以直接直接继承,添加一个TextVi
7、ew,对齐方式为右侧即可实现ImageButton支持文字右侧显示。8. Android代码优化技术 1.Java内存控制对于字符串操作而言如果需要连加这样的操作建议使用StringBuilder,经过调试不难发现如果你的字符串每次连加,使用String需要的内存开销会远大于StringBuilder,然后Android手机常规的运行内存大约在128MB左右,对于运行多任务就需要考虑了,Android开发网提示因为Java有GC不需要手动释放那么分配的时候就要格外的小心,频繁的GC操作仍然是很影响性能的,在调试时我们可以通过logcat查看内存释放情况。2.循环使用平时在访问一个属性的时候效率
8、远比一个固定变量低,如果你的循环估计次数常常大于5,假设xxx.GetLength()方法的值一般大于5,推荐这样写,比如for(int i=0;istyle name=cwjTheme parent=android:Theme.Lightitem name=windowNoTitletruewindowBackgroundandroid:color/transparentlistViewStylestyle/cwjListView有关ListView控件我们自定义的风格就是修改下系统listview这个控件的每行分隔符样式,这里我们在工程下res/drawable文件夹下放一个图片名为lis
9、t_selector图片,这样我们的cwjListView的代码可以这样写cwjListViewstyle/Widget.ListViewlistSelectordrawable/list_selector通过定义style可以设置更多,比如让cwjListView的字体颜色就加入textAppearance属性,比如 textAppearancestyle/TextAppearance 等等。11.Android JSON解析示例代码 来自Google官方的有关Android平台的JSON解析示例,如果远程服务器使用了json而不是xml的数据提供,在Android平台上已经内置的org.j
10、son包可以很方便的实现手机客户端的解析处理。下面Android123一起分析下这个例子,帮助Android开发者需要有关 HTTP通讯、正则表达式、JSON解析、appWidget开发的一些知识。 public class WordWidget extends AppWidgetProvider /appWidget public void onUpdate(Context context, AppWidgetManager appWidgetManager,int appWidgetIds) context.startService(new Intent(context, UpdateSe
11、rvice.class); /避免ANR,所以Widget中开了个服务 public static class UpdateService extends Service Overridepublic void onStart(Intent intent, int startId) / Build the widget update for todayRemoteViews updateViews = buildUpdate(this); ComponentName thisWidget = new ComponentName(this, WordWidget.class);AppWidget
12、Manager manager = AppWidgetManager.getInstance(this);manager.updateAppWidget(thisWidget, updateViews);public RemoteViews buildUpdate(Context context) / Pick out month names from resourcesResources res = context.getResources();String monthNames = res.getStringArray(R.array.month_names);Time today = n
13、ew Time();today.setToNow(); String pageName = res.getString(R.string.template_wotd_title,monthNamestoday.month, today.monthDay);RemoteViews updateViews = null;String pageContent = ; try SimpleWikiHelper.prepareUserAgent(context); pageContent = SimpleWikiHelper.getPageContent(pageName, false); catch
14、(ApiException e) Log.e(WordWidget, Couldnt contact API, e); catch (ParseException e) t parse API response Pattern pattern = Ppile(SimpleWikiHelper.WORD_OF_DAY_REGEX); /正则表达式处理,有关定义见下面的SimpleWikiHelper类Matcher matcher = pattern.matcher(pageContent);if (matcher.find() updateViews = new RemoteViews(con
15、text.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.getStr
16、ing(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 */);
17、 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;public IBinder onBind(Intent intent) / We dont need to bind to this servicereturn null;有关网络通讯的实体类,以及一些常量定义如下:public class SimpleWikiHelper private static final String TAG = SimpleWikiHelperpublic static final String WORD_OF_DAY_REGEX =(?s)wotd|(.+?)|(.+?)|(#|+).*?private static f
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1