设为首页 收藏本站
查看: 2242|回复: 0

Windows phone 8 基于定位的后台应用

[复制链接]

尚未签到

发表于 2015-5-22 09:18:23 | 显示全部楼层 |阅读模式
  Windows phone 8 基于定位的后台应用
  后台应用算是 windows phone 8 所特有的一个新功能,说起后台我经常要和地图一起聊起 【关于地图的用法请参考:Windows Phone 8 Nokia地图控件 】今天我着重跟大家聊一下手机定位及基于定位的后台应用,说到定位相信大家已经不再陌生,下载各个平台的只能手机定位 GPS & AGPS 都是一个基本功能很多应用都会用到,但是后台定位应用可能有些同学不他理解,我举一个“栗子”说,好比我现在开车正在借助一款手机导航软件寻找某个餐馆,此时家里的那位老大已经到了目的地要检查一下我到哪了,于是电话响了。。。这时导航软件必然被切换到后台,相信用过手机导航的同学都有过这样的经历,如此场景其他平台也就罢了,在如今windows phone8 是应用支持后台的,那么这个后台能做什么呢?简单的说也就是在此场景下应用可以通过其他形式的提醒方式继续为用户提供导航,例如 ShellToast 当然后台能够使用的API是受限制 但是也足够用了 API的限制请相信参考MSDN:http://msdn.microsoft.com/en-us/library/windowsphone/develop/jj662941(v=vs.105).aspx
  此文是 升级到WP8必需知道的13个特性 系列的一个更新 希望这个系列可以给 Windows Phone 8开发者带来一些开发上的便利。
  同时欢迎大家在这里和我沟通交流或者在新浪微博上 @王博_Nick
  首先我先给大家介绍如使用定位
  使用定位功能当然还是要在Manifest文件中声明 location 这里我用的是上一节的Demo 所以也选中了MAP如果你的应用没有使用地图控件可以不选MAP,
DSC0000.png
  
  这里介绍一下 Geolocator 这个对象使用它来对地理位置进行获取、初始精度、追踪状态等。



        private void TrackLocation_Click_1(object sender, EventArgs e)
{
if (!tracking)
{
App.Geolocator = new Geolocator();
App.Geolocator.DesiredAccuracy = PositionAccuracy.High;
App.Geolocator.MovementThreshold = 2; // The units are meters.

App.Geolocator.StatusChanged += geolocator_StatusChanged;
App.Geolocator.PositionChanged += geolocator_PositionChanged;
tracking = true;
//TrackLocationButton.Content = "stop tracking";
            }
else
{
App.Geolocator.PositionChanged -= geolocator_PositionChanged;
App.Geolocator.StatusChanged -= geolocator_StatusChanged;
App.Geolocator = null;
tracking = false;
//TrackLocationButton.Content = "track location";
StatusTextBlock.Text = "stopped";
}
}
  
  下面主要使用了StatusChange 和 PositionChange 时间来进行位置获取和路径追踪。
  注释中可以明确的看到返回的枚举值都代表着目前是什么样的一个状态,注意这里包括获取到用户在 系统设置中禁用了定位服务(之前有朋友问过我这个问题)



        void geolocator_StatusChanged(Geolocator sender, StatusChangedEventArgs args)
{
string status = "";
switch (args.Status)
{
case PositionStatus.Disabled:
// the application does not have the right capability or the location master switch is off
status = "location is disabled in phone settings";
break;
case PositionStatus.Initializing:
// the geolocator started the tracking operation
status = "initializing";
break;
case PositionStatus.NoData:
// the location service was not able to acquire the location
status = "no data";
break;
case PositionStatus.Ready:
// the location service is generating geopositions as specified by the tracking parameters
status = "ready";
break;
case PositionStatus.NotAvailable:
status = "not available";
// not used in WindowsPhone, Windows desktop uses this value to signal that there is no hardware capable to acquire location information
break;
case PositionStatus.NotInitialized:
// the initial state of the geolocator, once the tracking operation is stopped by the user the geolocator moves back to this state
break;
}
Dispatcher.BeginInvoke(() =>
{
StatusTextBlock.Text = status;
});
}
  
  
  PositionChange 中的代码是上次讲地图的时候写的code添加了一个图层来标记当前位置,当然 args.Position.Coordinate 中的属性就是我们想得到的经纬度信息了



        void geolocator_PositionChanged(Geolocator sender, PositionChangedEventArgs args)
{
Dispatcher.BeginInvoke(() =>
{
if (!MyMap.MapElements.Contains(MPL))
MyMap.MapElements.Add(MPL);
CurrenLocation = new GeoCoordinate(args.Position.Coordinate.Latitude, args.Position.Coordinate.Longitude);
MPL.Path.Add(CurrenLocation);
MyMap.SetView(CurrenLocation, 15, MapAnimationKind.Parabolic);
MyMap.Layers.Clear();
MapOverlay MyOverlay = new MapOverlay();
MyOverlay.Content = GetGrid();
MyOverlay.GeoCoordinate = new GeoCoordinate(CurrenLocation.Latitude, CurrenLocation.Longitude);
MyOverlay.PositionOrigin = new Point(0, 0.5);
MapLayer MyLayer = new MapLayer();
MyLayer.Add(MyOverlay);
MyMap.Layers.Add(MyLayer);
});
}
  
  其次再给大家介绍如何使应用在后台继续跟踪定位
  上面我只是实现了一个定位应用和WP7样的在后台不会继续工作,接下来我对这个项目稍作修改 让大家看看怎么做一个基于定位的后台应用。
  首先呢我们需要手动修改Manifest文件,也就是右键Manifest文件文本编辑,在Tasks 下 DefaultTask节点中添加 BackgroundExecution节点如下:



   






  
  之后呢我们打开 项目文件中的 App.xaml 在shell:PhoneApplicationService 中注册 RuningInBackground 事件用来标记此应用以及跑在后,并且我们声明两个静态属性 分别是 Geolocator 和 RunningInBackground,作用是在应用程序中共享状态。



public static Geolocator Geolocator { get; set; }
public static bool RunningInBackground { get; set; }
private void Application_RunningInBackground(object sender, RunningInBackgroundEventArgs args)
{
RunningInBackground = true;
// Suspend all unnecessary processing such as UI updates
}
private void Application_Activated(object sender, ActivatedEventArgs e)
{
RunningInBackground = false;
}
  分别在声明周期的 RunningInBackground 和 Activated 事件中标记应用程序的后台运行情况,细心的同学可能已经发现我在前面声明 Geolocator 的时候已经是赋值给 App.Geolocator 以确保在后台也可以持续访问该对象
DSC0001.png
  另外还要额外处理一下页面的 OnRemovedFromJournal 事件以确保再次访问此页面的时候讲从新创建新的实例:



protected override void OnRemovedFromJournal(System.Windows.Navigation.JournalEntryRemovedEventArgs e)
{
App.Geolocator.PositionChanged -= geolocator_PositionChanged;
App.Geolocator = null;
}
  同时更新代码 StatusChanged



        void geolocator_StatusChanged(Geolocator sender, StatusChangedEventArgs args)
{
string status = "";
switch (args.Status)
{
case PositionStatus.Disabled:
// the application does not have the right capability or the location master switch is off
status = "location is disabled in phone settings";
break;
case PositionStatus.Initializing:
// the geolocator started the tracking operation
status = "initializing";
break;
case PositionStatus.NoData:
// the location service was not able to acquire the location
status = "no data";
break;
case PositionStatus.Ready:
// the location service is generating geopositions as specified by the tracking parameters
status = "ready";
break;
case PositionStatus.NotAvailable:
status = "not available";
// not used in WindowsPhone, Windows desktop uses this value to signal that there is no hardware capable to acquire location information
break;
case PositionStatus.NotInitialized:
// the initial state of the geolocator, once the tracking operation is stopped by the user the geolocator moves back to this state
break;
}
Dispatcher.BeginInvoke(() =>
{
StatusTextBlock.Text = status;
});
if (App.RunningInBackground)
{
Microsoft.Phone.Shell.ShellToast toast = new Microsoft.Phone.Shell.ShellToast();
toast.Title = "Location: ";
toast.Content = args.Status.ToString();
toast.NavigationUri = new Uri("/MainPage.xaml", UriKind.Relative);
toast.Show();
}
}
  和 PositionChanged



        void geolocator_PositionChanged(Geolocator sender, PositionChangedEventArgs args)
{
if (App.RunningInBackground)
{
Microsoft.Phone.Shell.ShellToast toast = new Microsoft.Phone.Shell.ShellToast();
toast.Title = "Location is ";
toast.Content = "Latitude: " + args.Position.Coordinate.Latitude.ToString("0.00") + " Longitude: " + args.Position.Coordinate.Longitude.ToString("0.00");
toast.NavigationUri = new Uri("/MainPage.xaml", UriKind.Relative);
toast.Show();
}
Dispatcher.BeginInvoke(() =>
{
if (!MyMap.MapElements.Contains(MPL))
MyMap.MapElements.Add(MPL);
CurrenLocation = new GeoCoordinate(args.Position.Coordinate.Latitude, args.Position.Coordinate.Longitude);
MPL.Path.Add(CurrenLocation);
MyMap.SetView(CurrenLocation, 15, MapAnimationKind.Parabolic);
MyMap.Layers.Clear();
MapOverlay MyOverlay = new MapOverlay();
MyOverlay.Content = GetGrid();
MyOverlay.GeoCoordinate = new GeoCoordinate(CurrenLocation.Latitude, CurrenLocation.Longitude);
MyOverlay.PositionOrigin = new Point(0, 0.5);
MapLayer MyLayer = new MapLayer();
MyLayer.Add(MyOverlay);
MyMap.Layers.Add(MyLayer);
});
}
  之后我们启动应用程序
  打开模拟器的 Additional tools中的location来模拟地理位置的变化,当然实现要把我们的程序切如后台。
DSC0002.png
  运行效果如下:
DSC0003.png
  好了相信大家看过之后在 windows phone 8 中实现一个基于定位的后台应用已经有了一个了解,欢迎大家在这里和我沟通交流或者在新浪微博上 @王博_Nick
  
  
  

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-69406-1-1.html 上篇帖子: 重新想象 Windows 8 Store Apps (27) 下篇帖子: Windows Phone 8初学者开发—第4部分:XAML简介
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表