国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 學(xué)院 > 開(kāi)發(fā)設(shè)計(jì) > 正文

程序通知欄通知的開(kāi)發(fā)

2019-11-08 00:30:32
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

原理:

請(qǐng)求系統(tǒng)發(fā)送一個(gè)廣播,在本程序接收該廣播,并做一系列的操作,屬于跨進(jìn)程的開(kāi)發(fā)

1、點(diǎn)擊控件請(qǐng)求系統(tǒng)發(fā)送廣播

public void doClick(View v) {switch (v.getId()) {case R.id.btn_notification:sendNotification();// 發(fā)送通知break;

}

2、在通知欄需要顯示的通知樣式

/*** 自定義*/PRivate void sendNotificationZidinyi() {NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);Builder builder = new Builder(MainActivity.this);RemoteViews views = new RemoteViews(getPackageName(), R.layout.notification_zidinyi);//給RemoteViews添加點(diǎn)擊意圖Intent activityIntent = new Intent(MainActivity.this,MainActivity.class);PendingIntent pi5 = PendingIntent.getActivity(MainActivity.this, 0, activityIntent, PendingIntent.FLAG_UPDATE_CURRENT);views.setOnClickPendingIntent(R.id.btn_startActivity, pi5);//給播放按鈕添加點(diǎn)擊意圖Intent broadIntent = new Intent("ACTION_SEND_BROADCAST");PendingIntent broadCastPi = PendingIntent.getBroadcast(MainActivity.this, 0, broadIntent , Context.BIND_DEBUG_UNBIND);views.setOnClickPendingIntent(R.id.img_player, broadCastPi);builder.setContentTitle("").setTicker("自定義").setSmallIcon(R.drawable.small_pic).setContent(views );Notification notifi = builder.build();manager.notify(555,notifi);}

3、動(dòng)態(tài)注冊(cè)廣播接收者

@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);receiver = new PlayerBroadcastReceiver();IntentFilter filter = new IntentFilter();filter.addAction("ACTION_SEND_BROADCAST");registerReceiver(receiver, filter);}

4、寫一個(gè)清除通知的方法,在其生命周期結(jié)束時(shí)自行清除

private void clearNotification() {// 單例系統(tǒng)服務(wù),可重復(fù)創(chuàng)建NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);manager.cancel(NOTIFI_MANAGER_ID);}

@Overrideprotected void onDestroy() {// TODO Auto-generated method stubisDoing=false;NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);manager.cancel(588);unregisterReceiver(receiver);super.onDestroy();}


發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 汝州市| 泉州市| 巫山县| 九龙县| 越西县| 鹤峰县| 青冈县| 调兵山市| 台前县| 额敏县| 临清市| 抚顺县| 河池市| 洛扎县| 绥化市| 宜丰县| 新野县| 盐源县| 凯里市| 丹棱县| 通许县| 宁强县| 平泉县| 柳河县| 依兰县| 阿克| 米林县| 咸宁市| 卓尼县| 平原县| 兴宁市| 华亭县| 曲沃县| 灌阳县| 丹凤县| 峨眉山市| 庆云县| 唐山市| 青铜峡市| 西充县| 新民市|