update: 2016/07/20
reference:
1. Beacon Tech Overview - Estimote Developer
Part 2: Background monitoring
A. Add a beacon manager
1. 新增一個 MyApplication Java Class:
> 點選 MainActivity class 所屬的套件 > 滑鼠右鍵 > New
> Java Class
> Name: MyApplication
> Kind: Class
> OK
> 結果:
package myhome.airport;
import android.app.Application;
import com.estimote.sdk.BeaconManager;
//public class MyApplication {
public class MyApplication extends Application {
private BeaconManager beaconManager;
@Override
public void onCreate() {
super.onCreate();
beaconManager = new BeaconManager(getApplicationContext());
}
}
3. 在 AndroidManifest.xml 裡, 宣告此類別:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="myhome.airport">
<application
android:name=".MyApplication"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
-----------------------------------------------------------------------------------------------
B. Start monitoring
1. 開啟 MyApplication.java 修改如下:
package myhome.airport;
import android.app.Application;
import com.estimote.sdk.BeaconManager;
import com.estimote.sdk.Region;
import java.util.UUID;
//public class MyApplication {
public class MyApplication extends Application {
private BeaconManager beaconManager;
@Override
public void onCreate() {
super.onCreate();
beaconManager = new BeaconManager(getApplicationContext());
beaconManager.connect(new BeaconManager.ServiceReadyCallback() {
@Override
public void onServiceReady() {
// Monitor: UUID, major, minor
/*
beaconManager.startMonitoring(new Region(
"monitored region",
UUID.fromString("FDA50693-A4E2-4FB1-AFCF-C6EB07647825"),
0, 5));
*/
// Monitor: UUID, major
/*
beaconManager.startMonitoring(new Region(
"monitored region",
UUID.fromString("FDA50693-A4E2-4FB1-AFCF-C6EB07647825"),
0, null));
*/
// Monitor: UUID
beaconManager.startMonitoring(new Region(
"monitored region",
UUID.fromString("FDA50693-A4E2-4FB1-AFCF-C6EB07647825"),
null, null));
}
});
}
}
-----------------------------------------------------------------------------------------------
C. Show an "enter / exit" notification
1. add a helper method to the MyApplication class:
// 開啟 MyApplication.java 修改如下:
package myhome.airport;
import android.app.Application;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import com.estimote.sdk.BeaconManager;
import com.estimote.sdk.Region;
import java.util.UUID;
//public class MyApplication {
public class MyApplication extends Application {
....
// show Notification
public void showNotification(String title, String message) {
Intent notifyIntent = new Intent(this, MainActivity.class);
notifyIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pendingIntent = PendingIntent.getActivities(this, 0,
new Intent[] { notifyIntent }, PendingIntent.FLAG_UPDATE_CURRENT);
Notification notification = new Notification.Builder(this)
.setSmallIcon(android.R.drawable.ic_dialog_info)
.setContentTitle(title)
.setContentText(message)
.setAutoCancel(true)
.setContentIntent(pendingIntent)
.build();
notification.defaults |= Notification.DEFAULT_SOUND;
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(1, notification);
}
}
2. 開啟 MyApplication.java 修改如下:
package myhome.airport;
import android.app.Application;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import com.estimote.sdk.Beacon;
import com.estimote.sdk.BeaconManager;
import com.estimote.sdk.Region;
import java.util.List;
import java.util.UUID;
//public class MyApplication {
public class MyApplication extends Application {
private BeaconManager beaconManager;
@Override
public void onCreate() {
super.onCreate();
beaconManager = new BeaconManager(getApplicationContext());
beaconManager.setMonitoringListener(new BeaconManager.MonitoringListener() {
// on Entered Region
@Override
public void onEnteredRegion(Region region, List<Beacon> list) {
showNotification(
"Notification",
"You Entered Region!");
}
// on Exited Region
@Override
public void onExitedRegion(Region region) {
showNotification(
"Notification",
"You Exited Region!");
}
});
}
....
}
3. 結果:
沒有留言:
張貼留言
注意:只有此網誌的成員可以留言。