是否有使用 Mono for Android (C#) 创建简单主屏幕小部件的教程?在官方网站上只有小部件的代码,没有教程。我想做的就是向小部件写入一些文本并每隔 x 更新一次文本。
最佳答案
当您设置 updatePeriodMillis 属性值时,不能保证您的 onUpdate 方法会在该时间段内准确调用,您必须处理 AlarmManager。
来自 https://github.com/xamarin/monodroid-samples/tree/master/SimpleWidget , 像这样替换 onUpdate:
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);
}
然后在服务中:
WordEntry entry = new WordEntry() { Title = "test", Description = DateTime.Now.ToLongTimeString() };
小部件将每 3 秒刷新一次。
希望这对您有所帮助。
关于c# - Mono for Android Widget 教程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13212440/