2014-03-26 128 views
0

我正在使用此應用程序。我想要做的是在點擊按鈕的通知欄中顯示我的活動圖標。我希望它永久停留在那裏,直到其他事件完成。目前我沒有抓住過這樣的代碼,但它給予了很多的錯誤,請告訴我一個簡單的方法來做到這一點:在通知欄中顯示應用程序圖標

save.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       if(icon.isChecked()) { 
        CharSequence text = getText(R.string.local_service_started); 

        // Set the icon, scrolling text and timestamp 
        Notification notification = new Notification(R.drawable.ic_launcher, "Track Your Life", System.currentTimeMillis()); 

        // The PendingIntent to launch our activity if the user selects this notification 
        PendingIntent contentIntent = PendingIntent.getActivity(this, 0, 
          new Intent(), 0); 

        // Set the info for the views that show in the notification panel. 
        notification.setLatestEventInfo(this, getText(R.string.local_service_label), text, contentIntent); 

        // Send the notification. 
        mNM.notify(NOTIFICATION, notification); 
       } 

      } 
     }); 
+0

添加打印屏幕要什麼實現 –

+0

我只想讓我的應用圖標留在通知欄中。就像在線時的WhatsApp或Skype圖標一樣。 –

+0

請完整,準確地解釋**,「很多錯誤」的含義。此外,您可能希望使用'Notification.Builder'或'NotificationCompat.Builder',而不是直接嘗試自己創建'Notification'。 – CommonsWare

回答

0
與圖標創建通知

簡單的代碼

Intent intentSetDefault = new Intent(mContext, ConversationListActivity.class); 
     PendingIntent piSetDeafult = PendingIntent.getActivity(mContext, 0, intentSetDefault, 
       PendingIntent.FLAG_UPDATE_CURRENT); 
     // create pending intent for action discard 
     Intent intentdiscard = new Intent(mContext, NotificationReceiver.class).setType(CODE_DISCARD); 
     PendingIntent piDiscard = PendingIntent.getBroadcast(mContext, 0, intentdiscard, 
       PendingIntent.FLAG_UPDATE_CURRENT); 

    Notification.Builder mBuilder = 
        new Notification.Builder(mContext) 
          .setTicker(title) 
          .setSmallIcon(R.drawable.ic_stat_notify_incoming_msg) 
          .setContentTitle(title) 
          .setContentText(text) 
          .setLargeIcon(mAppBitmap) 
          .setContentInfo(Integer.toString(smsCount)) 
          .setPriority(Notification.PRIORITY_MAX) 
          .setDefaults(Notification.DEFAULT_ALL) 
          .setAutoCancel(false); 

      mBuilder.setContentIntent(piSetDeafult); 

      Notification notification = mBuilder.build(); 
      notification.flags = Notification.FLAG_NO_CLEAR | Notification.FLAG_ONGOING_EVENT; 

      mNotifyMgr.notify(NOTIFICATION_SET_DEFAULT, notification); 
+0

找不到mContext,piSetDefault,mNotifyMgr –

+0

添加成員上下文是簡單的上下文,並且notifyMgr是通知管理器 –

相關問題