[英]How to resume activity in Android?
我正在尝试创建自己的警报 Android 应用程序。 我想在触发警报时实现 MainActivity 恢复(不再创建)。 这基本上意味着如果我设置警报并离开我的应用程序,我希望在触发警报时恢复该应用程序。
目前,我面临一个问题,当我的应用程序在后台运行并单击应用程序图标时触发警报时,会调用 onCreate 方法并且基本上两个应用程序实例同时运行(我已使用 Toast 消息来确认这一点)。 如果应用程序图标已经在后台运行,我预计单击应用程序图标会导致其恢复,但似乎并非如此。
另外,我尝试过这里解释的程序: Resume activity in Android但它对我不起作用,来自 onCreate 方法的 Toast 消息出现在屏幕上。
有人可以帮忙吗? 我真的没有想法了。 提前致谢!
解决方案:
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>
您需要做的是将活动的启动模式指定为singleTask
或singleInstance
。 为此,将 go 到您的AndroidManifest.xml
并将 launchMode 更改/添加到您的活动中。
<activity
android:name=".YourActivity"
android:label="Your Activity"
android:launchMode="singleInstance">
此处解释了有关不同启动模式差异的更多信息: https://developer.android.com/guide/topics/manifest/activity-element
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.