使用应用程序进行 iBeacon 监控 class
iBeacon Monitoring with application class
我刚开始 Java 和 Android 编码。我在我的大学写了一份科学研究申请。该应用程序适用于博物馆的本地展览。我在镇上有不同的地点,每个地点都有自己的展览。
现在我为每个位置做了一个activity,这样用户就可以看到样本的一些有用信息。现在我想把这个应用程序和 iBeacons 结合起来,我从 Estimote 买了 6 个信标。我希望该应用程序向用户发送通知,其中包含一些文本,例如:"You are in front of object XY. Tap to see more information." 点击通知后,用户应打开我创建的特定 activity。我还希望该应用程序在后台搜索信标,因此如果用户靠近某个位置,he/she 会在几秒钟后自动收到通知。
现在我希望应用程序在后台运行。目前,用户只有在应用程序处于前台时才会收到通知。我拿了Estimote的例子,稍微修改了一下。
我已经在另一个线程中读到我必须在应用程序中持有 BeaconManager class。但是我应该怎么做呢。当我尝试将与信标相关的代码复制到一个新的 class 中时,我从
开始
public void BeaconMain extends Application{
但是后来我遇到了很多错误,我无法修复它们。你能帮我解决这个问题吗?到目前为止,这是我的代码:
public class MainActivity extends ActionBarActivity
{
private static final int NOTIFICATION_ID = 123;
private BeaconManager beaconManager;
private NotificationManager notificationManager;
private Region mariendom;
private Region andreasplatz;
private boolean entered = false;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Beacon beacon = getIntent().getParcelableExtra("extrasBeacon");
andreasplatz = new Region("andreasplatz", "B9407F30-F5F8-466E-AFF9-25556B57FE6D", 31134, 3);
mariendom = new Region("mariendom", "B9407F30-F5F8-466E-AFF9-25556B57FE6D", 31134, 2);
notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
beaconManager = new BeaconManager(this);
beaconManager.setBackgroundScanPeriod(500, 1000);
beaconManager.setMonitoringListener(new BeaconManager.MonitoringListener()
{
@Override
public void onEnteredRegion(Region region, List<Beacon> beacons)
{
if(region.getMinor().equals(2)){
postNotification("Sie sind am Mariendom");
Log.d("Estiomote", "Entered region");
}
else if(region.getMinor().equals(3)){
postNotification2("Sie sind am Andreasplatz");
Log.d("Estiomote", "Entered region");
}
}
@Override
public void onExitedRegion(Region region)
{
/*entered = false;
postNotification("Sie haben den Mariendom verlassen");
Log.d("Estiomote", "Exited region");*/
}
@Override
protected void onResume()
{
super.onResume();
notificationManager.cancel(NOTIFICATION_ID);
beaconManager.connect(new BeaconManager.ServiceReadyCallback()
{
@Override
public void onServiceReady()
{
try {
Log.d("Estiomote", "Start monitoring");
beaconManager.startMonitoring(andreasplatz);
beaconManager.startMonitoring(mariendom);
} catch (RemoteException e)
{
Log.d("Estiomote", "Error while starting monitoring");
}
}
});
}
@Override
protected void onDestroy()
{
notificationManager.cancel(NOTIFICATION_ID);
beaconManager.disconnect();
super.onDestroy();
}
private void postNotification(String msg)
{
Intent notifyIntent = new Intent("com.example.walter.him.mariendom");
notifyIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pendingIntent = PendingIntent.getActivities(
MainActivity.this,
0,
new Intent[]{notifyIntent},
PendingIntent.FLAG_UPDATE_CURRENT);
Notification notification = new Notification.Builder(MainActivity.this)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("HiM - Hildesheim im Mittelalter")
.setContentText(msg)
.setAutoCancel(true)
.setContentIntent(pendingIntent)
.build();
notification.defaults |= Notification.DEFAULT_SOUND;
notification.defaults |= Notification.DEFAULT_VIBRATE;
notification.defaults |= Notification.DEFAULT_LIGHTS;
notificationManager.notify(NOTIFICATION_ID, notification);
}
private void postNotification2(String msg)
{
//Intent notifyIntent = new Intent(MainActivity.this, MainActivity.class);
Intent notifyIntent = new Intent("com.example.walter.him.andreasplatz");
notifyIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pendingIntent = PendingIntent.getActivities(
MainActivity.this,
0,
new Intent[]{notifyIntent},
PendingIntent.FLAG_UPDATE_CURRENT);
Notification notification = new Notification.Builder(MainActivity.this)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("HiM - Hildesheim im Mittelalter")
.setContentText(msg)
.setAutoCancel(true)
.setContentIntent(pendingIntent)
.build();
notification.defaults |= Notification.DEFAULT_SOUND;
notification.defaults |= Notification.DEFAULT_LIGHTS;
notification.defaults |= Notification.DEFAULT_VIBRATE;
notificationManager.notify(NOTIFICATION_ID, notification);
}
}
我建议您从头开始学习如何将 BeaconManager 放入应用程序 class,而不是将 Activity 中的代码复制粘贴到应用程序中,因为有这两种方法之间存在一些差异。
最好的地方是从教程开始收集一些基本概念和一般理解:
http://developer.estimote.com/android/tutorial/part-1-setting-up/
还有一个您可以通过 Estimote Cloud 生成的通知示例:
https://cloud.estimote.com/#/apps/add/notification
一旦您了解了如何将 BeaconManager 集成到应用程序中 class,修改 onEntered/Exited 回调以根据您进入范围的信标生成不同的活动应该非常简单的。
这是我建议遵循的顺序。
完全披露:我在 Estimote 担任开发倡导者。
我刚开始 Java 和 Android 编码。我在我的大学写了一份科学研究申请。该应用程序适用于博物馆的本地展览。我在镇上有不同的地点,每个地点都有自己的展览。
现在我为每个位置做了一个activity,这样用户就可以看到样本的一些有用信息。现在我想把这个应用程序和 iBeacons 结合起来,我从 Estimote 买了 6 个信标。我希望该应用程序向用户发送通知,其中包含一些文本,例如:"You are in front of object XY. Tap to see more information." 点击通知后,用户应打开我创建的特定 activity。我还希望该应用程序在后台搜索信标,因此如果用户靠近某个位置,he/she 会在几秒钟后自动收到通知。
现在我希望应用程序在后台运行。目前,用户只有在应用程序处于前台时才会收到通知。我拿了Estimote的例子,稍微修改了一下。
我已经在另一个线程中读到我必须在应用程序中持有 BeaconManager class。但是我应该怎么做呢。当我尝试将与信标相关的代码复制到一个新的 class 中时,我从
开始public void BeaconMain extends Application{
但是后来我遇到了很多错误,我无法修复它们。你能帮我解决这个问题吗?到目前为止,这是我的代码:
public class MainActivity extends ActionBarActivity
{
private static final int NOTIFICATION_ID = 123;
private BeaconManager beaconManager;
private NotificationManager notificationManager;
private Region mariendom;
private Region andreasplatz;
private boolean entered = false;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Beacon beacon = getIntent().getParcelableExtra("extrasBeacon");
andreasplatz = new Region("andreasplatz", "B9407F30-F5F8-466E-AFF9-25556B57FE6D", 31134, 3);
mariendom = new Region("mariendom", "B9407F30-F5F8-466E-AFF9-25556B57FE6D", 31134, 2);
notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
beaconManager = new BeaconManager(this);
beaconManager.setBackgroundScanPeriod(500, 1000);
beaconManager.setMonitoringListener(new BeaconManager.MonitoringListener()
{
@Override
public void onEnteredRegion(Region region, List<Beacon> beacons)
{
if(region.getMinor().equals(2)){
postNotification("Sie sind am Mariendom");
Log.d("Estiomote", "Entered region");
}
else if(region.getMinor().equals(3)){
postNotification2("Sie sind am Andreasplatz");
Log.d("Estiomote", "Entered region");
}
}
@Override
public void onExitedRegion(Region region)
{
/*entered = false;
postNotification("Sie haben den Mariendom verlassen");
Log.d("Estiomote", "Exited region");*/
}
@Override
protected void onResume()
{
super.onResume();
notificationManager.cancel(NOTIFICATION_ID);
beaconManager.connect(new BeaconManager.ServiceReadyCallback()
{
@Override
public void onServiceReady()
{
try {
Log.d("Estiomote", "Start monitoring");
beaconManager.startMonitoring(andreasplatz);
beaconManager.startMonitoring(mariendom);
} catch (RemoteException e)
{
Log.d("Estiomote", "Error while starting monitoring");
}
}
});
}
@Override
protected void onDestroy()
{
notificationManager.cancel(NOTIFICATION_ID);
beaconManager.disconnect();
super.onDestroy();
}
private void postNotification(String msg)
{
Intent notifyIntent = new Intent("com.example.walter.him.mariendom");
notifyIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pendingIntent = PendingIntent.getActivities(
MainActivity.this,
0,
new Intent[]{notifyIntent},
PendingIntent.FLAG_UPDATE_CURRENT);
Notification notification = new Notification.Builder(MainActivity.this)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("HiM - Hildesheim im Mittelalter")
.setContentText(msg)
.setAutoCancel(true)
.setContentIntent(pendingIntent)
.build();
notification.defaults |= Notification.DEFAULT_SOUND;
notification.defaults |= Notification.DEFAULT_VIBRATE;
notification.defaults |= Notification.DEFAULT_LIGHTS;
notificationManager.notify(NOTIFICATION_ID, notification);
}
private void postNotification2(String msg)
{
//Intent notifyIntent = new Intent(MainActivity.this, MainActivity.class);
Intent notifyIntent = new Intent("com.example.walter.him.andreasplatz");
notifyIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pendingIntent = PendingIntent.getActivities(
MainActivity.this,
0,
new Intent[]{notifyIntent},
PendingIntent.FLAG_UPDATE_CURRENT);
Notification notification = new Notification.Builder(MainActivity.this)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("HiM - Hildesheim im Mittelalter")
.setContentText(msg)
.setAutoCancel(true)
.setContentIntent(pendingIntent)
.build();
notification.defaults |= Notification.DEFAULT_SOUND;
notification.defaults |= Notification.DEFAULT_LIGHTS;
notification.defaults |= Notification.DEFAULT_VIBRATE;
notificationManager.notify(NOTIFICATION_ID, notification);
}
}
我建议您从头开始学习如何将 BeaconManager 放入应用程序 class,而不是将 Activity 中的代码复制粘贴到应用程序中,因为有这两种方法之间存在一些差异。
最好的地方是从教程开始收集一些基本概念和一般理解:
http://developer.estimote.com/android/tutorial/part-1-setting-up/
还有一个您可以通过 Estimote Cloud 生成的通知示例:
https://cloud.estimote.com/#/apps/add/notification
一旦您了解了如何将 BeaconManager 集成到应用程序中 class,修改 onEntered/Exited 回调以根据您进入范围的信标生成不同的活动应该非常简单的。
这是我建议遵循的顺序。
完全披露:我在 Estimote 担任开发倡导者。