【问题标题】:How to resume activity in Android?如何在 Android 中恢复活动?
【发布时间】:2019-11-16 11:26:47
【问题描述】:

我正在尝试创建自己的 Alarm Android 应用程序。我想在触发警报时实现 MainActivity 恢复(不再创建)。这基本上意味着如果我设置了警报并离开我的应用程序,我希望在触发警报时恢复该应用程序。

目前,我面临一个问题,当我的应用程序在后台运行时触发警报并且我单击应用程序图标,onCreate 方法被调用并且基本上两个应用程序实例同时运行(我已经使用 Toast 消息来确认这一点)。如果应用程序图标已经在后台运行,我预计单击应用程序图标会导致其恢复,但似乎并非如此。

另外,我已经尝试过这里解释的程序:Resume activity in Android 但它对我不起作用,来自 onCreate 方法的 Toast 消息出现在屏幕上。

有人可以帮忙吗?我真的没有想法了。提前致谢!

【问题讨论】:

  • “点击应用程序图标”是什么意思?如果你的意思只是将它发送到后台并重新打开它,那么它可能与你的警报服务无关。
  • 伊万,谢谢你的回答。通过点击应用程序图标,我的意思是点击这个:drive.google.com/file/d/12xogGgJ0z2oMc6b9UuMEI2JhxD6ORY-g/… 换句话说,我的意思正是你写的:在设置闹钟时间后将其发送到后台并在闹钟触发时重新打开它
  • 您可以使用singleInstance启动模式,但不能保证您的应用不会被杀死以回收内存。
  • EpicPandaForce,感谢您的回答。我已经尝试过,但不幸的是,它并没有改变任何东西 - 当我重新打开应用程序时,会再次调用 onCreate 方法。应用程序没有恢复,而是再次启动,因此应用程序的两个实例并行运行。还有其他建议吗?

标签: java android android-studio


【解决方案1】:

解决方案:

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

使用以下代码取消通知您的MainActivity onDestroy Mathod 呼叫:

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

在这段代码中,通知总是使用相同的 id。如果您有不同的通知需要取消,您必须保存用于创建通知的 ID。

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>

【讨论】:

  • BADSHAH,谢谢您的回答。一旦我尝试,我会提供反馈。
  • 很高兴我能帮上忙!快乐编码
【解决方案2】:

您需要做的是将活动的启动模式指定为singleTasksingleInstance。为此,请转到您的 AndroidManifest.xml 并将 launchMode 更改/添加到您的活动中。

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

更多关于不同启动模式差异的信息在这里解释:https://developer.android.com/guide/topics/manifest/activity-element

【讨论】:

  • noahutz,谢谢您的回答。我刚刚尝试过使用 singleInstance,然后使用了 singleTask,不幸的是,行为是相同的 - 我看到再次调用了 onCreate 方法,我注意到两个应用程序实例同时运行。还有其他建议吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-10-17
  • 2014-11-17
  • 1970-01-01
  • 2017-02-10
  • 2013-08-08
相关资源
最近更新 更多