Android,显示alertDialog而不是应用程序打开时的通知

时间:2015-09-16 02:08:45

标签: android android-notifications android-alertdialog intentservice android-geofence

我按照this开发人员教程,按照预期在我的应用中使用Geofencing。

IntentService

内发生地理围栏转换时会发送通知
@Override
protected void onHandleIntent(Intent intent) {
    GeofencingEvent geofencingEvent = GeofencingEvent.fromIntent(intent);

    ...        

    sendNotification(geofenceTransitionDetails);
}

private void sendNotification(String notificationDetails) {
    // Create an explicit content Intent that starts the main Activity.
    Intent notificationIntent = new Intent(getApplicationContext(), MainActivity.class);

    // Construct a task stack.
    TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);

    // Add the main Activity to the task stack as the parent.
    stackBuilder.addParentStack(MainActivity.class);

    // Push the content Intent onto the stack.
    stackBuilder.addNextIntent(notificationIntent);

    // Get a PendingIntent containing the entire back stack.
    PendingIntent notificationPendingIntent =
            stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);

    // Get a notification builder that's compatible with platform versions >= 4
    NotificationCompat.Builder builder = new NotificationCompat.Builder(this);

    // Define the notification settings.
    builder.setSmallIcon(R.mipmap.ic_launcher)
            // In a real app, you may want to use a library like Volley
            // to decode the Bitmap.
            .setLargeIcon(BitmapFactory.decodeResource(getResources(),
                    R.mipmap.ic_launcher))
            .setColor(Color.RED)
            .setContentTitle(notificationDetails)
            .setContentText("Return to app")
            .setContentIntent(notificationPendingIntent);

    // Dismiss notification once the user touches it.
    builder.setAutoCancel(true);

    // Get an instance of the Notification manager
    NotificationManager mNotificationManager =
            (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

    // Issue the notification
    mNotificationManager.notify(0, builder.build());
}

这是教程中的千篇一律。意图在主要活动中设置:

private PendingIntent getGeofencePendingIntent() {
    // Reuse the PendingIntent if we already have it.
    if (mGeofencePendingIntent != null) {
        return mGeofencePendingIntent;
    }
    Intent intent = new Intent(this, GeofenceTransitionsIntentService.class);
    // We use FLAG_UPDATE_CURRENT so that we get the same pending intent back when calling
    // addGeofences() and removeGeofences().
    return PendingIntent.getService(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
}

如果应用程序处于打开状态,如何添加禁止通知的功能,而是向用户显示AlertDialog?理想情况下,我希望能够执行不同的任务,具体取决于发生地理围栏转换时用户当前所处的视图。我可以监控/拦截每个视图中的转换,还是以某种方式全局监控?

提前致谢。

3 个答案:

答案 0 :(得分:7)

有些答案是不完整的,所以这里是我正在寻找的完整解决方案。

首先,设置MyApplication类,实现ActivityLifecycleCallbacks

public class MyApplication extends Application implements Application.ActivityLifecycleCallbacks {

    private static boolean isActive;

    @Override
    public void onCreate() {
        super.onCreate();

        registerActivityLifecycleCallbacks(this);
    }

    public static boolean isActivityVisible(){
        return isActive;
    }

    @Override
    public void onActivityResumed(Activity activity) {
        isActive = true;
    }

    @Override
    public void onActivityPaused(Activity activity) {
        isActive = false;
    }

    ... no other methods need to be used, but there are more that 
    ... must be included for the ActivityLifecycleCallbacks
}

请务必在清单中命名(仅添加名称行,默认为休息):

<application
    android:name=".MyApplication"
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme"
    android:hardwareAccelerated="true">

上面所做的工作用于跟踪应用的生命周期。您可以使用它来检查您的应用当前是否在前台。

接下来是设置BroadcastReceiver,无论您希望代码运行在哪里(如果应用程序在触发器发生时打开)。在这种情况下,它位于我的MainActivity

protected BroadcastReceiver mNotificationReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        ... Do whatever you want here

        Toast.makeText(...).show();
    }
};

在同一活动的onCreate注册接收者:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    ...

    LocalBroadcastManager.getInstance(this).registerReceiver(mNotificationReceiver, new IntentFilter("some_custom_id"));
}

不要忘记取消注册:

@Override
protected void onDestroy() {
    LocalBroadcastManager.getInstance(this).unregisterReceiver(mNotificationReceiver);
    super.onDestroy();
}

收到广播时,接收器内的代码将被执行。

现在,检查应用程序是否在前台,如果是,则发送广播。在IntentService

@Override
protected void onHandleIntent(Intent intent) {
    GeofencingEvent geofencingEvent = GeofencingEvent.fromIntent(intent);
    if (geofencingEvent.hasError()) {
        String errorMessage = getErrorString(this,
                geofencingEvent.getErrorCode());
        return;
    }

    int geofenceTransition = geofencingEvent.getGeofenceTransition();

    // Test that the reported transition was of interest.
    if (geofenceTransition == Geofence.GEOFENCE_TRANSITION_ENTER ||
            geofenceTransition == Geofence.GEOFENCE_TRANSITION_EXIT) {

        ...

        if(MyApplication.isActivityVisible()){
            Intent intnt = new Intent("some_custom_id");
            intnt.putExtra("message", geofenceTransitionDetails);
            LocalBroadcastManager.getInstance(this).sendBroadcast(intnt);
        }else{
            sendNotification(geofenceTransitionDetails);
        }

    } else {
        // Log the error.
    }
}

重要的位是最后一个嵌套的if语句:

if(MyApplication.isActivityVisible()){
    Intent intnt = new Intent("some_custom_id");
    intnt.putExtra("message", geofenceTransitionDetails);
    LocalBroadcastManager.getInstance(this).sendBroadcast(intnt);
}else{
    sendNotification(geofenceTransitionDetails);
}

使用上面定义的MyApplication.isActivityVisible()检查应用是否位于前台,然后发送通知或发送广播。只需确保您的意图代码(即"some_custom_id")与您的发件人和收件人匹配。

就是这样。如果应用程序位于前台(特别是MainActivity),我会执行一些代码。如果应用程序不在前台,我会发送通知。

答案 1 :(得分:1)

最简单的方法是使用LocalBroadcastManager或某些event bus

因此,当转换发生时,您应该从IntentService发送本地广播,并在component X和您IntentService之间的某些Activity中抓住它。 Component X必须跟踪您Activity中的任何一个是否在前景中

  • 如果是 - 传递其他本地广播(到前台Activity),
  • 如果没有 - 显示通知。

请注意,在Android中,如果您的应用处于前台,则无法轻松跟踪(如果您有超过1项活动,则我认为您无法正确执行此操作),you can try

答案 2 :(得分:0)

a)您可以通知您的服务活动的生命周期事件。

b)您可以将UI的当前状态保存在活动的静态字段中,并在显示通知之前从服务中进行检查。