gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:20:02 26 4
gpt4 key购买 nike

我关注了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());
}

这是教程中的千篇一律。 Intent 在主要 Activity 中设置:

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?理想情况下,我希望能够执行不同的任务,具体取决于发生地理围栏转换时用户当前所在的 View 。我可以从每个 View 内部或以某种方式全局监视/拦截转换吗?

提前致谢。

最佳答案

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

首先,设置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
}

一定要在你的 list 中命名它(只添加了名称行,其余是默认的):

<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();
}
};

在同一 Activity 的 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() 检查应用是否在前台,然后发送通知或发送广播。只需确保您的 Intent 代码(即 “some_custom_id”)与您的发送者和接收者相匹配。

仅此而已。如果应用程序在前台(特别是 MainActivity),我会执行一些代码。如果应用程序不在前台,我会发送通知。

关于Android,当应用程序打开时显示 alertDialog 而不是通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32598604/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com