android四大组件之一notification(广播)

下面来模拟一个点击按钮就可以发送一条消息,并且显示在Title(手机顶部)位置,下面借来看一下代码的具体实现:

1、创建一个点击按钮XML布局

<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"
  >

    <Button
        android:id="@+id/notificationbtn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="发送通知"/>

</RelativeLayout>

2、创建一个Activty继承Activty

这里特别住的是要引入一个V4包,

package com.scxh.notification;

import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.NotificationCompat;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity {
	private NotificationCompat.Builder mBuilder;
	private NotificationManager mNotifyManager;
	private Handler mHandler = new Handler();
	private Button mButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mButton=(Button) findViewById(R.id.notificationbtn);
        mButton.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				mNotifyManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
				final int id = 001;
				// 第一步
				mBuilder = new NotificationCompat.Builder(MainActivity.this);
				mBuilder.setSmallIcon(R.drawable.a);
				mBuilder.setContentTitle("图片下载");
				mBuilder.setContentText("正在下载中...");
			
				 //mBuilder.setProgress(0, 0, false);
			
				new Thread(){
					public void run() {
						int max = 100;
						for(int i = 0; i< 100; i++){
							mBuilder.setProgress(max, i, false);
							mNotifyManager.notify(id, mBuilder.getNotification());
							
							try {
								Thread.sleep(200);
							} catch (InterruptedException e) {
								e.printStackTrace();
							}
						}
						mHandler.post(new Runnable() {
							
							@Override
							public void run() {
								mBuilder.setContentText("下载完成");
								mNotifyManager.notify(id, mBuilder.getNotification());
							}
						});
					};
				}.start();
				
				// 第三步 发布通知
				Notification notification = mBuilder.getNotification();
				notification.flags = Notification.FLAG_AUTO_CANCEL;
				mNotifyManager.notify(id, notification);
			}
		});

	}
			
        private void createBaseNotification(){
        	Intent intent=new Intent(MainActivity.this,Zzhuanghuan.class);
			//定义notification的Action(行为)
			PendingIntent pendingIntentp=PendingIntent.getActivity(MainActivity.this, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
			//获取NotificationManager(通知管理器)
			NotificationManager mNotificationManager=(NotificationManager) getSystemService(NOTIFICATION_SERVICE);
			//创建Builder
			NotificationCompat.Builder mbBuilder=new NotificationCompat.Builder(MainActivity.this);
			mbBuilder.setContentIntent(pendingIntentp);
	    	mbBuilder.setSmallIcon(R.drawable.a);
	    	mbBuilder.setContentTitle("問蒼茫大地,誰主沉浮");
	    	mbBuilder.setContentText("彈指間檣櫓灰飛煙滅");
	    	//弹出显示
	    	mbBuilder.setTicker("問蒼茫大地,誰主沉浮");
	    	//音乐属性
	    	//mbBuilder.setDefaults(Notification.DEFAULT_ALL);
	    	
		
		int notificationId=001;
		Notification notification= mbBuilder.getNotification();
		notification.flags=Notification.FLAG_AUTO_CANCEL;
		mNotificationManager.notify(notificationId,notification);
		}
	}
        
    
    	

    

这样就可以实现模拟接受通知了

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

generallizhong

你的鼓励是我创作最大的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值