簡單的總結一下這學期的Android學習內容:
Android樣式
Android的樣式一般定義在項目res/values/styles.xml中
樣式的使用:
<Button style="@style/mystyle">
Android拍照:
- 使用系統相機:原理是通過本APP來調用另一個APP(相機)來拍照,通過Intent來完成 。
//1.啟動系統相機:
Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent);
//2.在回調方法中的到拍照圖片
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Bitmap bp = (Bitmap) data.getExtras().get("data");
iv.setImageBitmap(bp);
}
- 使用Camera API
必須聲明一下兩個權限:
<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
創建Camera:
Camera obj Camera.open();
takePicture()
方法解釋:
takePicture(Camera.ShutterCallback, Camera.PictureCallback, Camera.PictureCallback, Camera.PictureCallback)
//第一個是塊門按下的回調。后三個接口分別對應三分圖像數據,分別是原始圖像,壓縮圖像,JPG圖像,通常只關心JPG圖像數據,此時前兩個接口參數可以直接傳null
位置服務
- Intent與PendingIntent:
- Intent:Intent代表一個意圖,它描述了想要啟動一個Activity、Broadcast或Service的意圖
通過startActivity、startService或sendBroadcast方法啟動. - PendingIntent:主要用于延遲執行的任務,是對Intent的包裝。由其他APP間接指揮本APP來執行Intent,PendingIntent的執行者是原APP。
- PendingIntent使用場景:
- 發送短消息
sendTextMessage(destinationAddress, scAddress, text, sentIntent, deliveryIntent);//在“短信發送成功”和“對方收到此短信”才會激活 sentIntent和deliveryIntent這兩個Intent。這也相當于是延遲執行了Intent。
- 通知:
notification.setLatestEventInfo(this, “標題”, “通知的內容", contentIntent);
- 定時任務:
setRepeating (int type, long triggerAtMillis, long intervalMillis, PendingIntent operation)
- 創建PendingIntent:
//PendingIntent類有三個靜態方法:
getActivity(Context c, int requestCode, Intent intent, int flags)
getService(Context c, int requestCode, Intent intent, int flags)
getBroadcast(Context c, int requestCode, Intent intent, int flags)
- 位置服務:
Android平臺支持提供位置服務的API,可以利用GPS(Global Positioning System,全球定位系統)和Network Location Provider(網絡位置提供器)來獲得用戶的位置。 - 核心API:LocationManager
它是訪問Android位置的入口,和其他服務一樣,不能直接創建,而是通過Context的getSystemService()來獲得對象實例
LocationManager lm = getSystemService(Context.LOCATION_SERVICE);
- 重要方法:
requestLocationUpdates()
,周期性的獲取定位信息。
getLastKnowLocation
:獲取最近一次已知的定位信息。 - 當位置發生改變,Android會用兩種方式來通知APP:(任意選擇一種實現)
- LocationListener:實現該接口及其定義的回調方法:
onLocationChanged
優點:簡單
缺點:若當前APP退出,則listener被destroy,無法再收到位置更新通知 - PendingIntent:包裝一個Intent(通常是廣播Intent),配合一個BroadcastReceiver來實現
優點:即使當前APP退出,也能收到位置更新通知(這是PendingIntent的特性)
缺點:實現較復雜
Intent
- 顯式Intent:
通過指定Intent組件名稱來實現的,它一般用在知道目標組件名稱的前提下,一般是在相同的應用程序內部實現的。
public void onClick(View v) {
Intent I = new Intent(QuizActivity.this, CheatActivity.class);
boolean answer = true;
I.putExtra(”myanswer”, answer);
startActivity(i);
}
//CheatActivity.java
public void onCreate(Bundle b) {
super.onCreate(b);
mAnswer = getIntent().getBooleanExtra(“myanswer”, false);
}
- 隱式Intent:
通過Intent Filter來實現的,它一般用在沒有明確指出目標組件名稱的前提下,一般是用于在不同應用程序之間。(隱式Intent告訴操作系統我們希望執行的功能,但并不清楚那個應用程序完成該功能,有操作系統或用戶決定)
例如:點擊分享按鈕,會彈出一系列可共享的APP供用戶選擇
public void onClick(View v) {
Intent i = new Intent(Intent.ACTION_SEND);
startActivity(i);
}
- 系統Launcher實現原理:
所有APP的主Activity都具有一個特殊得action和category
<intent-filter>
<action android:name=“android.intent.action.MAIN” />
<category android:name=“android.intent.category.LAUNCHER” />
</intent-filter>
想要作為系統的Launcher(或者叫做桌面,主屏)需要修改應用配置
當用戶按下Home鍵,則會出現讓用戶選擇Launcher的界面:
<intent-filter>
<action android:name=“android.intent.action.MAIN” />
<category android:name=“android.intent.category.LAUNCHER” />
<category android:name=“android.intent.category.HOME” />
<category android:name=“android.intent.category.DEFAULT” />
</intent-filter>
后臺服務(background service):
- 全局定時器AlarmManager:提供對系統鬧鐘服務的訪問接口(系統服務)
當鬧鐘響起,實際上是系統發出了為這個鬧鐘注冊的廣播,會自動開啟目標應用
在指定時間后執行一次任務:
set(int type, long triggerAtMillis, PendingIntent operation)
- 參數1:時間類型,可選值:
RTC,全局時間,但不會喚醒設備
RTC_WAKEUP,全局時間,會喚醒設備 - 參數2:開始時間,單位是毫秒。
若開始時間是過去的時間,則立即執行 - 參數3:待執行任務:其實就是Intent
注:若同一任務被設定兩次,則后一次會代替前一個任務,前一個任務失效
在指定重復周期內不斷執行:
setRepeating (int type, long triggerAtMillis, long intervalMillis, PendingIntent operation)
參數1:時間類型,可選RTC及RTC_WAKEUP
參數2:開始時間,單位毫秒
參數3:時間間隔,單位毫秒。每隔這么長時間就執行一次
參數4:執行的任務
- intent不能直接傳入AlarmManager,必須經過PendingIntent包裝一下。
Intent i = new Intent(context, MyActivity.class);
PendingIntent p = PendingIntent.getActivity(context, 0, i, 0);
- 后臺服務:
類似于Activity,但沒有界面
繼承自IntentService
可接收Intent,以便外界調用
必須在AndroidManifest.xml中注冊
public class PollService extends IntentService {
@Override
protected void onHandleIntent(Intent intent) {
//處理具體任務
Log.i(TAG, "開始執行任務:" + intent);
}
}
后臺服務默認采用隊列機制,避免并發問題
廣播接收器(broadcast receiver)
Android提供的系統服務
允許你的應用接收系統或其他應用的事件(即得到通知)
當一個事件發生時,所有注冊的接收器(receiver)都會被通知
例如:一個應用注冊了接收ACTION_BOOT_COMPLETED事件,那么當Android系統啟動完成時就會通知它。(即常見的自動啟動應用)
- 當事件發生時,一個BroadcastReceiver的onReceive()方法將會被調用。該方法結束,整個receiver對象也將會被清理掉
- 一般不應該在onReceive()方法中執行太耗時的任務,避免拖慢系統響應。
- 如何實現廣播接收器:
- 繼承BroadcastReceiver實現其中的onReceive()方法
- 登記注冊:
- 靜態注冊,通過AndroidManifest.xml
<application>
節點下添加<receiver>
節點來注冊。 - 動態注冊,調用Context.registerReceiver()方法。必須主動注銷
自定義權限必須制定android:protectionLevel屬性值:
normal,dangerous,signature,signatureOrSystem - 有序廣播(Ordered Broadcast)
有序廣播比較特殊,它每次只發送到優先級較高的接收者那里,然后由優先級高的接受者再傳播到優先級低的接收者那里,優先級高的接收者有能力終止這個廣播 - 設置有序廣播的優先級:
<intent-filter android:priority="998" >
網頁瀏覽
- 系統瀏覽器編程:
優點:無需特殊權限,簡單。
缺點:自定義選項少,控制力度小
Uri uri = Uri.parse(“http://www.google.com”);
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
- 使用WebView:
可以在應用中顯示一個網頁,是系統自帶的一個組件。但不是一個功能完整的瀏覽器:沒有前進,后退,沒有網址框
使用步驟:
- 首先將WebView添加到layout文件中
- 在activity中獲得WebView實例
開啟JavaScript
加載指定網頁
mWebView = (WebView)findViewById(R.id.activity_main_webview);
WebSettings = webSettings = mWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
mWebView.loadUrl("http:www.//baidu.com");
- 添加網絡使用權限:在
<manifest>
節點中
<uses-permission android:name="android.permission.INTERNET" />
- 綁定JavaScript與Android:
步驟:
- 建立一個類,其中包含希望JavaScript調用的方法
從Android 17版本(4.2)開始,所有可被調用的方法必須用@JavascriptInterface
注解
-
調用WebView的addJavascriptInterface(),將之前類的實例傳入
-
在JavaScript中即可調用自己所定義的Java方法
其他問題:
- 處理網頁跳轉:覆蓋
shouldOverrideUrlLoading(WebView view, String url)
方法。若返回true,則表示讓當前APP去處理即將打開的網址(默認是調用系統瀏覽器)。若返回false,則表示讓WebView來處理該網址(在當前APP打開) - 處理網頁瀏覽歷史:
覆蓋activity的onKeyDown()
方法來監測用戶按鍵并處理