安卓Phonegap应用 - 将活动置于前台

4

我正在学习如何在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);
 }

我发现更困难的是以下内容:

  • 用户启动应用程序
  • 用户离开并开始做其他事情-应用程序被放置在后台
  • 通知到来
  • 显示通知
  • 用户点击通知。

此时应用程序应该回到前台。我认为我的意图代码

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 - Boris Strandjev
我在那里放置了 finish(),因为我在另一个插件中看到了它。然而,我已经尝试将我的代码与 finish() 移动到 onCreate 底部以及完全删除它。应用程序仍然无法返回前台。 - DroidOS
1
你能否说明一下Pall活动的目的 - 我的意思是它立即重定向到您应用程序的启动器活动。PallActivity不是您的启动器活动吗?您能包含您的清单文件吗? - Boris Strandjev
首先使用NotificationCompat.Builder,并从您的notificationIntent中删除以下内容:notificationIntent.setAction(Intent.ACTION_MAIN); notificationIntent.addCategory(Intent.CATEGORY_LAUNCHER); - Ankit Aggarwal
1个回答

2

从你的oncreate中删除finish();,否则它会在你到达任何地方之前调用finish。

不要从PallActivity开始,它似乎你打算启动第二个活动或至少将PallActivity视为第二个活动。你不需要从forceMainActivityReload();中这样启动它。我会从通知意图中启动你想要启动的活动,而不是使问题更复杂和混淆。

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:如何从通知中恢复应用程序?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:从以前的位置恢复应用程序:Android: resume app from previous position 如何使通知意图恢复而不是创建新意图?How to make notification intent resume rather than making a new intent?

1
谢谢,Yvette - 你提供的有用链接范围特别受到赞赏。 - DroidOS

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接