2012-11-03 59 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秒。

希望这会有所帮助。