鱼C论坛

 找回密码
 立即注册
查看: 2562|回复: 0

[学习笔记] android prgraming 8.2.1

[复制链接]
发表于 2017-10-27 08:56:03 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
新建个项目,修改activity_main.xml
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.     xmlns:app="http://schemas.android.com/apk/res-auto"
  4.     xmlns:tools="http://schemas.android.com/tools"
  5.     android:layout_width="match_parent"
  6.     android:layout_height="match_parent"
  7.     tools:context="com.example.xinwei.notificationtest.MainActivity">

  8.     <Button
  9.         android:id="@+id/send_notice"
  10.         android:layout_width="wrap_content"
  11.         android:layout_height="wrap_content"
  12.         android:text="send notice"/>

  13. </android.support.constraint.ConstraintLayout>
复制代码

修改mainactivity.java
  1. package com.example.xinwei.notificationtest;

  2. import android.app.Notification;
  3. import android.app.NotificationManager;
  4. import android.app.PendingIntent;
  5. import android.content.Intent;
  6. import android.graphics.BitmapFactory;
  7. import android.support.v7.app.AppCompatActivity;
  8. import android.os.Bundle;
  9. import android.support.v7.app.NotificationCompat;
  10. import android.view.View;
  11. import android.widget.Button;

  12. public class MainActivity extends AppCompatActivity implements View.OnClickListener{

  13.     @Override
  14.     protected void onCreate(Bundle savedInstanceState) {
  15.         super.onCreate(savedInstanceState);
  16.         setContentView(R.layout.activity_main);
  17.         Button sendNotice=(Button)findViewById(R.id.send_notice);
  18.         sendNotice.setOnClickListener(this);
  19.     }

  20.     @Override
  21.     public void onClick(View view) {
  22.         Intent[] intent=new android.content.Intent[]{new Intent(this,NotificationActivity.class)};
  23.         PendingIntent pi= PendingIntent.getActivities(this,0,intent,0);
  24.         NotificationManager manager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
  25.         Notification notification=new NotificationCompat.Builder(this)
  26.                 .setContentTitle("This is content icon")
  27.                 .setContentText("This is content text")
  28.                 .setWhen(System.currentTimeMillis())
  29.                 .setSmallIcon(R.mipmap.ic_launcher)
  30.                 .setLargeIcon(BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher))
  31.                 .setContentIntent(pi)
  32.                 .setAutoCancel(true)
  33.                 .build();
  34.         manager.notify(1,notification);

  35.     }
  36. }
复制代码

再新建个NotificationActivity.java的空活动,把布局文件命名为notification_layout.xml,然后把布局文件修改为
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.     xmlns:app="http://schemas.android.com/apk/res-auto"
  4.     xmlns:tools="http://schemas.android.com/tools"
  5.     android:layout_width="match_parent"
  6.     android:layout_height="match_parent"
  7.     tools:context="com.example.xinwei.notificationtest.NotificationActivity">
  8.     <TextView
  9.         android:layout_width="wrap_content"
  10.         android:layout_height="wrap_content"
  11.         android:text="This is notification layout"
  12.         android:textSize="24sp"/>
  13. </android.support.constraint.ConstraintLayout>
复制代码

效果图:
jdfw.gif

本帖被以下淘专辑推荐:

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-24 12:11

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表