Android Notification显示通知的两种技巧
发布时间:2021-12-16 12:39:35 所属栏目:教程 来源:互联网
导读:第一种显示通知的方法: /** * notification * * @param id */ private void showNotification() { RemoteViews views = new RemoteViews(getPackageName(), R.layout.statusbar); views.setImageViewResource(R.id.icon, R.drawable.fm_icon); views.setTex
第一种显示通知的方法: /** * notification * * @param id */ private void showNotification() { RemoteViews views = new RemoteViews(getPackageName(), R.layout.statusbar); views.setImageViewResource(R.id.icon, R.drawable.fm_icon); views.setTextViewText(R.id.fm_run, getString(R.string.fm_run)); Notification status = new Notification(); status.contentView = views; status.flags |= Notification.FLAG_ONGOING_EVENT; status.icon = R.drawable.ic_fm_status_bar; status.contentIntent = PendingIntent.getActivity(this, 0, new Intent(this, FMEntryView.class).addFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT), 0); startForeground(PLAYBACKSERVICE_STATUS, status); } 删除通知: /** * delete notification */ private void deleteNotification() { NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); nm.cancel(PLAYBACKSERVICE_STATUS); stopForeground(true); } 第二种显示通知的方法: public void showNotification(int icon,String tickertext,String title,String content){ //设置一个唯一的ID,随便设置 //Notification管理器 Notification notification=new Notification(icon,tickertext,System.currentTimeMillis()); //后面的参数分别是显示在顶部通知栏的小图标,小图标旁的文字(短暂显示,自动消失)系统当前时间(不明白这个有什么用) notification.defaults=Notification.DEFAULT_ALL; //这是设置通知是否同时播放声音或振动,声音为Notification.DEFAULT_SOUND //振动为Notification.DEFAULT_VIBRATE; //Light为Notification.DEFAULT_LIGHTS,在我的Milestone上好像没什么反应 //全部为Notification.DEFAULT_ALL //如果是振动或者全部,必须在AndroidManifest.xml加入振动权限 PendingIntent pt=PendingIntent.getActivity(this, 0, new Intent(this,main.class), 0); //点击通知后的动作,这里是转回main 这个Acticity notification.setLatestEventInfo(this,title,content,pt); nm.notify(notification_id, notification); } //showNotification(R.drawable.home,"图标边的文字","标题","内容"); 删除通知: nm.cancel(notification_id); 上面此法别忘了增加权限: <-permission android:name="android.permission.VIBRATE" /> <!-- 允许振动 --> ![]() (编辑:我爱制作网_潮州站长网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |