gpt4 book ai didi

Android Phonegap 应用程序 - 将 Activity 放在首位

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

我正在自学如何在 Android 上使用 Phonegap 通知。虽然显示通知似乎是一个相当简单的过程

public void triggerTestNotification(String tag, int id,Context ctxt) 
{
Intent notificationIntent = new Intent(context, PallActivity.class);
notificationIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
Intent.FLAG_ACTIVITY_CLEAR_TOP);

notificationIntent.setAction(Intent.ACTION_MAIN);
notificationIntent.addCategory(Intent.CATEGORY_LAUNCHER);

PendingIntent contentIntent = PendingIntent.getActivity(ctxt, 0,
notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);

Notification not = new Notification.Builder(ctxt)
.setContentTitle("Title").setContentText("Title")
.setTicker("Tick tock")
.setAutoCancel(true)
.setContentIntent(contentIntent)
.setSmallIcon(ctxt.getApplicationInfo().icon).build();
NotificationManager notificationManager = (NotificationManager)
ctxt.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(tag, id, not);
}

我发现比较困难的是以下内容

  • 用户启动应用
  • 用户离开并开始做其他事情 - 应用在后台运行
  • 收到通知
  • 显示
  • 用户点击通知。

此时应用程序应该回到前台。我以为我的 Intent 代码

public class PallActivity extends Activity
{
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
finish();
forceMainActivityReload();
}

private void forceMainActivityReload()
{
PackageManager pm = getPackageManager();
Intent launchIntent =
pm.getLaunchIntentForPackage(getApplicationContext().getPackageName());
startActivity(launchIntent);
}

@Override
protected void onResume()
{
super.onResume();
final NotificationManager notificationManager = (NotificationManager)
this.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.cancelAll();
}

}

会处理这个,但它什么都不做。显然,我在这里弄错了——也许是在 Intent 标志中。如果有人能帮助我走上正轨,我将不胜感激

最佳答案

从您的 oncreate 中删除 finish();,它会在您继续任何操作之前调用 finish。

不是启动 PallActivity,似乎您打算启动第二个 Activity 或至少将 PallActivity 视为第二个 Activity 。您不需要像在 forceMainActivityReload(); 中那样启动它。我将从通知 Intent 开始您希望开始的 Activity ,而不是让它变得更加复杂和混淆问题。

notification.flags = Notification.FLAG_ONGOING_EVENT | Notification.FLAG_NO_CLEAR;   
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);

确保您使用的上下文是:

Context context = getApplicationContext();

并且,如评论中所述,从通知中删除这些内容:

notificationIntent.setAction(Intent.ACTION_MAIN);
notificationIntent.addCategory(Intent.CATEGORY_LAUNCHER);

如果需要,这里有更多详细信息:

Resume an activity when clicked on a notification

Android: How to resume an App from a Notification?

resuming an activity from a notification

Notification Resume Activity

Intent to resume a previously paused activity (Called from a Notification)

Android: resume app from previous position

How to make notification intent resume rather than making a new intent?

关于Android Phonegap 应用程序 - 将 Activity 放在首位,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37099732/

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