<p><strong>一、Android中的通知 </strong></p>
<p>一般手机上边都有一个状态条,显示电池电量、信号强度、未接来电、短信...。Android的屏幕上方也具有状态条。这里所说的通知,就是在这个状态条上显示通知。</p>
<p>发送通知的步骤如下:</p>
<p>1).获取通知管理器</p>
<p>NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);</p>
<p>2).新建一个通知,指定其图标和标题</p>
<p>int icon = android.R.drawable.stat_notify_chat;</p>
<p>long when = System.currentTimeMillis();</p>
<p>//第一个参数为图标,第二个参数为标题,第三个为通知时间</p>
<p>Notification notification = new Notification(icon, null, when);</p>
<p>Intent openintent = new Intent(this, OtherActivity.class);</p>
<p>//当点击消息时就会向系统发送openintent意图</p>
<p>PendingIntent contentIntent = PendingIntent.getActivity(this, 0, openintent, 0);</p>
<p>notification.setLatestEventInfo(this, “标题”, “内容", contentIntent);</p>
<p>mNotificationManager.notify(0, notification);</p>
<p><strong>二、Android中的样式和主题</strong></p>
<p>android中的样式和CSS样式作用相似,都是用于为界面元素定义显示风格,它是一个包含一个或者多个view控件属性的集合。如:需要定义字体的颜色和大小。</p>
<p>1).在values目录下添加styles.xml:</p>
<p><!--l version=<-->"1.0" encoding=<em>"utf-8"</em>?></p>
<p></p>
<p><!--
"changcheng"</em>></div>
<div mce_tmp="1"><item name=<em>"android:textSize"</em>>18px</item></div>
<div mce_tmp="1"><item name=<em>"android:textColor"</em>>#0000CC</item></div>
<div mce_tmp="1">
--></p>
<p></p>
<p>2).在layout文件中可以通过style或 theme属性设置样式或主题。</p>
<p><strong>三、使用HTML做为UI</strong></p>
<p>使用LayoutUI比较麻烦,不能让美工参与进来,这样就为开发人员带来了麻烦。但我们可以通过HTML+JS来进行UI的设计与操作。</p>
<p>1).在assets添加Html页面</p>
<p><!--CTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dt--></p>
<p></p>
<p></p>
<p></p>
<p></p>
<p></p>
<p></p>
<p></p>
<p></p>
<p>
</p>
<table id="personTable" style="width: 100%;" border="0" cellspacing="0">
<p></p>
<tbody>
<tr>
<p></p>
<td width="15%">编号</td>
<td align="center">姓名</td>
<td width="15%">电话</td>
<p></p>
</tr>
<p></p>
</tbody>
</table>
<p></p>
<p></p>
<p>2).在main.xlm中添加一个WebView控件</p>
<p><!--l version=<-->"1.0" encoding=<em>"utf-8"</em>?></p>
<p>"http://schemas.android.com/apk/res/android"</p>
<p>android:orientation=<em>"vertical"</em></p>
<p>android:layout_width=<em>"fill_parent"</em></p>
<p>android:layout_height=<em>"fill_parent"</em></p>
<p>></p>
<p>
</p>
<p>android:layout_width=<em>"fill_parent"</em></p>
<p>android:layout_height=<em>"fill_parent"</em></p>
<p>android:id=<em>"@+id/webView"</em></p>
<p></p>
<p></p>
<p>3).Activity类</p>
<p><strong>package</strong> cn.itcast.html;</p>
<p><strong>import</strong> java.util.ArrayList;</p>
<p><strong>import</strong> java.util.List;</p>
<p><strong>import</strong> org.json.JSONArray;</p>
<p><strong>import</strong> org.json.JSONObject;</p>
<p><strong>import</strong> cn.itcast.domain.Contact;</p>
<p><strong>import</strong> android.app.Activity;</p>
<p><strong>import</strong> android.content.Intent;</p>
<p><strong>import</strong> android.net.Uri;</p>
<p><strong>import</strong> android.os.Bundle;</p>
<p><strong>import</strong> android.os.Handler;</p>
<p><strong>import</strong> android.util.Log;</p>
<p><strong>import</strong> android.webkit.WebView;</p>
<p><strong>public</strong> <strong>class</strong> ContactActivity <strong>extends</strong> Activity {</p>
<p><strong>private</strong> <strong>static</strong> <strong>final</strong> String <em>TAG</em> = "ContactActivity";</p>
<p><strong>private</strong> WebView webView;</p>
<p><strong>private</strong> Handler handler = <strong>new</strong> Handler();</p>
<p>@Override</p>
<p><strong>public</strong> <strong>void</strong> onCreate(Bundle savedInstanceState) {</p>
<p><strong>super</strong>.onCreate(savedInstanceState);</p>
<p>setContentView(R.layout.<em>main</em>);</p>
<p>webView = (WebView)<strong>this</strong>.findViewById(R.id.<em>webView</em>);</p>
<p>webView.getSettings().setJavaScriptEnabled(<strong>true</strong>);//设置支持javaScript</p>
<p>webView.getSettings().setSaveFormData(<strong>false</strong>);//不保存表单数据</p>
<p>webView.getSettings().setSavePassword(<strong>false</strong>);//不保存密码</p>
<p>webView.getSettings().setSupportZoom(<strong>false</strong>);//不支持页面放大功能</p>
<p>//addJavascriptInterface方法中要绑定的Java对象及方法要运行在另外的线程中,不能运行在构造他的线程中</p>
<p>webView.addJavascriptInterface(<strong>new</strong> MyJavaScript(), "itcast");</p>
<p>webView.loadUrl("file:///android_asset/index.html");</p>
<p>}</p>
<p><strong>private</strong> <strong>final</strong> <strong>class</strong> MyJavaScript{</p>
<p><strong>public</strong> <strong>void</strong> call(<strong>final</strong> String phone){</p>
<p>handler.post(<strong>new</strong> Runnable() {</p>
<p>@Override</p>
<p><strong>public</strong> <strong>void</strong> run() {</p>
<p>Intent intent = <strong>new</strong> Intent(Intent.<em>ACTION_CALL</em>, Uri.<em>parse</em>("tel:"+ phone));</p>
<p>startActivity(intent);</p>
<p>}</p>
<p>});</p>
<p>}</p>
<p>/**</p>
<p>* 获取所有联系人</p>
<p>*/</p>
<p><strong>public</strong> <strong>void</strong> getContacts(){</p>
<p>handler.post(<strong>new</strong> Runnable() {</p>
<p>@Override</p>
<p><strong>public</strong> <strong>void</strong> run() {</p>
<p>//可以通过访问SQLLite数据库得到联系人</p>
<p>List contacts = <strong>new</strong> ArrayList();</p>
<p>contacts.add(<strong>new</strong> Contact(27, "路飞", "12345"));</p>
<p>contacts.add(<strong>new</strong> Contact(28, "索隆", "67890"));</p>
<p>String json = buildJson(contacts);</p>
<p>webView.loadUrl("javascript:show('"+ json +"')");</p>
<p>}</p>
<p>});</p>
<p>}</p>
<p>//生成<span style="text-decoration: underline;">Json</span>格式的数据</p>
<p><strong>private</strong> String buildJson(List contacts){</p>
<p><strong>try</strong> {</p>
<p>JSONArray array = <strong>new</strong> JSONArray();</p>
<p><strong>for</strong>(Contact contact : contacts){</p>
<p>JSONObject item = <strong>new</strong> JSONObject();</p>
<p>item.put("id", contact.getId());</p>
<p>item.put("name", contact.getName());</p>
<p>item.put("phone", contact.getPhone());</p>
<p>array.put(item);</p>
<p>}</p>
<p><strong>return</strong> array.toString();</p>
<p>} <strong>catch</strong> (Exception e) {</p>
<p>Log.<em>e</em>(<em>TAG</em>, e.toString());</p>
<p>}</p>
<p><strong>return</strong> "";</p>
<p>}</p>
<p>}</p>
<p>}</p>
<p>MyJavaScript接口实现的方法正是提供给页面中的JS代码调用的!</p>
<p><strong>四、打包和安装Android应用</strong></p>
<p><strong>1.</strong><strong>导出Android应用</strong></p>
<p>在工程上右解-->Export-->Android-->Export Android Application,将工程导出为APK包。</p>
<p><strong>2.</strong><strong>将APK包放入到SDCard目录中</strong></p>
<p>在FileExplorer面板的右上角有一个导入手机图标,将上面生成的APK包导入到SDCard目录中。</p>
<p><strong>3.</strong><strong>编写安装APK包的Android程序</strong></p>
<p>1).在AndoirdManifest.xml添加权限:</p>
<p><!-- 安装程序权限 --></p>
<p>"android.permission.INSTALL_PACKAGES"/></p>
<p>2).通过Android提供的功能,安装APK:</p>
<p>Intent intent = new Intent();</p>
<p>intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);</p>
<p>intent.setAction(android.content.Intent.ACTION_VIEW);</p>
<p>Uri data = Uri.fromFile(new File(Environment.getExternalStorageDirectory(), filename));</p>
<p>intent.setDataAndType(data, "application/vnd.android.package-archive");</p>
<p>startActivity(intent);</p>
<p>Android的学习到此结束!</p>
<p>一般手机上边都有一个状态条,显示电池电量、信号强度、未接来电、短信...。Android的屏幕上方也具有状态条。这里所说的通知,就是在这个状态条上显示通知。</p>
<p>发送通知的步骤如下:</p>
<p>1).获取通知管理器</p>
<p>NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);</p>
<p>2).新建一个通知,指定其图标和标题</p>
<p>int icon = android.R.drawable.stat_notify_chat;</p>
<p>long when = System.currentTimeMillis();</p>
<p>//第一个参数为图标,第二个参数为标题,第三个为通知时间</p>
<p>Notification notification = new Notification(icon, null, when);</p>
<p>Intent openintent = new Intent(this, OtherActivity.class);</p>
<p>//当点击消息时就会向系统发送openintent意图</p>
<p>PendingIntent contentIntent = PendingIntent.getActivity(this, 0, openintent, 0);</p>
<p>notification.setLatestEventInfo(this, “标题”, “内容", contentIntent);</p>
<p>mNotificationManager.notify(0, notification);</p>
<p><strong>二、Android中的样式和主题</strong></p>
<p>android中的样式和CSS样式作用相似,都是用于为界面元素定义显示风格,它是一个包含一个或者多个view控件属性的集合。如:需要定义字体的颜色和大小。</p>
<p>1).在values目录下添加styles.xml:</p>
<p><!--l version=<-->"1.0" encoding=<em>"utf-8"</em>?></p>
<p></p>
<p><!--
"changcheng"</em>></div>
<div mce_tmp="1"><item name=<em>"android:textSize"</em>>18px</item></div>
<div mce_tmp="1"><item name=<em>"android:textColor"</em>>#0000CC</item></div>
<div mce_tmp="1">
--></p>
<p></p>
<p>2).在layout文件中可以通过style或 theme属性设置样式或主题。</p>
<p><strong>三、使用HTML做为UI</strong></p>
<p>使用LayoutUI比较麻烦,不能让美工参与进来,这样就为开发人员带来了麻烦。但我们可以通过HTML+JS来进行UI的设计与操作。</p>
<p>1).在assets添加Html页面</p>
<p><!--CTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dt--></p>
<p></p>
<p></p>
<p></p>
<p></p>
<p></p>
<p></p>
<p></p>
<p></p>
<p>
</p>
<table id="personTable" style="width: 100%;" border="0" cellspacing="0">
<p></p>
<tbody>
<tr>
<p></p>
<td width="15%">编号</td>
<td align="center">姓名</td>
<td width="15%">电话</td>
<p></p>
</tr>
<p></p>
</tbody>
</table>
<p></p>
<p></p>
<p>2).在main.xlm中添加一个WebView控件</p>
<p><!--l version=<-->"1.0" encoding=<em>"utf-8"</em>?></p>
<p>"http://schemas.android.com/apk/res/android"</p>
<p>android:orientation=<em>"vertical"</em></p>
<p>android:layout_width=<em>"fill_parent"</em></p>
<p>android:layout_height=<em>"fill_parent"</em></p>
<p>></p>
<p>
</p>
<p>android:layout_width=<em>"fill_parent"</em></p>
<p>android:layout_height=<em>"fill_parent"</em></p>
<p>android:id=<em>"@+id/webView"</em></p>
<p></p>
<p></p>
<p>3).Activity类</p>
<p><strong>package</strong> cn.itcast.html;</p>
<p><strong>import</strong> java.util.ArrayList;</p>
<p><strong>import</strong> java.util.List;</p>
<p><strong>import</strong> org.json.JSONArray;</p>
<p><strong>import</strong> org.json.JSONObject;</p>
<p><strong>import</strong> cn.itcast.domain.Contact;</p>
<p><strong>import</strong> android.app.Activity;</p>
<p><strong>import</strong> android.content.Intent;</p>
<p><strong>import</strong> android.net.Uri;</p>
<p><strong>import</strong> android.os.Bundle;</p>
<p><strong>import</strong> android.os.Handler;</p>
<p><strong>import</strong> android.util.Log;</p>
<p><strong>import</strong> android.webkit.WebView;</p>
<p><strong>public</strong> <strong>class</strong> ContactActivity <strong>extends</strong> Activity {</p>
<p><strong>private</strong> <strong>static</strong> <strong>final</strong> String <em>TAG</em> = "ContactActivity";</p>
<p><strong>private</strong> WebView webView;</p>
<p><strong>private</strong> Handler handler = <strong>new</strong> Handler();</p>
<p>@Override</p>
<p><strong>public</strong> <strong>void</strong> onCreate(Bundle savedInstanceState) {</p>
<p><strong>super</strong>.onCreate(savedInstanceState);</p>
<p>setContentView(R.layout.<em>main</em>);</p>
<p>webView = (WebView)<strong>this</strong>.findViewById(R.id.<em>webView</em>);</p>
<p>webView.getSettings().setJavaScriptEnabled(<strong>true</strong>);//设置支持javaScript</p>
<p>webView.getSettings().setSaveFormData(<strong>false</strong>);//不保存表单数据</p>
<p>webView.getSettings().setSavePassword(<strong>false</strong>);//不保存密码</p>
<p>webView.getSettings().setSupportZoom(<strong>false</strong>);//不支持页面放大功能</p>
<p>//addJavascriptInterface方法中要绑定的Java对象及方法要运行在另外的线程中,不能运行在构造他的线程中</p>
<p>webView.addJavascriptInterface(<strong>new</strong> MyJavaScript(), "itcast");</p>
<p>webView.loadUrl("file:///android_asset/index.html");</p>
<p>}</p>
<p><strong>private</strong> <strong>final</strong> <strong>class</strong> MyJavaScript{</p>
<p><strong>public</strong> <strong>void</strong> call(<strong>final</strong> String phone){</p>
<p>handler.post(<strong>new</strong> Runnable() {</p>
<p>@Override</p>
<p><strong>public</strong> <strong>void</strong> run() {</p>
<p>Intent intent = <strong>new</strong> Intent(Intent.<em>ACTION_CALL</em>, Uri.<em>parse</em>("tel:"+ phone));</p>
<p>startActivity(intent);</p>
<p>}</p>
<p>});</p>
<p>}</p>
<p>/**</p>
<p>* 获取所有联系人</p>
<p>*/</p>
<p><strong>public</strong> <strong>void</strong> getContacts(){</p>
<p>handler.post(<strong>new</strong> Runnable() {</p>
<p>@Override</p>
<p><strong>public</strong> <strong>void</strong> run() {</p>
<p>//可以通过访问SQLLite数据库得到联系人</p>
<p>List contacts = <strong>new</strong> ArrayList();</p>
<p>contacts.add(<strong>new</strong> Contact(27, "路飞", "12345"));</p>
<p>contacts.add(<strong>new</strong> Contact(28, "索隆", "67890"));</p>
<p>String json = buildJson(contacts);</p>
<p>webView.loadUrl("javascript:show('"+ json +"')");</p>
<p>}</p>
<p>});</p>
<p>}</p>
<p>//生成<span style="text-decoration: underline;">Json</span>格式的数据</p>
<p><strong>private</strong> String buildJson(List contacts){</p>
<p><strong>try</strong> {</p>
<p>JSONArray array = <strong>new</strong> JSONArray();</p>
<p><strong>for</strong>(Contact contact : contacts){</p>
<p>JSONObject item = <strong>new</strong> JSONObject();</p>
<p>item.put("id", contact.getId());</p>
<p>item.put("name", contact.getName());</p>
<p>item.put("phone", contact.getPhone());</p>
<p>array.put(item);</p>
<p>}</p>
<p><strong>return</strong> array.toString();</p>
<p>} <strong>catch</strong> (Exception e) {</p>
<p>Log.<em>e</em>(<em>TAG</em>, e.toString());</p>
<p>}</p>
<p><strong>return</strong> "";</p>
<p>}</p>
<p>}</p>
<p>}</p>
<p>MyJavaScript接口实现的方法正是提供给页面中的JS代码调用的!</p>
<p><strong>四、打包和安装Android应用</strong></p>
<p><strong>1.</strong><strong>导出Android应用</strong></p>
<p>在工程上右解-->Export-->Android-->Export Android Application,将工程导出为APK包。</p>
<p><strong>2.</strong><strong>将APK包放入到SDCard目录中</strong></p>
<p>在FileExplorer面板的右上角有一个导入手机图标,将上面生成的APK包导入到SDCard目录中。</p>
<p><strong>3.</strong><strong>编写安装APK包的Android程序</strong></p>
<p>1).在AndoirdManifest.xml添加权限:</p>
<p><!-- 安装程序权限 --></p>
<p>"android.permission.INSTALL_PACKAGES"/></p>
<p>2).通过Android提供的功能,安装APK:</p>
<p>Intent intent = new Intent();</p>
<p>intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);</p>
<p>intent.setAction(android.content.Intent.ACTION_VIEW);</p>
<p>Uri data = Uri.fromFile(new File(Environment.getExternalStorageDirectory(), filename));</p>
<p>intent.setDataAndType(data, "application/vnd.android.package-archive");</p>
<p>startActivity(intent);</p>
<p>Android的学习到此结束!</p>