码迷,mamicode.com
首页 > 其他好文 > 详细

通知(二)你可能不知道的Notification用法

时间:2015-01-31 21:53:40      阅读:255      评论:0      收藏:0      [点我收藏+]

标签:

这里是通知的其中一种,Notification。常用的和自定义的

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.statusnotification.MainActivity" >

    <Button
        android:id="@+id/button1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="65dp"
        android:text="普通通知的使用" />

    <Button
        android:id="@+id/button2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="自定义通知的使用" />

</RelativeLayout>
自定义通知用到的布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_alignParentLeft="true"
        android:layout_marginLeft="30dp"
        android:src="@drawable/notify" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@+id/imageView1"
        android:text="TextView1" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/imageView1"
        android:layout_below="@id/textView1"
        android:text="TextView2" />

</RelativeLayout>
package com.example.statusnotification;

import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RemoteViews;

public class MainActivity extends Activity {

	private Button button1;
	private NotificationManager notificationManager;//通知的管理类
	private Notification.Builder builder;
	private Button button2;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		button1 = (Button) findViewById(R.id.button1);
		button2 = (Button) findViewById(R.id.button2);
		//用getSystemService实例化通知
		notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
		builder = new Notification.Builder(MainActivity.this);
		button1.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(MainActivity.this,MainActivity.class);
				PendingIntent pendingIntent = PendingIntent.getActivity(MainActivity.this, 0, intent, 0);
				builder.setContentIntent(pendingIntent);
				builder.setContentTitle("通知提示");
				builder.setContentText("明天放假,今天提前下班");
				builder.setSmallIcon(R.drawable.notify);
				builder.setTicker("有新通知");//第一次来通知的时候能看见
				builder.setDefaults(Notification.DEFAULT_ALL);
				/*DEFAULT_ALL全部默认
				 *DEFAULT_LIGHTS默认闪光
				 *DEFAULT_SOUND默认的铃声
				 *DEFAULT_VIBRATE默认振动
				 * 可通过指定Notification的不同参数设置默认的闪光、声音、振动
				 * 最后不要在AndroidManifest.xml里忘了授权
				 * 
				 */
				Uri uri = Uri.parse("file:///mnt/sdcard/Music/audio.mp3");
				builder.setSound(uri);
				Notification notification = builder.build();
				//仅限在Android4.1以上使用,较低版本直接 new Notification
				//低版本使用notification.defaults = Notification.DEFAULT_SOUND
				/*
				 * 第一个参数:标识符id
				 * 第二个参数:Notification
				 */
				//long[] vibrates = { 10, 20, 30};
				//notification.vibrate = vibrates;//通过一个数组设置振动的时间
				notificationManager.notify(88, notification);
			}
		});
		
		button2.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				//自定义的通知一定要用RemoteViews
				RemoteViews view = new RemoteViews(getPackageName(), R.layout.custom_notification);
				view.setImageViewResource(R.id.imageView1, R.drawable.notify);
				view.setTextViewText(R.id.textView1, "通知");
				view.setTextViewText(R.id.textView2, "今晚一起聚餐");
				
				Intent intent = new Intent(MainActivity.this, MainActivity.class);
				PendingIntent pendingIntent = PendingIntent.getActivity(MainActivity.this, 0, intent, 0);
				builder.setContentIntent(pendingIntent);
				builder.setContent(view);  //这里别忘了指定它的布局
				Notification notification = builder.build();
				notificationManager.notify(89, notification);
			}
		});
		
	}
}
工程可设置自定义通知的铃声,mp3文件放在SDCard中。高版本路径可能有所不同sdcard目录在mnt目录下,这里放的mp3文件是audio.mp3

技术分享

运行结果

技术分享技术分享技术分享

先点击普通通知按钮再点击自定义的通知按钮,结果如预期一样。但如果先点击自定义的通知按钮再点击普通通知按钮,出来的通知都是自定义通知的布局和内容。??难道是线程的问题??

今天发现eclipse也有很奇葩的时候,注释里有个"Android4.1""new Notification"也能给我报错,一定要clean才行

通知(二)你可能不知道的Notification用法

标签:

原文地址:http://blog.csdn.net/lindonglian/article/details/43346549

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!