android四大组件之一广播notification(通知)的Title模拟下载

这里简单说一个Title里的下载进度条和下载的内容,下面就看一下代码的实现:

1、新建一个点击就可以马上下载的一个按钮

 

<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包,有可能有的eclipse版本里的V4包没有Progress,就需要重新引入有的Progress的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);
			}
		});

	}

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

generallizhong

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

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

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

打赏作者

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

抵扣说明:

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

余额充值