开发者

How can I bring an activity to the front from a background service

First of all, I am aware my issue is against the philosophy of Android, but I have no choice, this application will run on a embedded car gps and I need to bring an activity to prevent from car accident, for example, when it's happen around the user. I have to put other activity on the back and bring my alert pop up without user manipulation like notification on the front.

Is there a way to bring manually an activity to the front, by resuming it like when you click on the a开发者_运维百科ndroid task switcher?


Call getApplicationContext() in your Service which will give you a Context and then launch the target Activity as usual.


This should really be a comment on Alex's answer but that is not possible for some reason.

You don't have to use getApplicationContext() at all as your Service is a Context.

Intent intent = new Intent(this, MainActivity.class);


Its better to use pendintIntent because using the FLAG_ACTIVITY_NEW_TASK is slow and ugly.

Context context = getApplicationContext();
        Intent intent = new Intent(context, MainActivity.class);
        PendingIntent contentIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
        try {
            contentIntent.send();
        } catch (PendingIntent.CanceledException e) {
            e.printStackTrace();
        }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜