设为首页收藏本站

LUPA开源社区

 找回密码
 注册
文章 帖子 博客
LUPA开源社区 首页 业界资讯 技术文摘 查看内容

谷歌Proximity Alerts教程

2014-6-16 14:09| 发布者: joejoe0332| 查看: 4613| 评论: 0|原作者: gagayu|来自: 伯乐在线

摘要: 智能手机正在逐渐统治整个手机世界,这是一个显而易见的事实。自从GPS设备被普遍的内嵌到智能手机中,它给那些利用地理定位的应用程序提供了极大的帮助。 其中一类程序是采用基于定位的服务(Location Based Service ...


  在onCreate方法中,我们用了location manager并加入一个自定义类,它实现了LocatioListener接口。可以让我们通过onLocationChanged方法得到位置变换的通知。接下来,看一下怎样去处理更新。我们会使用不同的UI控件给按钮添加onClickListeners。


  当用户想要得到目前的坐标时,populateCoordinatesFromLastKnownLocation方法会被调用。在方法内部,使用getLastKnownLocation方法来得到Location对象,位置信息将会被填充到EditText控件。


  同样的,当位置信息第一次被获取时,用户想要保存坐标并向saveProxityAlertPoint提供alert(添加POI)。我们可以将经度和纬度以preference data的方式用SharedPreferences类来保存下来,详情请见SharedPreferences.Editor。最后,我们通过getBrodcast静态方法来创建一个PendingIntent。对于这种封装过的intent,我们创建一个intentFilter并使用registerReceiver方法来绑定一个自定义BroadcastReceiver和intentfiler。需要提醒的是,这种绑定还可以通过manifest来实现。


  现在,我们来看一下如何来处理用户位置变化。在实现接口的MyLocationListener类里,我们从SharedPreference里获取存储的位置信息(retrievelocationFromPreferences)。然后,通过distranceTo方法计算两个位置(POI和现在位置)的距离。通过这个计算,我们才会知道是否真正进入了某个区域。


  第一步,处理进入POI事件。 可以通过继承BroadcastReceiver的类(ProximityIntentReceiver)来实现。当proximity alert被触发,会返回一个自定义intent(之前绑定的locationManager)。处理过程发生在onReceive方法里,它会被事件(进出POI区域)调用。在方法内部,我们从相关intent获取KEY_PROXIMITY_ENTERING key(用来定义proximity alert类型是进入-true,还是离开-false))。


  示例代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
    package com.javacodegeeks.android.lbs;
    import android.app.Notification;
    import android.app.NotificationManager;
    import android.app.PendingIntent;
    import android.content.BroadcastReceiver;
    import android.content.Context;
    import android.content.Intent;
    import android.graphics.Color;
    import android.location.LocationManager;
    import android.util.Log;
 
    public class ProximityIntentReceiver extends BroadcastReceiver {
     
    private static final int NOTIFICATION_ID = 1000;
 
    @Override
    public void onReceive(Context context, Intent intent) {
         
        String key = LocationManager.KEY_PROXIMITY_ENTERING;
 
        Boolean entering = intent.getBooleanExtra(key, false);
         
        if (entering) {
            Log.d(getClass().getSimpleName(), "entering");
        }
        else {
            Log.d(getClass().getSimpleName(), "exiting");
        }
         
        NotificationManager notificationManager =
            (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
         
        PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, null, 0);       
         
        Notification notification = createNotification();
        notification.setLatestEventInfo(context,
            "Proximity Alert!", "You are near your point of interest.", pendingIntent);
         
        notificationManager.notify(NOTIFICATION_ID, notification);
         
    }
     
    private Notification createNotification() {
        Notification notification = new Notification();
         
        notification.icon = R.drawable.ic_menu_notifications;
        notification.when = System.currentTimeMillis();
         
        notification.flags |= Notification.FLAG_AUTO_CANCEL;
        notification.flags |= Notification.FLAG_SHOW_LIGHTS;
         
        notification.defaults |= Notification.DEFAULT_VIBRATE;
        notification.defaults |= Notification.DEFAULT_LIGHTS;
         
        notification.ledARGB = Color.WHITE;
        notification.ledOnMS = 1500;
        notification.ledOffMS = 1500;
         
        return notification;
    }
}


  代码简洁明了。当我们定义了一个进入或离开的proximity alert,就可以提供一个自定义的推送了。要实现它,首先需要引用一个合适的Service,比如NotificationMananger。通过这个Service,我们发送通知给用户,并封装合适的推送对象。推送的方式是可定制的,如包括震动或者闪光灯等。我们还可以添加特定的图标在状态栏显示。如果只是简单添加标题和信息,适合调用setLastestEventInfo方法。 你可以在这里查看关于notification的详细内容。除此之外,我们可以通过PendingIntent来引导Activity进行下意识地点击推送。 为了让代码更通俗易懂,这里我就不这样做了。


  我们最后得到的manifest代码如下:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
<?xml version="1.0" encoding="utf-8"?>
 
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.javacodegeeks.android.lbs"
      android:versionCode="1"
      android:versionName="1.0">
       
    <application android:icon="@drawable/icon" android:label="@string/app_name">
         
        <activity android:name=".ProxAlertActivity"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>    
 
    </application>
     
    <uses-sdk android:minSdkVersion="3" />
    
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.VIBRATE" />
 
</manifest>


  别忘了,添加相应的Permission到Manifest里:


  现在可以测试我们的程序了。启动Eclipse配置,然后跳转到DDMS界面查看Emulation Control标签,可以发现一个Location Controls选项。它可以给模拟器发送模拟的位置数据。在Manual标签里,点击Send按钮,这样就能生成一些坐标。



酷毙

雷人

鲜花

鸡蛋

漂亮
  • 快毕业了,没工作经验,
    找份工作好难啊?
    赶紧去人才芯片公司磨练吧!!

最新评论

关于LUPA|人才芯片工程|人才招聘|LUPA认证|LUPA教育|LUPA开源社区 ( 浙B2-20090187 浙公网安备 33010602006705号   

返回顶部