我对Android开发还很陌生,刚刚开始尝试使用Estimote Beacon开发套件,并且遵循了最初的教程:Android Tutorial Background Monitoring
我按照本教程创建了一个应用程序,当手机在信标附近时会弹出通知。我的应用程序有一个MainActivity类以及以下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() {
beaconManager.startMonitoring(new Region(
"monitored region",
UUID.fromString("B9407F30-F5F8-466E-AFF9-25556B57FE6D"),
5461, 23416));
}
});
beaconManager.setMonitoringListener(new BeaconManager.MonitoringListener() {
@Override
public void onEnteredRegion(Region region, List<Beacon> list) {
showNotification(
"Your gate closes in 47 minutes.",
"Current security wait time is 15 minutes, "
+ "and it's a 5 minute walk from security to the gate. "
+ "Looks like you've got plenty of time!");
}
@Override
public void onExitedRegion(Region region) {
// could add an "exit" notification too if you want (-:
}
});
}
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);
}
但是,我希望我的应用程序显示警报而不是通知,并且我被告知您无法将警报设置为MyApplication类,因为它不是Activity,因此没有主题。
当在MyApplication类中触发OnEnteredRegion时,有没有办法从MainActivity类设置AlertDialog?
由于