µ±Ç°Î»Ö㺠´úÂëÃÔ >> Android >> Androidѧϰ֮·(1)֪ͨNotification
  Ïêϸ½â¾ö·½°¸

Androidѧϰ֮·(1)֪ͨNotification

Èȶȣº17   ·¢²¼Ê±¼ä£º2016-05-01 14:19:34.0
Androidѧϰ֮·(һ)֪ͨNotification

ÕâÊǵ÷ÓÃ֪ͨµÄº¯Êý

?private void showNotification(PlayWarper mPlayWarper) {
??????? mNotificationManager = (NotificationManager) this.getSystemService(NOTIFICATION_SERVICE);
??????? intent = new Intent(mContext, FileListLocalActivity.class);

????????pendingIntent = PendingIntent.getActivity(mContext, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
??????? RemoteViews rv = new RemoteViews(getPackageName(), R.layout.notice);
??????? String name = mPlayWarper.musicInfo.getName();
??mNotification = new Notification(android.R.drawable.ic_media_play, name, System.currentTimeMillis());

??????? mNotification.contentIntent = pendingIntent;
??????? mNotification.contentView = rv;
??????? // mNotification.setLatestEventInfo(this, "ÕýÔÚ²¥·Å£º" + name, name,
??????? // pendingIntent);
???????? mNotification.flags |= Notification.FLAG_ONGOING_EVENT; //
??????? // ½«´Ë֪ͨ·Åµ½Í¨ÖªÀ¸µÄ"Ongoing"¼´"ÕýÔÚÔËÐÐ"×éÖÐ
??????? mNotification.flags |= Notification.FLAG_NO_CLEAR; //
??????? mNotification.flags |= Intent.FLAG_ACTIVITY_SINGLE_TOP;

??????? // ±íÃ÷ÔÚµã»÷ÁË֪ͨÀ¸ÖеÄ"Çå³ý֪ͨ"ºó£¬´Ë֪ͨ²»Çå³ý£¬¾­³£ÓëFLAG_ONGOING_EVENTÒ»ÆðʹÓÃ
?????????mNotificationManager.notify(Constant.NOTICE_ID, mNotification);
??? }

?

?

¿ª·¢NotificationÓöµ½ÎÊÌâ

?

1,µã»÷֪ͨÎÞActivity·µ»Ø,ÈçͬûÓеã»÷(ÓÐЩÈ˵¹ÏëʵÏÖÕâÑùµÄЧ¹û)

Õâ¸öÎÊÌâºÜ¶þ,¹þ¹þ,Ô­Òò¾ÍÊÇAndroidManifest.xmlûÓж¨ÒåNoticeActivity.

?ÕâÐдúÂë¾ö¶¨µã»÷֪ͨºóÌøתµ½ÄĸöActivity

intent = new Intent(mContext, FileListLocalActivity.class);

?

2,µã»÷֪ͨºóÓÐActivityÁË,µ«ÊÇÔٴεã»÷ÎÞÈκβÙ×÷.

Õâ¸öÔ­ÒòÊÇ´´½¨PendingIntentʱ×îºóÒ»¸ö²ÎÊýflag¶¨Òå²»ÕýÈ·,Ó¦¸ÃÊÇ

PendingIntent.getActivity(mContext, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
FLAG_ONE_SHOTÊÇÖ»Äܵã»÷֪ͨһ´Î

FLAG_ONE_SHOT, FLAG_NO_CREATE, FLAG_CANCEL_CURRENT, FLAG_UPDATE_CURRENT,

?

3,֪ͨÄÚÈݲ»ÊǶ¨ÒåµÄlayout,¶øÊÇÎÄ×ÖÐÅÏ¢

?// mNotification.setLatestEventInfo(this, "ÕýÔÚ²¥·Å£º" + name, name,
??????? // pendingIntent);
×¢Ê͵ôÕâÑùµÄ´úÂë¾Í¿ÉÒÔÏÔʾR.layout.noticeµÄlayoutµ½Í¨ÖªÀïÃæ.

?

4,µã»÷֪ͨºóÿ´Î¶¼Ð½¨Ò»¸öActivity,²»ÄܰѺǫ́µÄActivityÖ±½ÓÏÔʾµ½Ç°Ì¨

?½â¾ö·½·¨ÊÇÐ޸ĵã»÷ºó³öÏÖµÄActivityÔÚAndroidManifest.xmlÖмÓÉÏlaunchMode,´úÂëÈçÏÂ:

???? <activity
??????????? android:name=".FileListLocalActivity"
??????????? android:launchMode="singleTask" />

?

launchModeµÄ½âÊÍÏà¼ûSDKÎĵµhttp://developer.android.com/reference/android/R.attr.html#launchMode

?

  Ïà¹Ø½â¾ö·½°¸