【问题标题】:Android Multiple InstancesAndroid 多实例
【发布时间】:2016-03-03 20:13:27
【问题描述】:

我在android中的通知有问题,每当我每次点击通知时,我都必须再次调用相同的活动,但据我认为新活动被调用但前一个也在后端运行,因此我的代码一次又一次地运行(因为多个实例)

请帮助我每次点击通知时如何解决或关闭多个实例。

代码

public void notificationforChat(CharSequence message,String toJid, int notificationID) {

    int notificationCount = 1;
    String ns = Context.NOTIFICATION_SERVICE;
    NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);
    int icon = R.drawable.ic_launcher;
    CharSequence tickerText = message;
    long when = System.currentTimeMillis();
    Notification notification = new Notification(icon, tickerText, when);
    //notification.number = notificationCount++;
    Context context = getApplicationContext();



    CharSequence contentTitle = "Chat";
    CharSequence contentText = message;
    Intent notificationIntentforChat = new Intent(this, UserChatActivity.class);
    notificationIntentforChat.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);

    notificationIntentforChat.putExtra("userNameVal", toJid);       
    PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
            notificationIntentforChat, PendingIntent.FLAG_UPDATE_CURRENT);
    notification.setLatestEventInfo(context, contentTitle, contentText,
            contentIntent);
    notification.flags |= Notification.FLAG_AUTO_CANCEL;
    notification.defaults = Notification.DEFAULT_ALL;   
    mNotificationManager.notify(notificationID, notification);
}

谢谢

【问题讨论】:

  • 您是指仅在您的应用中的单个活动?
  • 是的。假设您在您的 gtalk 聊天屏幕上,正在与其他人聊天,现在如果另一个人 ping 您,那么您将通过通知收到通知,现在当您单击该通知时,其他人的聊天屏幕将打开。现在这里我前一个人的聊天画面也在后台运行

标签: android notifications multiple-instances


【解决方案1】:

将该活动的以下代码放在 maifest 中:

android:launchMode="singleTop"

然后为了在再次调用活动时处理新的更改,请覆盖以下方法。

@Override
protected void onNewIntent(Intent intent) {
    super.onNewIntent(intent);      
    //Do your new intent handling here.     
}

您放入清单中的代码确保只创建该活动的一个实例。您可以在该活动的 onNewIntent 覆盖方法中处理新意图所需的更改。

【讨论】:

  • 问题是当我打开新窗口时,同一个屏幕正在打开,新屏幕没有打开。有什么问题吗??
【解决方案2】:

为了防止多个活动实例。

您可以在活动中使用android:launchMode="singleTask"

<activity
            android:launchMode="singleTask"
            android:name=".UserChatActivity"
            android:label="Your title" > 

....
</activity>

一旦你调用它。 onNewIntent(Intent) 将被新意图触发

    @Override
    protected void onNewIntent(Intent intent) {

       super.onNewIntent(intent);      


    }

【讨论】:

  • 问题是当我打开新窗口时,同一个屏幕正在打开,新屏幕没有打开。有什么问题吗??
  • 就是这样,我告诉你使用覆盖的 onNewIntent 方法。在该方法中,使用您需要显示的新值更新视图。
【解决方案3】:

android:launchMode="singleInstance" 模式或android:launchMode="singleTop" 模式启动活动怎么样。我认为这会有所帮助。

【讨论】:

    【解决方案4】:

    添加到您的呼叫意图

    mIntent. setFlags (Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
    

    添加到清单中的活动

    android:launchMode="singleTop"
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多