gpt4 book ai didi

android - 您如何检测您的 Activity 是否由于您创建的 PendingIntent 而处于 onPaused 状态?

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:04:34 25 4
gpt4 key购买 nike

如果我有一个位于前台的 Activity,并且我在我的应用程序中导航到另一个 Activity,我可以通过在您开始传输时设置一个标志并在您传输到新 Activity 时删除它来检测到这一点。这使我能够区分由于内部(标志设置)或外部(标志未设置)事件导致的 onPause Activity 。

但是,我无法为通知中嵌入的 PendingIntents 执行此操作。是否有可能检测到我的 Activity 正在暂停,因为他们选择了我在通知栏上创建的通知?是否有某种我可以使用的通知监听器,它会在通知触发之前并执行暂停我的 Activity 的未决 Intent ?

我明白这有点令人困惑,所以我制作了一个框架项目来演示这个问题:

package com.example.notificationtest;

import android.os.Bundle;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.View;

public class MainActivity extends Activity {

private static final int INCOMING_NOTIFICATION_ID = 1;
private static final String TAG = "NotificationTest";

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}

@Override
public void onPause() {
super.onPause();
Log.e(TAG,"onPause");
}

@Override
public void onResume() {
super.onResume();
Log.e(TAG,"onResume");
}

public void onTransitionButtonClick(View view) {
Intent intent = new Intent(this, MainActivity.class);
Log.e(TAG,"About to start activity: onPause will be invoked.");
startActivity(intent);
}

public void onNotificationButtonClick(View view) {
Intent intent = new Intent(this, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
Notification notification = new Notification(android.R.drawable.alert_dark_frame, "Alert!", System.currentTimeMillis());
notification.defaults |= Notification.DEFAULT_SOUND;
notification.defaults |= Notification.DEFAULT_VIBRATE;
notification.defaults |= Notification.DEFAULT_LIGHTS;
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notification.flags |= Notification.FLAG_INSISTENT;
notification.setLatestEventInfo(this, "Click to open", "Click to open", pendingIntent);

// Show the alert.
final NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(INCOMING_NOTIFICATION_ID, notification);
}

}

使用 activity_main.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<Button
android:id="@+id/notify_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="@dimen/padding_medium"
android:text="Make a notification"
android:onClick="onNotificationButtonClick"
tools:context=".MainActivity" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/notify_button"
android:padding="@dimen/padding_medium"
android:text="Normal transition"
android:onClick="onTransitionButtonClick"
tools:context=".MainActivity" />

</RelativeLayout>

如果您选择“正常转换”按钮,日志会打印“即将开始 Activity :将调用 onPause。”在暂停之前。

如果您选择“发出通知”按钮,然后向下拖动通知栏并点击通知,我想在 onPause 之前收到该点击的通知,以便我可以插入同一行“即将开始 Activity : onPause 将被调用。”。我该怎么做?

最佳答案

除了持有开始 Activity 的 Intent 外,PendingIntent 还可以持有“触发”广播的 Intent

您可以使用此类自定义广播作为通知的触发器,然后实现将注册到该广播的 BroadcastReceiver,并显示您想要的任何内容,然后才会启动所需的 Activity 。

Intent intent = new Intent("my.custom.broadcast.action");
pendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intent, 0);

接收器看起来像这样:

private BroadcastReceiver mReceiver = new BroadcastReceiver() {

@Override
public void onReceive(Context context, Intent intent) {
//do whatever you want
// if this code is being executed - it's a sign that the user clicked on the notification...

Intent intent = new Intent(this, MainActivity.class);
Log.e(TAG,"About to start activity: onPause will be invoked.");
startActivity(intent);

}
};

不要忘记在 onCreate() 调用时注册 mReceiver,并在 onDestroy() 中取消注册

registerReceiver(mReceiver , new IntentFilter("my.custom.broadcast.action"));
unregisterReceiver(mReceiver);

在这种方法中,您可以准确控制用户点击通知时会发生什么。如您所见 - 只有在点击通知时才可以发送广播。根本没有人说您必须开始 Activity ..

关于android - 您如何检测您的 Activity 是否由于您创建的 PendingIntent 而处于 onPaused 状态?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14159321/

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