how to awake already running app after c2dm message on android

爱⌒轻易说出口 提交于 2020-01-07 09:45:48

问题


is there a way to awake already running app from the notification bar after c2dm message? i have this app that is registered with c2dm servers that receives push notifications from my server to do some processing. so after i receive c2dm message from my server, it displays the status bar notification to the user, user expands the notifications and clicks on my app, brings it up.

all is good but if this app was already running before (stared from the icon) this would load another instance of my app into memory. also some of the things are crashing in it. i already changed the android:launchMode="singleTop" on all my activities, i tried using intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP) in my notification and no luck. i always end up with 2 apps running.

any help is appreciated

here my static function that i use to create a notification after i receive the c2dm message:

public static void notifyStart(Context context, String notificationText) {      
        //notification
        String ns = Context.NOTIFICATION_SERVICE;
        NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(ns);

        int icon = R.drawable.icon_notify;
        CharSequence tickerText = notificationText;
        long when = System.currentTimeMillis();

        Notification notification = new Notification(icon, tickerText, when);              
        notification.ledARGB = 0xff00ff00;
        notification.ledOnMS = 400;
        notification.ledOffMS = 400;
        notification.flags |= Notification.FLAG_SHOW_LIGHTS;        
        notification.defaults |= Notification.DEFAULT_SOUND;   

        CharSequence contentTitle = "App Name";
        CharSequence contentText = notificationText;

        Intent notificationIntent = new Intent(context, home.class);
        notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

        PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);

        notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);   

        mNotificationManager.notify(1350, notification);          
    }

and here is my home activity:

    <activity android:name=".home"
              android:label="@string/app_name"
              android:theme="@android:style/Theme.NoTitleBar"
              android:launchMode="singleTop" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

回答1:


guys, i am very sorry but it was my fault :| couple of days ago i decided to change the name of the package of my app and forgot to remove the old app from the phone. i didn't realize that it would cause two separate installations on my phone, the task manager displays only app name so it did look like there were two instances of the same app, in fact there was one instance of each ;) sorry about trouble and appreciate your willingness to help :)




回答2:


Android documentation says:

As shown in the table below, the modes fall into two main groups, with "standard" and "singleTop" activities on one side, and "singleTask" and "singleInstance" activities on the other. An activity with the "standard" or "singleTop" launch mode can be instantiated multiple times. The instances can belong to any task and can be located anywhere in the activity stack. Typically, they're launched into the task that called startActivity() (unless the Intent object contains a FLAG_ACTIVITY_NEW_TASK instruction, in which case a different task is chosen — see the taskAffinity attribute). In contrast, "singleTask" and "singleInstance" activities can only begin a task. They are always at the root of the activity stack. Moreover, the device can hold only one instance of the activity at a time — only one such task.

so it seems (though I haven't checked) that singleInstance is what you're looking for.

see: http://bit.ly/gH8SBb



来源:https://stackoverflow.com/questions/5240372/how-to-awake-already-running-app-after-c2dm-message-on-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!