1

我对 Android 开发相当陌生,最近才开始尝试 Estimote Beacon 开发工具包,并遵循以下初始教程:Android 教程后台监控

我按照本教程创建了一个应用程序,当手机靠近信标时会弹出通知。我的应用程序有一个 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 类中,因为它不是活动,因此没有主题。

当在 MyApplication 类中触发 OnEnteredRegion 时,有没有办法可以从 MainActivity 类设置 AlertDialog?

谢谢

4

0 回答 0