开始从安卓通知栏的应用程序 [英] start an application from notification bar in android

查看:110
本文介绍了开始从安卓通知栏的应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序,我想告诉我的应用程序图标,通知栏时,我的应用程序正在运行,我也希望当用户点击我的应用程序图标present在通知栏 我的应用程序将被打开。如何做到这一点?请帮助!

I have an application, I want to show my app icon to the notification bar when my application 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!

推荐答案

要创建一个状态栏通知,这样在您的onCreate方法:

To create a status bar notification, do this in your onCreate Method:

  1. 获取一个参考NotificationManager:

  1. Get a reference to the NotificationManager:

  String ns = Context.NOTIFICATION_SERVICE;
  NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);

  • 实例化的通知:

  • Instantiate the Notification:

      int icon = R.drawable.notification_icon;
      CharSequence tickerText = "Hello";
      long when = System.currentTimeMillis();
    
      Notification notification = new Notification(icon, tickerText, when);
    

  • 定义通知的扩展信息和意图:

  • Define the Notification's expanded message and Intent:

      Context context = getApplicationContext();
      CharSequence contentTitle = "My notification";
      CharSequence contentText = "Hello World!";
      Intent notificationIntent = new Intent(this, MyClass.class);
      PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
    
      notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
    

  • 通过通知到NotificationManager:

  • Pass the Notification to the NotificationManager:

      private static final int HELLO_ID = 1;
    
      mNotificationManager.notify(HELLO_ID, notification);
    

    就是这样。您的用户现在已经收到通知。

    That's it. Your user has now been notified.

    这篇关于开始从安卓通知栏的应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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