一、Android中的通知
一般手機上邊都有一個狀態條,顯示電池電量、信號強度、未接來電、短信...。Android的屏幕上方也具有狀態條。這里所說的通知,就是在這個狀態條上顯示通知。
發送通知的步驟如下:
1).獲取通知管理器
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
2).新建一個通知,指定其圖標和標題
int icon = android.R.drawable.stat_notify_chat;
long when = System.currentTimeMillis();
//第一個參數為圖標,第二個參數為標題,第三個為通知時間
Notification notification = new Notification(icon, null, when);
Intent openintent = new Intent(this, OtherActivity.class);
//當點擊消息時就會向系統發送openintent意圖
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, openintent, 0);
notification.setLatestEventInfo(this, “標題”, “內容", contentIntent);
mNotificationManager.notify(0, notification);
二、Android中的樣式和主題
android中的樣式和CSS樣式作用相似,都是用于為界面元素定義顯示風格,它是一個包含一個或者多個view控件屬性的集合。如:需要定義字體的顏色和大小。
1).在values目錄下添加styles.xml:
<?xml version="1.0" encoding="utf-8"?> <resources> <style name="changcheng"> <item name="android:textSize">18px</item> <item name="android:textColor">#0000CC</item> </style> </resources> |
2).在layout文件中可以通過style或 theme屬性設置樣式或主題。
三、使用HTML做為UI
使用LayoutUI比較麻煩,不能讓美工參與進來,這樣就為開發人員帶來了麻煩。但我們可以通過HTML+JS來進行UI的設計與操作。
1).在assets添加Html頁面
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>聯系人列表</title> <script type="text/javascript"> function show(jsondata){ var jsonobjs = eval(jsondata); var table = document.getElementById("personTable"); for(var y=0; y<jsonobjs.length; y++){ var tr = table.insertRow(table.rows.length); //添加一行 //添加三列 var td1 = tr.insertCell(0); var td2 = tr.insertCell(1); td2.align = "center"; var td3 = tr.insertCell(2); //設置列內容和屬性 td1.innerHTML = jsonobjs[y].id; td2.innerHTML = jsonobjs[y].name; td3.innerHTML = "<a href='javascript:itcast.call(\""+ jsonobjs[y].phone +"\")'>"+ jsonobjs[y].phone+ "</a>"; } } </script> </head> <body> <body bgcolor="#000000" text="#FFFFFF" style="margin:0 0 0 0" onload="javascript:itcast.getContacts()"> <table border="0" width="100%" id="personTable" cellspacing="0"> <tr> <td width="15%">編號</td><td align="center">姓名</td><td width="15%">電話</td> </tr> </table>
</body> </html> |
2).在main.xlm中添加一個WebView控件
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <WebView android:layout_width="fill_parent" android:layout_height="fill_parent" android:id="@+id/webView" /> </LinearLayout> |
3).Activity類
package cn.itcast.html;
import java.util.ArrayList; import java.util.List; import org.json.JSONArray; import org.json.JSONObject; import cn.itcast.domain.Contact; import android.app.Activity; import android.content.Intent; import android.net.Uri; import android.os.Bundle; import android.os.Handler; import android.util.Log; import android.webkit.WebView;
public class ContactActivity extends Activity { private static final String TAG = "ContactActivity"; private WebView webView; private Handler handler = new Handler();
@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); webView = (WebView)this.findViewById(R.id.webView); webView.getSettings().setJavaScriptEnabled(true);//設置支持javaScript webView.getSettings().setSaveFormData(false);//不保存表單數據 webView.getSettings().setSavePassword(false);//不保存密碼 webView.getSettings().setSupportZoom(false);//不支持頁面放大功能 //addJavascriptInterface方法中要綁定的Java對象及方法要運行在另外的線程中,不能運行在構造他的線程中 webView.addJavascriptInterface(new MyJavaScript(), "itcast"); webView.loadUrl("file:///android_asset/index.html"); } private final class MyJavaScript{ public void call(final String phone){ handler.post(new Runnable() { @Override public void run() { Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:"+ phone)); startActivity(intent); } }); } /** * 獲取所有聯系人 */ public void getContacts(){ handler.post(new Runnable() { @Override public void run() { //可以通過訪問SQLLite數據庫得到聯系人 List<Contact> contacts = new ArrayList<Contact>(); contacts.add(new Contact(27, "路飛", "12345")); contacts.add(new Contact(28, "索隆", "67890")); String json = buildJson(contacts); webView.loadUrl("javascript:show('"+ json +"')"); } }); } //生成Json格式的數據 private String buildJson(List<Contact> contacts){ try { JSONArray array = new JSONArray(); for(Contact contact : contacts){ JSONObject item = new JSONObject(); item.put("id", contact.getId()); item.put("name", contact.getName()); item.put("phone", contact.getPhone()); array.put(item); } return array.toString(); } catch (Exception e) { Log.e(TAG, e.toString()); } return ""; } } } |
MyJavaScript接口實現的方法正是提供給頁面中的JS代碼調用的!
四、打包和安裝Android應用
1.導出Android應用
在工程上右解-->Export-->Android-->Export Android Application,將工程導出為APK包。
2.將APK包放入到SDCard目錄中
在FileExplorer面板的右上角有一個導入手機圖標,將上面生成的APK包導入到SDCard目錄中。
3.編寫安裝APK包的Android程序
1).在AndoirdManifest.xml添加權限:
<!-- 安裝程序權限 --> <uses-permission android:name="android.permission.INSTALL_PACKAGES"/> |
2).通過Android提供的功能,安裝APK:
Intent intent = new Intent(); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); intent.setAction(android.content.Intent.ACTION_VIEW); Uri data = Uri.fromFile(new File(Environment.getExternalStorageDirectory(), filename)); intent.setDataAndType(data, "application/vnd.android.package-archive"); startActivity(intent); |
Android的學習到此結束!