[Xamarin] 用Service 來製作一個Notification的時鐘 (转帖)
這篇利用來製作一個會出現在Notification的時鐘,來敘述一下 Service,在你製作的App被關閉時,可以透過Service繼續運行你想處理的部分,當然Service 也有其生命周期
![]()
接下來我們來介紹一下今天的案例:![]()
當按下開始報時服務按鈕時,就會啟動Service,並且更新Notification 如下:![]()
之後就算離開APP他的秒數也是會繼續跑的跟現在時間同步,之後按下停止報時服務才會將其終結..
1.首先我們得在專案裡面建立一個 RemindService.cs 當然這是自己命名的 ,他必須繼承Service
namespace XamarinServiceTest
{
//此 attribute 一定要加
[Service]
public class RemindService : Service
{
...
之後因為方便我對Notification 更新資料,我在這 ReminService 寫一個專門更新 Notification的method :
/// <summary>
/// 簡單包裝Notification
/// 詳情可以參考
/// http://no2don.blogspot.com/2013/07/xamarin-notification.html
/// </summary>
/// <param name="id">Notify id</param>
/// <param name="content">Notify content</param>
private void NotiSomething(int id, string content)
{
NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
.SetContentTitle(content)
.SetSmallIcon(Resource.Drawable.Icon2);
var notificationManager = (NotificationManager)GetSystemService(Context.NotificationService);
//都用同一組id
notificationManager.Notify(id, builder.Build());
}
因為每秒更新,所以我必須 宣告一隻 Timer
private System.Timers.Timer TimerMainTask { get; set; }
在Service 開始時,只要複寫 OnStartCommand 即可,在內容我就寫啟動 TimerMainTask讓TimerMainTask每一秒鐘都去更新Notification資訊
/// <summary>
/// 開始Service 開始時呼叫
/// </summary>
/// <param name="intent"></param>
/// <param name="flags"></param>
/// <param name="startId"></param>
/// <returns></returns>
public override StartCommandResult OnStartCommand(Android.Content.Intent intent, StartCommandFlags flags, int startId)
{
TimerMainTask = new System.Timers.Timer(1000);
TimerMainTask.Elapsed += TimerMainTask_Elapsed;
TimerMainTask.Start();
NotiSomething(4, "開始進入" + System.DateTime.Now.ToString("hh:mm:ss"));
return StartCommandResult.Sticky;
}
/// <summary>
/// 每次timer 做的事情
/// 設計為一秒跑一次
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
void TimerMainTask_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
NotiSomething(1, "當麻報時-" + System.DateTime.Now.ToString("hh:mm:ss"));
}
再來就是當Service 停止時,必須要將TimerMainTask關掉:
/// <summary>
/// 當Service 停止時呼叫
/// </summary>
public override void OnDestroy()
{
NotiSomething(4, "已停止" + System.DateTime.Now.ToString("hh:mm:ss"));
TimerMainTask.Stop();
base.OnDestroy();
}
2.第一步完成後,接下來就是在AndroidManifest.xml 加入Service服務
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.donma.xamarin.servicetest" android:installLocation="auto">
<uses-sdk android:minSdkVersion="8" android:targetSdkVersion="8" />
<application android:label="XamarinServiceTest"></application>
<service android:name="XamarinServiceTest.RemindService"></service>
</manifest>
就是加入 <service android:name="XamarinServiceTest.RemindService"></service>
3. 主程式中將RemindService 呼叫起來:
var btnStart = FindViewById<Button>(Resource.Id.btnStart);
btnStart.Click += delegate
{
StartService(new Intent(this, typeof(RemindService)));
Toast.MakeText(this, "開始Service", ToastLength.Short).Show();
};
關閉RemindService
var btnEnd = FindViewById<Button>(Resource.Id.btnEnd);
btnEnd.Click += delegate
{
StopService(new Intent(this, typeof(RemindService)));
Toast.MakeText(this, "停止Service", ToastLength.Short).Show();
};
結果:
![]()
![]()
![]()
![]()
reference :
http://docs.xamarin.com/guides/android/application_fundamentals/services/part_1_-_started_services
https://developer.android.com/training/run-background-service/index.html
[Xamarin] 用Service 來製作一個Notification的時鐘 (转帖)的更多相关文章
- [Xamarin] 製作吐司(Toast)以及圖文並茂的Toast (转帖)
最近在看Xamarin使用C#來撰寫Android App . 紀錄一下,順便給之後有需要的人可以有所參考 :) 今天要來聊的是關於Toast 這東西,這在以前Android 上面我是很常使用 拿來l ...
- concat函數 函數concat 可以用來合拼兩個或以上的字串。
12. “Mexico 墨西哥”的首都是”Mexico City”. 顯示所有國家名字,其首都是國家名字加上”City”. concat函數 函數concat 可以用來合拼兩個或以上的字串. : SE ...
- [Xamarin] 透過 intent-filter 來接管 http ,製作偽瀏覽器 (转帖)
使用Android 的朋友一定對這畫面不陌生在開啟網址的時候,或是Youtube連結的時候,因為Android 發現,你手機安裝的App有哪些可以支援這些東西的瀏覽 所以,就可以使用甚麼東西來進行開啟 ...
- [Xamarin] 製作Options Menu、Intent 呼叫網址和Market (转帖)
Android的設計如果沒意外的話通常有三棵按鈕,BACK,HOME,OPTION (圖片來源:http://developer.android.com/design/index.html) 在OPT ...
- [Xamarin] 使用Webview 來做APP (转帖)
有時候,企業要求的沒有這麼多,他原本可能官方網站就已經有支援Mobile Web Design 他只需要原封不動的開發一個APP 也或是,他只是要型錄型,或是問卷調查的型的APP,這時候透過類似像if ...
- CSS製作動畫效果(Transition、Animation、Transform)
CSS 2D Transforms https://www.w3schools.com/css/css3_2dtransforms.asp CSS 3D Transforms https://www. ...
- yii框架製作簡易RBAC權限管理
控制器源碼 <?php namespace app\controllers; use yii; use yii\web\Controller; class PowerController ext ...
- 需要重刷整個 image 的時機 - 1
最近遇到一個問題, gpio 讀出來的值與預期不同, 詳細描述如下: 首先手機 download 了一個完整的 daily build image , 接下來 不斷地修改 kernel 部分 code ...
- 鸟哥之安裝 CentOS7.x
http://linux.vbird.org/linux_basic/0157installcentos7.php since 2002/01/01 新手建議 開始閱讀之前 網站導覽 Linux 基礎 ...
随机推荐
- 第五百八十二天 how can I 坚持
好吧,是我错了,昨天,做好自己就行了,别人怎么样是别人的事,永远保持一颗单纯向上的心. 时间过得真快,明天又周六了.. 睡觉.
- APP测试工具之TraceView卡顿检测
Traceview卡顿检测 Traceview是Android平台特有的数据采集和分析工具,集成在DDMS工具中,可以采集程序中的方法执行耗时.调用关系.调用次数以及资源占用等情况. 一.使用方法 1 ...
- 【转载】理解GL_TRIANGLE_STRIP等绘制三角形序列的三种方式
GL_TRIANGLE_STRIP绘制三角形方式很多时候令人疑惑,在这里对其运作机理进行解释. 一般情况下有三种绘制一系列三角形的方式,分别是GL_TRIANGLES.GL_TRIANGLE_STRI ...
- 如何做一个avalon组件
在avalon1.5中改用更直观的自定义标签来声明组件,废掉ms-widget,引入更强大的生命周期管理,可以让组件任意套嵌. 组件是由JS,HTML,CSS构成 JS 以AMD形式组织,引入HTML ...
- 2015年毕业生收到的offer和薪资透露
个人情况:全国排名70多的大学,非211,计算机专业硕士 收到的offer: 苏州同程旅游--6k/月 ,自己要了这么多,就给了这么多,没有规范.苏州蜗牛科技--8k/月 ,给了个口头offer,要去 ...
- 何修改WAMP中mysql默认空密码--转
何修改WAMP中mysql默认空密码 http://www.cnblogs.com/hooray/archive/2011/07/23/2114792.html WAMP安装好后,mysql密码是为 ...
- SPI总线的特点、工作方式及常见错误解答
1.SPI总线简介 SPI(serial peripheral interface,串行外围设备接口)总线技术是Motorola公司推出的一种同步串行接口.它用于CPU与各种外围器件进行全双工.同步串 ...
- Python验证码识别处理实例(转载)
版权声明:本文为博主林炳文Evankaka原创文章,转载请注明出处http://blog.csdn.net/evankaka 一.准备工作与代码实例 1.PIL.pytesser.tesseract ...
- android笔记:ViewPager实现界面的滑动
最近在学习ViewPager实现界面的滑动,拜读了郭神的博客文章,并抽取归纳了自己对ViewPager的理解. ViewPager实现界面滑动的步骤如下: 1.在xml布局内加入控件android.s ...
- 5.HotSpot的算法实现
1.枚举根节点 在可达性分析中,可以作为GC Roots的节点有很多,但是现在很多应用仅仅方法区就有上百MB,如果逐个检查的话,效率就会变得不可接受. 而且,可达性分析必须在一个一致性的快照中进行-即 ...