服務
服務和Android Activity是同級別的,並不是單獨的進程和上面三以及Content provider並稱爲Android 四大件。服務和Activity一樣,有自己的生命週期,只是沒有界面,其也是運行在主線程裏面的,所以當有耗時的操作時,一定要另開一個線程來完成這個操作。不然就會卡死。
Service與Intentservice
Service與Intent service的區別就是Intent service裏面可以有耗時的操作,它是另起一個線程
Service與bindingService
服務從創建到銷燬,有兩種方式,一爲start service ,其二是bind service,兩者都要
但是呢,其調用的方法不一樣
就是說binding service比start service多了一個解除bind的過程
當所有的與服務的連接都被unbind時,服務就被銷燬了
通知
先獲取管理通知類
private NotificationManager nMgr;
nMgr =(NotificationManager)GetSystemService(NotificationService);
設置通知有關信息,並通過管理通知類發送
//初始化點擊通知後打開的活動
PendingIntent pintent =PendingIntent.GetActivity(this, 0, new Intent(this, typeof(MainActivity)),PendingIntentFlags.UpdateCurrent);
//設置通知的主體
notify.SetLatestEventInfo(this,"普通通知標題","普通通知內容",pintent);
//發送通知
nMgr.Notify(0, notify);
取消通知
//根據id取消通知
nMgr.Cancel(0);
除開通知本身,其中推送,取消推送都是靠通知管理類的 實例來完成的
廣播
先實現一個類,繼承BroadcastReceiver
classBroadReceiver:Android.Content.BroadcastReceiver
{
public override void OnReceive(Context context, Intent intent)
{
//todo
}
}
註冊廣播接收器
broad = new BroadReceiver();
RegisterReceiver(broad, newIntentFilter()); //Register
發送廣播
SendBroadcast(sintent);
取消發送廣播
UnregisterReceiver(broad);//ungister
關於Activity活動,服務,廣播,ContentProvider理解比較好的博文(隨手記) :http://www.cnblogs.com/bravestarrhu/archive/2012/05/02/2479461.html