您的位置:首页 > 移动开发 > Android开发

四种通知栏提醒,Android实现通知栏提醒方法,手机通知栏适配测试Demo

2016-09-06 14:48 711 查看
package com.example.testnotice;

import android.support.v7.app.ActionBarActivity;

import android.annotation.SuppressLint;

import android.app.Notification;

import android.app.NotificationManager;

import android.app.PendingIntent;

import android.content.Context;

import android.content.Intent;

import android.os.Bundle;

import android.util.Log;

import android.view.Menu;

import android.view.MenuItem;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

import android.widget.RemoteViews;

@SuppressLint("NewApi")

public class MainActivity extends ActionBarActivity implements OnClickListener{

private Button bt1,bt2,bt3,bt4;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

bt1=(Button)findViewById(R.id.button1);

bt2=(Button)findViewById(R.id.button2);

bt3=(Button)findViewById(R.id.button3);

bt4=(Button)findViewById(R.id.button4);

bt1.setOnClickListener(this);

bt2.setOnClickListener(this);

bt3.setOnClickListener(this);

bt4.setOnClickListener(this);

}

@Override

public void onClick(View v) {

switch (v.getId()) {

case R.id.button1:

NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

Notification myNotify = new Notification();

// myNotify.icon = R.drawable.message;

myNotify.tickerText = "TickerText:您有新短消息,请注意查收!";

myNotify.when = System.currentTimeMillis();

myNotify.flags = Notification.FLAG_NO_CLEAR;// 不能够自动清除

RemoteViews rv = new RemoteViews(getPackageName(),

R.layout.my_notification);

rv.setTextViewText(R.id.text_content, "hello wrold!");

myNotify.contentView = rv;

Intent intent = new Intent(Intent.ACTION_MAIN);

PendingIntent contentIntent = PendingIntent.getActivity(this, 1,

intent, 1);

myNotify.contentIntent = contentIntent;

manager.notify(11, myNotify);

Log.i("AA", "1====");

break;

case R.id.button2:

NotificationManager manager2 = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

PendingIntent pendingIntent2 = PendingIntent.getActivity(this, 0,

new Intent(this, MainActivity.class), 0);

// 通过Notification.Builder来创建通知,注意API Level

// API11之后才支持

Notification notify2 = new Notification.Builder(this)

.setSmallIcon(R.drawable.ic_launcher) // 设置状态栏中的小图片,尺寸一般建议在24×24,这个图片同样也是在下拉状态栏中所显示,如果在那里需要更换更大的图片,可以使用setLargeIcon(Bitmap

// icon)

.setTicker("TickerText:" + "您有新短消息,请注意查收!")// 设置在status

// bar上显示的提示文字

.setContentTitle("我是消息标题")// 设置在下拉status

// bar后Activity,本例子中的NotififyMessage的TextView中显示的标题

.setContentText("我是消息内容")// TextView中显示的详细内容

.setContentIntent(pendingIntent2) // 关联PendingIntent

.setNumber(1) // 在TextView的右方显示的数字,可放大图片看,在最右侧。这个number同时也起到一个序列号的左右,如果多个触发多个通知(同一ID),可以指定显示哪一个。

.getNotification(); // 需要注意build()是在API level

// 16及之后增加的,在API11中可以使用getNotificatin()来代替

notify2.flags |= Notification.FLAG_AUTO_CANCEL;

manager2.notify(12, notify2);

Log.i("AA", "2====");

break;

case R.id.button3:

NotificationManager manager3 = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

PendingIntent pendingIntent3 = PendingIntent.getActivity(this, 0,

new Intent(this, MainActivity.class), 0);

// 通过Notification.Builder来创建通知,注意API Level

// API16之后才支持

Notification notify3 = new Notification.Builder(this)

.setSmallIcon(R.drawable.ic_launcher)

.setTicker("TickerText:" + "您有新短消息,请注意查收!")

.setContentTitle("我是消息标题")

.setContentText("我是消息内容")

.setContentIntent(pendingIntent3).setNumber(1).build(); // 需要注意build()是在API

// level16及之后增加的,API11可以使用getNotificatin()来替代

notify3.flags |= Notification.FLAG_AUTO_CANCEL; // FLAG_AUTO_CANCEL表明当通知被用户点击时,通知将被清除。

manager3.notify(13, notify3);// 步骤4:通过通知管理器来发起通知。如果id不同,则每click,在status哪里增加一个提

Log.i("AA", "3====");

break;

case R.id.button4:

String service = NOTIFICATION_SERVICE;

NotificationManager nManager = (NotificationManager) this.getSystemService(service);

Notification notification = new Notification();

String tickerText = "测试N otifaction"; // 通知提示

// 显示时间

long when = System.currentTimeMillis();

notification.icon = R.drawable.ic_launcher;// 设置通知的图标

notification.tickerText = tickerText; // 显示在状态栏中的文字

notification.when = when; // 设置来通知时的时间

notification.flags |= Notification.FLAG_AUTO_CANCEL; // 点击清除按钮或点击通知后会自动消

// 设置显示通知时的默认的发声、振动、Light效果

notification.defaults = Notification.DEFAULT_ALL;

PendingIntent pIntent = PendingIntent.getActivity(this, 0,

new Intent(this, MainActivity.class), 0);

// 设置通知的标题和内容

notification.setLatestEventInfo(this, "标题", "内容", pIntent);

// 发出通知

nManager.notify(42, notification);

Log.i("AA", "4====");

break;

default:

break;

}

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: