博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
推送通知的两种编写方式
阅读量:2395 次
发布时间:2019-05-10

本文共 1958 字,大约阅读时间需要 6 分钟。

使用通知管理者 NotificationManager

package com.itheima.notification;import android.annotation.SuppressLint;import android.app.Activity;import android.app.Notification;import android.app.NotificationManager;import android.app.PendingIntent;import android.content.Intent;import android.graphics.BitmapFactory;import android.net.Uri;import android.os.Bundle;import android.view.View;public class MainActivity extends Activity {	@Override	protected void onCreate(Bundle savedInstanceState) {		super.onCreate(savedInstanceState);		setContentView(R.layout.activity_main);	}	public void click(View view){		//获取系统的服务  通知管理者		NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);				//第一参数  通知的图标		//第二参数  通知文本		//第三参数  通知时间		Notification notification = new Notification(R.drawable.notification, "我是一个通知", System.currentTimeMillis());				//设置点击之后会自动取消掉通知		notification.flags = Notification.FLAG_AUTO_CANCEL;				//目的,也就是点击通知之后会发生的事情		Intent intent = new Intent();		intent.setAction(Intent.ACTION_CALL);		intent.setData(Uri.parse("tel:110"));				PendingIntent contentIntent = PendingIntent.getActivity(this, 0, intent, 0);		//设置下拉通知之后显示的通知的标题和内容		notification.setLatestEventInfo(this, "我是标题", "我是内容", contentIntent);		//发一个通知   第一参数是给通知编号,方便以后删除等管理,不写就直接0		nm.notify(0, notification);	}	/**	 * 新版本的notification	 * @param view	 */	@SuppressLint("NewApi")	public void click2(View view){		//为了兼容旧的版本  这个不怎么推荐使用		 Notification noti = new Notification.Builder(this)		 //设置标题         .setContentTitle("我是标题")         //设置内容         .setContentText("我是内容")         //设置小图标         .setSmallIcon(R.drawable.notification)         //设置大图标         .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher))         .build();		 //通过管理器显示出来		 NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);		 nm.notify(0, noti);	}}

转载于:https://my.oschina.net/u/2356176/blog/420618

你可能感兴趣的文章
Flask-Migrate实现数据库迁移
查看>>
su: cannot set user id: Resource temporarily unavailable
查看>>
SSHException: Incompatible ssh peer (no acceptable kex algorithm)
查看>>
shell切换用户
查看>>
session机制详解
查看>>
《算法导论》学习总结——第二部分1堆排序
查看>>
linux下进程的一些总结
查看>>
强大的g++呢还是强大的C++?太假了吧
查看>>
C++中的内联函数inline总结
查看>>
C++中的函数指针的一些总结
查看>>
ubuntu下为postgresql添加ODBC驱动过程
查看>>
linux下的su,su -,以及cd,cd - ,cd ~总结
查看>>
Argument of type '(Foo::)(int,int)' does not match 'void (*)(int,int)'以及静态函数问题
查看>>
今天遇到的postgresql中的备份和恢复
查看>>
今天又搞到个libDTL.so is not an ELF file - it has the wrong magic bytes at the start.
查看>>
MinGW和vc6中编译DTL的过程
查看>>
Fedora13下为postgresql添加ODBC驱动过程
查看>>
Bridge模式学习
查看>>
Virtual的一些总结
查看>>
Fedora13上折腾了下ACE
查看>>