开发者

start an application from notification bar in android

I have an application, I want to show my app icon to the notification bar when my a开发者_如何学运维pplication is running and i also want when user will click on my app icon present in the notification bar my app will be open. How to do this? Please Help!


The accepted answer is deprecated. Here is the way to show a dialog, from google's documentation.

NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this).setSmallIcon(R.drawable
            .logo_listy).setContentTitle("My notification").setContentText("Hello World!");

    Intent resultIntent = new Intent(this, ResultActivity.class);
    TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
    stackBuilder.addParentStack(ResultActivity.class);
    stackBuilder.addNextIntent(resultIntent);
    PendingIntent resultPendingIntent =
            stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
    mBuilder.setContentIntent(resultPendingIntent);

    NotificationManager mNotificationManager =
            (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    mNotificationManager.notify(0, mBuilder.build());


Few suggestions:

  • if you want icon in the notification bar, you must send some notification.
  • Application cannot be started by clicking on the notification icon. It may be started by clicking to the notification, that will be available if user pull-down notification bar. For that purpose you need to create PendingIntent.


You need to post a notification with a pending intent which contains an intent to start your app. See http://developer.android.com/guide/topics/ui/notifiers/notifications.html for how to do it in general and http://javablogs.com/Jump.action?id=628173 for a trap you may fall into.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜