Notification的一些知识

之前准备用Notification.builder 但是这个是api11能用,但是我们在开发的过程中还是要向下兼容的,所以,我研究了下之前的方式呢  new Notification();上次有个同学问到的,刚刚弄了一个demo,希望对大家有点点帮助,不过有个疑问,就是现在酷狗音乐的后台在通知栏的效果不知道怎么实现的呢,因为我自定义的通知其实高度只能那么高的,如果有人了解这方面的,求链接,求思路,谢谢了!

效果图如下:





demo的架构:



1、布局文件:

<LinearLayout 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:orientation="vertical"
    >

    <Button
        android:id="@+id/button1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="简单的通知"
        />
    
    <Button
        android:id="@+id/button2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="自定义通知"
        ></Button>
    <Button 
        android:id="@+id/button3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="下载"
        />
</LinearLayout>

2、确定、取消的自定义布局:

<?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"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textview1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingLeft="60dip"
         />

    <Button
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_below="@id/textview1"
        android:layout_height="wrap_content" 
        android:layout_marginLeft="40dp"
        />
    <Button
        android:id="@+id/button4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_below="@id/textview1"
        android:layout_marginRight="40dip"
        />

</RelativeLayout>

3、下载的自定义布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    
    <TextView
        android:id="@+id/tv_download"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        />
	<ProgressBar 
	    android:id="@+id/progress_down"
	    android:layout_width="match_parent"
	    android:layout_height="wrap_content"
	    style="?android:attr/progressBarStyleHorizontal"
	    />
</LinearLayout>


4、自定义MyNotification.java

package com.example.bamboo_notification.service;

import com.example.bamboo_notification.R;

import android.app.Notification;
import android.app.NotificationManager;
import android.content.Context;
import android.widget.RemoteViews;

public class MyNotification extends Notification {
	private Context mContext;
	private int id;
	/** 通知的管理对象*/
	private NotificationManager mManager;
	public MyNotification(Context context,int id) {
		this.mContext=context;
		mManager=(NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
		
		this.icon=R.drawable.ic_launcher;
		this.tickerText="下载服务";
		RemoteViews views=new RemoteViews(mContext.getPackageName(), R.layout.download_notification);
		this.contentView=views;
		/** 一直运行这*/
		this.flags=Notification.FLAG_ONGOING_EVENT;
		this.id=id;
		
	}
	
	/**
	 * 显示通知
	 */
	public void show(){
		/** 显示通知*/
		mManager.notify(id, this);
	}
	/**
	 * 更新进度
	 * @param precent 当前进度
	 */
	public void updateProgress(int precent){
		if(precent < 100){
			this.contentView.setTextViewText(R.id.tv_download, "正在下载中...");
		}else{
			this.contentView.setTextViewText(R.id.tv_download, "下载完毕");
			/** 下载好了可以取消操作*/
			this.flags=Notification.FLAG_AUTO_CANCEL;
		}
		this.contentView.setProgressBar(R.id.progress_down, 100, precent, false);
		/** 刷新*/
		show();
	}
	
}

4、service的代码:

package com.example.bamboo_notification.service;

import android.app.Notification;
import android.app.Service;
import android.content.Intent;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;

public class DownloadService extends Service {

	@Override
	public IBinder onBind(Intent intent) {
		return null;
	}

	/** 通知 */
	private MyNotification mNotification;

	private Handler handler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			switch (msg.what) {
			case 1:
				mNotification.updateProgress(msg.arg1);
				break;
			case 2:
				mNotification.updateProgress(msg.arg1);
				/** 停止服务*/
				stopSelf();
				break;
			}
		}
	};

	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {
		mNotification = new MyNotification(getApplicationContext(),1004);
		mNotification.show();
		/** 模拟下载的线程 */
		new Thread(new Runnable() {

			@Override
			public void run() {
				for (int i = 1; i <= 100; i++) {
					Message msg = handler.obtainMessage();
					msg.arg1 = i;
					if (i == 100) {
						msg.what = 2;
					} else
						msg.what = 1;
					handler.sendMessage(msg);
					try {
						Thread.sleep(200);
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
			}
		}).start();
		return super.onStartCommand(intent, flags, startId);
	}

}

5、MainActivity.java

package com.example.bamboo_notification;

import com.example.bamboo_notification.service.DownloadService;

import android.app.Activity;
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.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.RemoteViews;

public class MainActivity extends Activity {
	/** 通知的管理者 */
	private NotificationManager mManager;
	/** 简单的通知 */
	private Button button1;
	/** 自定义通知 */
	private Button button2;
	/** 下载通知*/
	private Button button3;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		button1 = (Button) this.findViewById(R.id.button1);
		button2 = (Button) this.findViewById(R.id.button2);
		button3=(Button) this.findViewById(R.id.button3);
		
		mManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

		button1.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				Notification notification = new Notification(
						R.drawable.ic_launcher, "通知来了", System
								.currentTimeMillis());

				/** 点击了清除通知栏是否清除掉 */
				notification.flags = Notification.FLAG_SHOW_LIGHTS;
				/**
				 * //DEFAULT_ALL 使用所有默认值,比如声音,震动,闪屏等等 //DEFAULT_LIGHTS 使用默认闪光提示
				 * //DEFAULT_SOUNDS 使用默认提示声音 //DEFAULT_VIBRATE
				 * 使用默认手机震动,需加上<uses-permission
				 * android:name="android.permission.VIBRATE" />权限
				 * notification.defaults = Notification.DEFAULT_LIGHTS;
				 * */
				notification.defaults = Notification.DEFAULT_ALL;

				/** 延迟跳转的intent */
				Intent intent = new Intent(MainActivity.this,
						MainActivity.class);
				PendingIntent pIntent = PendingIntent.getActivity(
						MainActivity.this, 0, intent, 0);
				/** 这个方法是给通知设置标题、内容、即将跳转的intent */
				notification.setLatestEventInfo(MainActivity.this, "标题", "内容",
						pIntent);
				/** 发送通知 */
				mManager.notify(1001, notification);
			}
		});

		button2.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				/** 即将显示的intent*/
				Intent intent = new Intent(MainActivity.this,
						MainActivity.class);
				PendingIntent pIntent = PendingIntent.getActivity(
						MainActivity.this, 0, intent, 0);
				/** 创建一个通知*/
				Notification notification = new Notification(
						R.drawable.ic_launcher, "通知来了", System
								.currentTimeMillis());
				/** 自定义布局 */
				RemoteViews views = new RemoteViews(getPackageName(),
						R.layout.item_notification);
				notification.contentView = views;
				/** 设置自定义布局里面的内容 */
				notification.contentView.setTextViewText(R.id.textview1, "标题");
				notification.contentView.setTextViewText(R.id.button3, "确定");
				/** 设置相应的监听事件 */
				notification.contentView.setOnClickPendingIntent(R.id.button3,
						pIntent);
				notification.contentView.setTextViewText(R.id.button4, "取消");
				/** 网上看了别人的说这个手动设置contentView时必须设置,但是我注释了并没有报错!*/
				notification.contentIntent = pIntent;
				/** 发送通知*/
				mManager.notify(1002, notification);
			}
		});
		
		/** 开始下载*/
		button3.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Intent intent=new Intent(MainActivity.this, DownloadService.class);
				startService(intent);
			}
		});
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

}


学习的来源:

http://www.oschina.net/question/565065_72695

http://www.oschina.net/code/snippet_270292_14489

http://www.2cto.com/kf/201404/295565.html


源码下载:

点击打开链接

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值