我的应用程序有通知,这是 - 很明显 - 没有任何标志,每次启动一个新的活动,所以我得到对方的顶部,这是错误的运行多个同样的活动.
我想要它做的是将通知挂起意图中指定的活动带到前面(如果它已在运行),否则启动它.
到目前为止,我所拥有的通知的意图/待定意图是
private static PendingIntent prepareIntent(Context context) {
Intent intent = new Intent(context, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
return PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
}
Run Code Online (Sandbox Code Playgroud)
奇怪的是,它有时会起作用,有时它不会......我觉得我已经尝试了每一个标志组合.
android android-intent android-activity android-pendingintent
我的应用状态栏中有通知:
Notification notification = new Notification(R.drawable.icon, null, System.currentTimeMillis());
Intent notificationIntent = new Intent(this.parent, MainActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(this.parent, 0, notificationIntent, 0);
...
notification.flags = Notification.FLAG_ONGOING_EVENT;
mNotificationManager.notify(NOTIFICATION_ID, notification);
Run Code Online (Sandbox Code Playgroud)
这个问题是当你从应用程序按下主页按钮(将其推到后台),然后按下从状态栏访问的列表中的通知,它会启动活动的新副本.我想要做的就是恢复应用程序(比如当你长按主页按钮并按下应用程序的图标时).有没有办法创建一个Intent来做到这一点?
我向用户发送推送通知,点击它时会打开应用程序.
我的问题是,当应用程序已经打开时,单击通知会再次启动应用程序.
我只希望它启动应用程序,如果它还没有运行.
我在通知中使用Pending Intent:
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(this, Splash.class), 0);
Run Code Online (Sandbox Code Playgroud)
我看到帖子说使用:
<activity
android:name=".Splash"
android:launchMode="singleTask"
Run Code Online (Sandbox Code Playgroud)
但问题是我正在运行的应用程序正在运行其他活动,然后启动应用程序启动后7秒内完成,因此当应用程序运行时,Splash不是当前活动
notifications android push-notification android-pendingintent
我有一个前台服务,只要用户登录到应用程序,就会保持与服务器的连接.这样即使用户按Home键将应用程序发送到后台,连接仍然保持活动并且可以直接从服务器接收消息.
该应用程序有许多活动,当它们被发送到后台时,其中任何活动都可以是活动的.
我想允许用户单击通知以恢复当前活动.我了解如何恢复特定活动,但想知道是否有办法恢复用户所在的最后一个活动?当然我可以跟踪最后一个,然后从Notification回调中调用它,但是认为在任务级别可能有办法?
感谢您提供的任何建议.
我需要获取启动器活动的名称才能从我的应用程序启动活动.任何解决方案
我正在尝试将我的通知编程为RESUME我的应用程序,而不是简单地启动我的应用程序的新实例...我基本上是在寻找它做同样的事情,因为长按主页按钮并恢复应用程序从那里.
这是我目前正在做的事情:
void notifyme(String string){
String ns = Context.NOTIFICATION_SERVICE;
NotificationManager mNotificationManager = (NotificationManager)
getSystemService(ns);
int icon = R.drawable.notification_icon; // icon from resources
CharSequence tickerText = string + " Program Running..."; // ticker-text
long when = System.currentTimeMillis(); // notification time
Context context = getApplicationContext(); // application Context
CharSequence contentTitle = *********; // expanded message title
CharSequence contentText = string + " Program Running...";//expanded msg text
Intent notificationIntent = new Intent(this, Main.class);
PendingIntent contentIntent = PendingIntent.getActivity(
this, 0, notificationIntent, 0);
// the …Run Code Online (Sandbox Code Playgroud) 我有一个Android应用程序,当它运行服务时,我想在状态栏上显示通知.然后用户可以通过按HOME键导航到其他应用程序.但是,当我尝试通过通知图标将之前运行的应用程序恢复到Front时,现有活动存在一些问题.即使我将其声明为"单顶"模式(我想运行现有的活动,因为有关联的服务正在运行),不知何故,在OnResume之前已经调用了活动的OnDestroy.这是我创建通知对象的代码.你能指点我错了吗?谢谢.
private void showNotification ()
{
Intent toLaunch = new Intent(getApplicationContext(),
MySingleTopActivity.class);
PendingIntent intentBack = PendingIntent.getActivity(getApplicationContext(), 0,toLaunch, PendingIntent.FLAG_UPDATE_CURRENT);
notification.setLatestEventInfo(getApplicationContext(),
getText(R.string.GPS_service_name), text, intentBack);
....
}
Run Code Online (Sandbox Code Playgroud) 我有一个基本的问题,我不认为附加任何代码片段可能会有所帮助.当我们在应用程序位于前面时按下主页按钮时,我们转到主页,应用程序失去了焦点.
现在,我的应用程序必须在状态栏中有通知图标.就像在我的应用程序中一样,有可能显示2-3个活动(在对话视图中一个在另一个上面).在某些情况下,当我按状态栏中的通知图标时,我不确定如何恢复这种状态.
但是,当我按下主页按钮然后按主屏幕中的应用程序图标时,我得到了所有内容.
那么,有什么方法可以用通知栏图标做同样的事情.
我想通过点击状态栏中的通知来打开上次启动的活动.假设我启动了活动A(我的应用程序的主要活动),此活动会向通知状态栏发送通知.活动A还打开活动B,B打开另一个活动C.从C i按主页按钮.现在我想再次访问我的应用程序,所以从通知栏我点击通知(由A发送).这里通知应该启动活动C,因为它是上次打开的.
我对此进行了搜索但未找到正确的答案.提前致谢.
当活动中发生某些事件时,我会向用户显示一条通知。创建通知时,如果系统清除了我的活动,我会使用 TaskStackBuilder 确保我有后退堆栈活动列表。我的代码看起来像这样:
...
TaskStackBuilder builder = TaskStackBuilder.create(sContext);
builder.addNextIntent(new Intent(context, ActivityA));
builder.addNextIntent(new Intent(context, ActivityB));
builder.addNextIntent(new Intent(context, ActivityC));
PendingIntent pendingIntent =
builder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT)
Notification.Builder builder = new Notification.Builder(context)
.setContentTitle(title))
.setContentText(notificationText)
.setSmallIcon(getNotificationIcon())
.setContentIntent(pendingIntent);
builder.build();
...
Run Code Online (Sandbox Code Playgroud)
因此,打开的活动将是最后添加的,即 ActivityC(我不希望重新创建它)。我找到的一个解决方案是:如何使通知恢复而不是重新创建活动? 据说我不应该使用TaskStackBuilder,但这不是我想要的。我想使用 TaskStackBuilder 从通知启动一个活动,而不是重新创建整个活动。
有什么解决方案/建议吗?
notifications android android-activity android-pendingintent taskstackbuilder
我有一个以 Activity 开头的应用程序,用于在显示主 Activity 之前加载内容。启动应用程序通常会执行MyApplication--> MyLoadingActivity--> MyMainActivity。其中MyMainActivity有一个带有 RecyclerViews 和其他东西的 ViewPager。MyMainActivity当导航到应用程序中的其他 Activity 或从其他 Activity 导航时,状态会正确保存和恢复,但从MyMainActivity通知启动时,所有状态都会被清除,因为 Android 会重新启动该 Activity。
通知是通过检查远程服务器更新的服务创建的。一旦发现更新,新数据就会存储在 SQLite 数据库中,并创建有关此的通知。当我单击此通知时,MyMainActicity已启动,但状态丢失。
按照官方指南,以下是我在服务中创建通知的方法:
NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.foo)
.setAutoCancel(true)
.setContentTitle("title")
.setContentText("text")
.setStyle(new NotificationCompat.BigTextStyle().bigText("A long text will go here if we are on Lollipop or above."));
Intent notifyIntent = new Intent(context, MyMainActivity.class);
notifyIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
PendingIntent notifyPendingIntent = PendingIntent.getActivity(context, 0, notifyIntent, PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(notifyPendingIntent);
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); …Run Code Online (Sandbox Code Playgroud) 我需要从后台服务重新启动我的主要活动.
我通过BroadcastReceiver得到了我的服务结果,我需要恢复它(在一些操作之后).
可能吗 ?
我怎样才能做到这一点 ?
我试过这个解决方案从通知恢复应用程序和堆栈,但它对我不起作用.
编辑:
那是我的接收器:
private BroadcastReceiver receiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
Bundle bundle = intent.getExtras();
if (bundle != null) {
int resultCode = bundle.getInt(DownloadService.RESULT);
if (resultCode == RESULT_OK) {
Log.i("MyApp", "RESULT OK");
final Intent mainIntent = new Intent(context, MainActivity.class);
mainIntent.setAction(Intent.ACTION_MAIN);
mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
startActivity(mainIntent);
} else {
Toast.makeText(MainActivity.this, "KO",Toast.LENGTH_LONG).show();
}
}
}
};
Run Code Online (Sandbox Code Playgroud)