简体   繁体   English

如何恢复 Android 中的活动?

[英]How to resume activity in Android?

I am trying to create my own Alarm Android application.我正在尝试创建自己的警报 Android 应用程序。 I want to achieve that when alarm is triggered, MainActivity is resumed ( not created again ).我想在触发警报时实现 MainActivity 恢复(不再创建)。 It basically means that if I set alarm and leave my application, I want that application to be resumed when alarm is triggered.这基本上意味着如果我设置警报并离开我的应用程序,我希望在触发警报时恢复该应用程序。

Currently, I am facing a problem that when alarm is triggered while my application runs in background and I click on application icon, onCreate method is called and basically two instances of application are running simultaneously (I have used Toast messages to confirm this).目前,我面临一个问题,当我的应用程序在后台运行并单击应用程序图标时触发警报时,会调用 onCreate 方法并且基本上两个应用程序实例同时运行(我已使用 Toast 消息来确认这一点)。 I expected that click on application icon will cause its resuming if it is already running in background, but it seems it is not the case.如果应用程序图标已经在后台运行,我预计单击应用程序图标会导致其恢复,但似乎并非如此。

Also, I have tried procedure explained here: Resume activity in Android but it didn't work for me, Toast message from onCreate method appears on screen.另外,我尝试过这里解释的程序: Resume activity in Android但它对我不起作用,来自 onCreate 方法的 Toast 消息出现在屏幕上。

Can anybody help, please?有人可以帮忙吗? I am really running out of ideas here.我真的没有想法了。 Thanks in advance!提前致谢!

Solution:解决方案:

Intent intent1 = new Intent(activity, ResultActivity.class);
                intent1.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
                intent1.putExtra(Extras.EXTRA_NOTIFICATION_ID, true);
                intent1.putExtra("notificationId", EXPORT_NOTIFICATION_ID);

                Log.i(TAG, "onPostExecute: app is BackGround ");
                pendingIntent = PendingIntent.getActivity(activity, EXPORT_NOTIFICATION_ID, intent1, PendingIntent.FLAG_UPDATE_CURRENT);
                builder.setContentIntent(pendingIntent);
                mNotifyManager.cancel(EXPORT_NOTIFICATION_ID);


                Intent buttonIntent = new Intent(activity, NotificationReceiver.class);
                buttonIntent.putExtra("notificationId", EXPORT_NOTIFICATION_ID);
                PendingIntent dismissIntent = PendingIntent.getBroadcast(activity, 0, buttonIntent, 0);

                builder.addAction(android.R.drawable.ic_menu_view, "VIEW", pendingIntent);
                builder.addAction(android.R.drawable.ic_delete, "DISMISS", dismissIntent);



    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(mContext);
    notificationBuilder.setPriority(Notification.PRIORITY_MAX);
    notificationBuilder
            .setSmallIcon(R.drawable.status_icon)
            .setColor(ContextCompat.getColor(mContext, R.color.transparent))
            .setContentIntent(NotificationPendingIntent)
            .setTicker(notificationData.getCleverTapTitle())
            .setAutoCancel(isAutoCancelable())
            .setContentTitle(notificationData.getCleverTapTitle())
            .setContentText(notificationData.getCleverTapMessage())
            .setVisibility(Notification.VISIBILITY_PUBLIC)
            .setDefaults(NotificationCompat.DEFAULT_VIBRATE | NotificationCompat.DEFAULT_SOUND);


    notificationBuilder.setDefaults(Notification.DEFAULT_SOUND);
    notificationBuilder.setChannelId(NotificationChannelName.GENERAL);

    /*Create a Delete Intent that will be called when user removes the notification by swiping or by clear*/
    notificationBuilder.setDeleteIntent(getNotificationDeletePendingIntent());


    notification = notificationBuilder.build();
    notification.flags |= NotificationCompat.FLAG_AUTO_CANCEL;
    if (BMSUiUtility.isNotificationFromInbox(rawNotificaionData, mSharedPreferencesManager)) {
        mNotificationManager.notify(notificationId, notification);
    } else {
        mNotificationManager.notify(0, notification);
    }

Use the following code to cancel a Notification Your MainActivity onDestroy Mathod call:使用以下代码取消通知您的MainActivity onDestroy Mathod调用:

NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.cancel(NOTIFICATION_ID);

In this code there is alway the same id used for notifications.在此代码中,通知始终使用相同的 id。 If you have different notifications that need to be canceled you have to save the ids that you used to create the Notification.如果您有不同的通知需要取消,您必须保存用于创建通知的 ID。

AndroidManifest.xml: AndroidManifest.xml:

<activity
            android:name="com.ui.activity.ResultActivity"
            android:screenOrientation="portrait"
            android:launchMode="singleTop"
            android:theme="@style/AppTheme">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>

What you need to do is specify your activity's launch mode to singleTask or singleInstance .您需要做的是将活动的启动模式指定为singleTasksingleInstance To do this, go to your AndroidManifest.xml and change/add launchMode to your activity.为此,将 go 到您的AndroidManifest.xml并将 launchMode 更改/添加到您的活动中。

<activity
    android:name=".YourActivity"
    android:label="Your Activity"
    android:launchMode="singleInstance">

More info on the differences of different launch modes are explained here: https://developer.android.com/guide/topics/manifest/activity-element此处解释了有关不同启动模式差异的更多信息: https://developer.android.com/guide/topics/manifest/activity-element

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM