Notification通知
来源:互联网 发布:迅游加速器mac版 编辑:程序博客网 时间:2024/06/04 19:34
notification就是在我们手机通知栏里面显示的,一般是推送。很多app都有推送功能,那么他们的推送是怎么来的呢,首先他们得有一个后台服务在,并且在你连上互联网的时候,他给他的服务器发送一个请求,然后收到一些数据,以notification显示通知,你点开后在请求全部数据。这里我只说说notification。notification你可以使用系统自带的,也可以使用自定义的。对于notification,系统自带的就可以解决很多问题了。但是有时候还是需要自定义。用法很简单,部分说明代码里面有,如果需要更详细的了解他,可以查看api。下面是代码
public class MainActivity extends AppCompatActivity implements View.OnClickListener{ public Button button1; public Button button2; public NotificationManager nm=null; public Notification n; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); nm= (NotificationManager) getSystemService(NOTIFICATION_SERVICE); button1= (Button) findViewById(R.id.button1); button2= (Button) findViewById(R.id.button2); button1.setOnClickListener(this); button2.setOnClickListener(this); } public void show(){ //系统自带的通知 Notification.Builder builder=new Notification.Builder(this); builder.setContentText("这是标题"); builder.setContentText("这是内容"); builder.setSmallIcon(R.mipmap.ic_launcher);//设置图标 builder.setWhen(System.currentTimeMillis());//设置显示通知的时间 //设置大标题,参数真是Bitmap所以将res里的图片转化成Bitmap builder.setLargeIcon(BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher)); builder.setAutoCancel(true);//设置可以被取消 builder.setDefaults(Notification.DEFAULT_SOUND);// Notification.DEFAULT_VIBRATE(添加默认震动提醒);// Notification.DEFAULT_SOUND(添加默认声音提醒);// Notification.DEFAULT_LIGHTS(添加默认三色灯提醒)// Notification.DEFAULT_ALL(添加默认以上3种全部提醒) //部分需要权限。 PendingIntent pendingIntent=PendingIntent.getActivity(this,0,new Intent(this,MainActivity.class),0); builder.setContentIntent(pendingIntent);//设置响应 n=builder.build(); nm.notify(1,n); } public void myshow(){ //自定义通知 Notification.Builder builder=new Notification.Builder(this); builder.setSmallIcon(R.mipmap.ic_launcher); RemoteViews remoteViews=new RemoteViews(getPackageName(),R.layout.view_notification); builder.setContent(remoteViews); remoteViews.setTextViewText(R.id.te,"这是一个自定义标题"); //事件类型只能为PendingIntent PendingIntent pendingIntent=PendingIntent.getActivity(this,0,new Intent(this,MainActivity.class),0); remoteViews.setOnClickPendingIntent(R.id.bu,pendingIntent); n=builder.build(); nm.notify(2,n); } @Override public void onClick(View v) { switch (v.getId()){ case R.id.button1: show(); break; case R.id.button2: myshow(); break; } }}
自定义布局。
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:id="@+id/te" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <Button android:id="@+id/bu" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="按钮"/></LinearLayout>
阅读全文
0 0
- Notification通知
- Notification通知
- Notification(通知)
- Notification通知
- Notification通知
- notification通知
- Notification 通知
- 通知(Notification)
- Notification通知
- 通知notification
- 通知Notification
- 通知 ------- Notification
- 通知Notification
- Notification--通知
- Notification通知
- 通知 notification
- 通知Notification
- Notification通知
- codeforces 583A Asphalting Roads
- iOS懒加载不执行的坑
- LeetCode之路:543. Diameter of Binary Tree
- 求素数及素数表的获取
- parse(),stringify()
- Notification通知
- 使用nose组织自动化测试用例(1)
- uva10305--拓扑
- Unity Android 支付宝 接入
- 读书笔记之Modernizr
- java判断字符串相等
- String类——练习(1)
- spring 嵌套事务(Nested Transaction) 和新建事务测试
- jquery获得select option的值 和对select option的操作