Android的多个实例 [英] Android Multiple Instances

查看:140
本文介绍了Android的多个实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在与Android的通知,每当我点击通知evrytime我必须再次拨打同一活动的一个问题,但据我想到了新的活动被称为但是previous也运行在后端,由于这我code被一次又一次地运行(becoz多个实例)

I am in a problem with the notifications in android, whenever I click the notification evrytime I have to call the same activity again, but as far as I am thinking the new activity is called but the previous is also running in the backend, due to which my code is running again and again ( becoz of multiple instances )

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

Please help me how to resolve or close multiple instances each time a notification is clicked.

code

公共无效notificationforChat(CharSequence的消息,字符串toJid,诠释notificationID){

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);
}

感谢

推荐答案

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

What about starting the activity in android:launchMode="singleInstance" mode or android:launchMode="singleTop" mode. I think this helps.

这篇关于Android的多个实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆