2012-11-03 58 views
2

是否有任何教程用於使用Mono for Android(C#)創建簡單的主屏幕小部件?在官方網站上,只有一個小部件的代碼,沒有教程。我只想寫一些文本到小部件並每隔x更新文本。Mono for Android Widget教程

回答

0

當您設置updatePeriodMillis屬性它不能保證爲您的onUpdate方法的價值與週期恰好被調用,你必須處理AlarmManager。在服務

private PendingIntent service = null; 

    public override void OnUpdate (Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) 
    { 
     //// To prevent any ANR timeouts, we perform the update in a service 
     //context.StartService (new Intent (context, typeof (UpdateService))); 

     AlarmManager m = (AlarmManager) context.GetSystemService(Context.AlarmService); 

     Intent i = new Intent (context, typeof (UpdateService)); 

     if (service == null) 
     { 
      service = PendingIntent.GetService(context, 0, i, PendingIntentFlags.CancelCurrent); 
     } 

     m.SetRepeating(AlarmType.Rtc, 0, 1000 * 3, service); 
    } 

然後:

https://github.com/xamarin/monodroid-samples/tree/master/SimpleWidget,這樣更換的onUpdate

WordEntry entry = new WordEntry() { Title = "test", Description = DateTime.Now.ToLongTimeString() }; 

Widget就會刷新一次,每3秒。

希望這會有所幫助。