Android之开发消息通知栏

一:先来效果图

Android之开发消息通知栏_第1张图片

Android之开发消息通知栏_第2张图片

Android之开发消息通知栏_第3张图片

二:实现步骤

1.xml布局实现



 

2.activity的实现

package edu.feicui.notification;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.RemoteViews;
import butterknife.ButterKnife;
import butterknife.OnClick;
public class MainActivity extends AppCompatActivity {
 /**
 * 通知栏Notification
 */
 private NotificationManager mManager;
 private Notification mNotification;
 private PendingIntent mIntent;
 private String cll;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 cll = "今年27号过年哟!";
 ButterKnife.bind(this);
 }
 @Override
 public void onContentChanged() {
 super.onContentChanged();
 init();
 }
 private void init() {
 //初始化通知栏管理者
 mManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

 //意图数组
 Intent[] intents = {new Intent(this, NotificationAcitivity.class)};
 //待处理意图对象
 mIntent = PendingIntent.getActivities(this, 0, intents, 0);
 //消息栏通知对象
 mNotification = new Notification();
 }
 @OnClick(R.id.btn_create)
 public void create() {
 //设置在通知栏的消息图标
 mNotification.icon = R.mipmap.logo_new;
 //设置在通知栏的信息内容
 mNotification.tickerText = "重大消息";
 //设置默认的声音,此外还可以设置震动(需加入权限)
 mNotification.defaults = Notification.DEFAULT_SOUND;
 //添加灯光
// mNotification.defaults=Notification.DEFAULT_LIGHTS;
 //不能删除
 mNotification.flags = Notification.FLAG_NO_CLEAR;
 //设置下拉时的显示布局
 RemoteViews convertView = new RemoteViews(getPackageName(), R.layout.layout_content);
 convertView.setImageViewResource(R.id.img, R.mipmap.logo_new);
 convertView.setTextViewText(R.id.txt, cll);
 mNotification.contentView = convertView;
 mNotification.contentIntent = mIntent;
 //发送通知
 // 第一个参数唯一的标识该Notification,第二个参数就是Notification对象
 mManager.notify(1, mNotification);
 }
}

3.AndroidManifest添加权限

4.跳转界面的xml



 

5.跳转activity的实现

package edu.feicui.notification;
import android.app.Activity;
import android.app.NotificationManager;
import android.os.Bundle;
import android.widget.TextView;
/**
 * Created by Administrator on 2017-1-20.
 */
public class NotificationAcitivity extends Activity {
 private NotificationManager mManager;
 private int index = 2;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_notification);
 //初始化通知栏管理者
 mManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
 index = 2;
 mManager.cancelAll();
 }
}

简单粗暴实用,你值得拥有

以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,同时也希望多多支持脚本之家!

你可能感兴趣的:(Android之开发消息通知栏)