Notification顶部状态栏和下拉任务栏通知

界面很简单,功能也实现的很简单,

一个简单的DEMO,

尴尬欢迎转载,请加地址http://blog.csdn.net/jing110fei/article/details/39055809

1主页面


2点击开启发送后

      

设置每隔10秒发送1次直到用户点击通知栏或点击停止发送

点击通知栏,会跳转



首先的一点是,为了能使所有Actvity都能方便的调用同一对象或者方法,我自定义了Application

public class AllApplication extends Application{
	public ScheduledExecutorService scheduledThreadPool;
	@Override
	public void onCreate() {
		// TODO Auto-generated method stub
		super.onCreate();
		scheduledThreadPool=null;
	}	
}

为了使这个Application生效,需要在AndroidManifest.xml中替换原来的

<application
        android:name=".AllApplication"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
</application>

不多说,直接上代码,注释就在代码里

首先是两个简单的布局文件

activity_notification_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
	<Button 
	    android:id="@+id/Button1"
	    android:layout_width="fill_parent"
	    android:layout_height="wrap_content"
	    android:text="开启发送通知栏"/>
	<Button 
	    android:id="@+id/Button2"
	    android:layout_width="fill_parent"
	    android:layout_height="wrap_content"
	    android:text="停止发送通知栏"/>
   
</LinearLayout>

activity2.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
	<TextView 
	    android:id="@+id/text1"
	    android:layout_width="fill_parent"
	    android:layout_height="fill_parent"
	    android:background="@drawable/setbar_bg"
	    />
</LinearLayout>

主文件NotificationMainActivity.java

public class NotificationMainActivity extends Activity implements OnClickListener {
	private static Button button1,button2;
	private AllApplication application;
	public NotificationManager mNotificationManager;
	//定义count为通知内容用以测试
	public int count=0;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_notification_main);
       // 获取全局上下文对象
        application=(AllApplication) getApplication();
        button1=(Button)findViewById(R.id.Button1);
        button1.setOnClickListener(this);
        findViewById(R.id.Button2).setOnClickListener(this);
       
        
    }
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.Button1:
			count=0;
			 /**创建一个可安排在给定延迟后运行命令或者定期地执行的线程池。 效果类似于Timer定时器
	         * ScheduledThreadPool是一个固定大小的线程池,与FixedThreadPool类似,执行的任务是定时执行。 
	         *  */
			application.scheduledThreadPool = Executors.newScheduledThreadPool(1);
			//5秒后执行,以后每10秒执行一次
			application.scheduledThreadPool.scheduleWithFixedDelay(new CustomTask(), 5, 10,
							TimeUnit.SECONDS);
			
			break;
		case R.id.Button2:
			application.scheduledThreadPool.shutdown();
			application.scheduledThreadPool=null;	
			//取消通知栏显示
			mNotificationManager.cancel(100);
			break;
		}
	}
	class CustomTask implements Runnable {

		public void run() {
			// TODO Auto-generated method stub
			Bundle date=new Bundle();
			date.putInt("count", count++);
			Message message = Message.obtain(sHandler, 1);	
			message.setData(date);
			message.sendToTarget();
		}
		
	}
	private Handler sHandler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			// TODO Auto-generated method stub
			super.handleMessage(msg);
			switch (msg.what) {
			case 1:
				int counts=msg.getData().getInt("count");
				mNotificationManager= (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
				//手机最上方提示栏的内容
				Notification notification = new Notification(R.drawable.ic_launcher,
						"count值发生变化当前为"+count, System.currentTimeMillis());
				/**
				 * 设置添加声音
				 * 或者使用以下几种方式 
				 * notification.sound = Uri.parse("file:///sdcard/notification/ringer.mp3"); 
				 * notification.sound = Uri.withAppendedPath(Audio.Media.INTERNAL_CONTENT_URI, "6"); 
				 * 如果想要让声音持续重复直到用户对通知做出反应,则可以在notification的flags字段增加"FLAG_INSISTENT" 
				 * 如果notification的defaults字段包括了"DEFAULT_SOUND"属性,则这个属性将覆盖sound字段中定义的声音 
				 * 还可以添加震动 notification.defaults |= Notification.DEFAULT_VIBRATE; 
				 * 或者可以定义自己的振动模式: 
				 * long[] vibrate = {0,100,200,300}; //0毫秒后开始振动,振动100毫秒后停止,再过200毫秒后再次振动300毫秒 
				 * notification.vibrate = vibrate; 
				 * */
				notification.defaults |= Notification.DEFAULT_SOUND;
				/**
				 * notification.flags |= FLAG_AUTO_CANCEL; //在通知栏上点击此通知后自动清除此通知 
				 * notification.flags |= FLAG_INSISTENT; //重复发出声音,直到用户响应此通知 
				 */
				notification.flags = Notification.FLAG_AUTO_CANCEL;
				//通知栏的title
				CharSequence contentTitle = "测试";
				//通知栏的内容
				CharSequence contentText="count值发生变化快去看看吧";
				//当点击通知栏时给其设置一个跳转的页面
				Intent notificationIntent = new Intent();			
				notificationIntent.setClass(getApplicationContext(),
						Activity2.class);	
				//在这里传值给点击跳转的Intent
				notificationIntent.putExtra("count", counts);
				PendingIntent contentIntent = PendingIntent.getActivity(
				getApplicationContext(), 0, notificationIntent,
				PendingIntent.FLAG_UPDATE_CURRENT);		
				//将上面的设置加入通知栏
				notification.setLatestEventInfo(getApplicationContext(),
					contentTitle, contentText, contentIntent);

			// 用mNotificationManager的notify方法通知用户生成标题栏消息通知
			//这里的ID100,用来定义状态栏通知以及下拉任务栏通知,并且用来在需要的时候,将这两样取消
			mNotificationManager.notify(100, notification);
				break;
			default:
				break;
			}
		}
		
	};
	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
	}
	
}

点击跳转后的文件Activity2.java

public class Activity2 extends Activity{

	private AllApplication application;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		 application=(AllApplication) getApplication();
		application.scheduledThreadPool.shutdown();
		 application.scheduledThreadPool=null;	
		 setContentView(R.layout.activity2);
		 Bundle bundle=getIntent().getExtras();
		 int count=bundle.getInt("count");
		 TextView textView=(TextView)findViewById(R.id.text1);
		 textView.setText("当前count值为"+count);
	}
	
}

好了,全部代码都在这里了。。。有不足的地方欢迎大家指正可怜







  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: Android通知栏是一种用于显示应用程序通知的用户界面元素。它可以在屏幕的顶部或底部显示,并且可以包含文本、图像、声音和其他交互元素。通知栏可以帮助用户及时了解应用程序的状态和事件,例如新消息、更新、提醒和警告等。开发人员可以使用Android SDK提供的通知API来创建和管理通知栏,以便更好地与用户进行交互。 ### 回答2: Android 通知栏是一种提供给用户显示有关当前状态和行动的信息的方式,以便用户可以及时地采取必要的行动。通知栏可以显示来自应用程序和系统的通知消息,例如电子邮件、信息和其他事件,可以让用户在不离开当前应用的情况下对这些消息进行响应。 在通知栏中,每个通知都包含一个图标、标题、简短的消息文本和通知时间。用户可以从通知栏中直接打开应用程序或查看通知的详细信息。通知栏还可以显示多个通知,按照时间顺序进行排序。 开发人员可以使用 Android SDK 提供的 Notification 类来创建自定义通知。可以设置通知图标、文本、声音、震动和延迟时间等属性。还可以指定通知的优先级,以便系统在有限的屏幕空间中为用户先显示最重要的通知通知还可以与 PendingIntent 实例相关联,以便在用户单击通知时执行特定的操作,例如打开应用程序,启动活动或显示通知详细信息的专用活动。 总之,Android 通知栏是一种非常有用的功能,可以让用户及时了解应用程序和系统中的重要事件,并采取及时的行动。开发人员可以使用通知栏来实现更好的用户体验。 ### 回答3: Android 通知栏是一种非常有用的功能,它可以让你的应用程序以一种全新的方式与用户进行交互。在 Android 应用程序中,通知栏是一种特殊的 UI 元素,它显示在屏幕的顶部,并显示当前状态、事件或提示。通知栏通常包含一组小图标,可以展开或折叠以显示更多详细信息。 Android 通知栏有许多不同的用途,例如提醒用户新的消息、电子邮件、电话、提醒等等。发送通知的应用程序无需与用户保持连接,这使得通知栏非常适合后台服务或其他形式的低功耗通信。通知栏还允许用户直接从通知菜单中操作应用程序。例如,当用户收到新的电子邮件时,他们可以在通知栏中选择该电子邮件并立即查看其内容,而无需打开电子邮件应用程序。 Android 通知栏的另一个优点是它的可定制性。可以轻松地修改通知的外观、行为和内容,以满足不同应用程序的需求和设计要求。您可以为通知添加各种元素,如纯文本、小图标、大图标、进度指示器、按钮等等。这样,您可以轻松地创造与您的品牌和应用程序设计语言保持一致的通知。 在实现通知栏之前,您需要确保该应用程序已获得了通知权限。如果您的应用程序需要通知用户任何内容,则必须获得 Android 手机上的通知权限。可以在应用程序设置中找到此选项。 虽然 Android 通知栏很有用,但在某些情况下,它们可能会变得令人分心。因此,应该仔细考虑应用程序通知的数量和类型,以确保用户不会感到困扰。通知栏还可以消耗设备电池,因此也应考虑优化应用程序以最小化资源消耗。 Android 通知栏作为 Android 应用程序非常重要的一部分,可以帮助您在应用程序和用户之间建立更紧密的联系,并提供有关应用程序状态、事件和提示的有用信息。通过努力优化您的应用程序通知,您可以确保用户感到受到了关注,并且同时不会让他们感到困扰。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值