`

Notification 简介

阅读更多
public class MainActivity extends Activity {
	private Spinner spinner = null;
	private NotificationManager notificationManager = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		spinner = (Spinner) findViewById(R.id.spinner1);
		ArrayAdapter spinnerAdapter = ArrayAdapter.createFromResource(
				MainActivity.this, R.array.spinnerItems,
				android.R.layout.simple_spinner_item);
		spinnerAdapter
				.setDropDownViewResource(android.R.layout.simple_list_item_checked);
		spinner.setAdapter(spinnerAdapter);
		spinner.setSelection(1, true);
		spinner.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {
				// TODO Auto-generated method stub
				if (arg0.getItemAtPosition(arg2).toString().equals("Online")) {
					showNotification("Online");
				} else {
					// showNotification("Outline");
					showDefinedNotifications();
				}

			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
				// TODO Auto-generated method stub

			}
		});

	}

	/**
	 * 显示系统预定义的通知栏
	 * 
	 * @param string
	 */
	private void showNotification(String string) {
		notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
		Notification notification = new Notification();
		notification.when = System.currentTimeMillis();
		notification.tickerText = "show";// notification刚刚生成时弹出的提示文字
		notification.icon = R.drawable.acc_on;
		notification.flags = Notification.FLAG_AUTO_CANCEL;// 可以清除

		// notification.defaults = Notification.DEFAULT_VIBRATE;//添加震动,记得加权限
		// long[] vibrate = {0,100,200,300};
		// notification.vibrate = vibrate;

		Intent intent = new Intent(MainActivity.this, MainActivity.class);
		// intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_NEW_TASK);
		PendingIntent pendingIntent = PendingIntent
				.getActivity(MainActivity.this, 0, intent,
						PendingIntent.FLAG_UPDATE_CURRENT);

		notification.setLatestEventInfo(MainActivity.this, "Notification_test",
				string, pendingIntent);
		notificationManager.notify(1, notification);

	}

	/**
	 * 显示自定义的通知栏
	 * 使用自定义的点击通知后才会消失。
	 */
	private void showDefinedNotifications() {
		notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
		Notification notification = new Notification(R.drawable.acc_on, "有提示!",
				System.currentTimeMillis());
		Intent intent = new Intent(MainActivity.this, MainActivity.class);
		notification.flags = Notification.FLAG_AUTO_CANCEL;
		PendingIntent pendingIntent = PendingIntent
				.getActivity(MainActivity.this, 0, intent,
						PendingIntent.FLAG_UPDATE_CURRENT);
		RemoteViews remoteViews = new RemoteViews(getPackageName(),
				R.layout.notification_items);
		remoteViews.setImageViewResource(R.id.imageView1, R.drawable.ic_launcher);
		remoteViews.setTextViewText(R.id.tx_content, "我是自定义的Notification");
		notification.contentView = remoteViews;
		notification.contentIntent = pendingIntent;
		notificationManager.notify(1, notification);
	}

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

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub

		super.onResume();
	}
}

自定义的R.layout.notification_items:

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

  
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <ImageView
            android:id="@+id/imageView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/ic_launcher" />

        <TextView
            android:id="@+id/tx_content"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:textAppearance="?android:attr/textAppearanceMedium" />

    </LinearLayout>

</LinearLayout>

 自定义数组array.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string-array name="spinnerItems">
      <item>Online</item>
      <item>Outline</item>
    </string-array>
    
</resources>

 结构图:

 

  • 大小: 13.2 KB
分享到:
评论

相关推荐

    Apple Push Notification Service简介

    Apple Push Notification Service简介

    HTML5 Notification(桌面提醒)功能使用实例

    一、HTML5 Notification 简介HTML5 Notification ,即桌面通知。目前浏览器依然是一个严格的沙盒工作模式,这种模式把浏览器和桌面的通信隔离开。Notification可以跨越沙盒能够让浏览器即使是最小化状态也能将消息...

    Android高级编程.pdf

    8.4 Notification简介 8.4.1 Notification Manager简介 8.4.2 创建Notification 8.4.3 触发Notification 8.4.4 向Earthquake Monitor中添加Notiflcation 8.4.5 高级Notification技术 8.4.6 持续的和连续的...

    《Android高级编程》

    8.2.1 创建新的线程 8.2.2 为GUI操作同步线程 8.2.3 将Earthquake Service移动到后台线程 8.3 创建一个Toast 8.3.1 定制Toast 8.3.2 在工作(worker)线程中使用Toast 8.4 Notification简介 8.4.1 Notification ...

    Android高级编程--源代码

    8.4 Notification简介 266 8.4.1 Notification Manager简介 267 8.4.2 创建Notification 267 8.4.3 触发Notification 268 8.4.4 向Earthquake Monitor中添加Notification 268 8.4.5 高级Notification技术 271 ...

    Android中AlarmManager+Notification实现定时通知提醒功能

    AlarmManager简介 AlarmManager实质是一个全局的定时器,是Android中常用的一种系统级别的提示服务,在指定时间或周期性启动其它组件(包括Activity,Service,BroadcastReceiver)。本文将讲解一下如何使用...

    荷兰Covid19 Notification App的Android来源-Android开发

    荷兰Covid19 Notification App的Android来源Covid19 Notification App-Android简介此存储库包含荷兰暴露通知应用程序概念验证的Android应用程序。 我们会在早期提供此代码,以便每个人在开发应用程序时都可以跟进,...

    Android开发之Notification手机状态栏通知用法实例分析

    简介: 通知是显示在手机状态栏的通知(PS:就是手机上方,显示时间啥的那一栏) 用法: Notification添加了Builder()类,其包含如下方法: 1. setDefaults() 通知led灯、音乐、震动等 2. setAutoChange() 设置...

    easy-notification:[只读]客户端,用于在EonX上发送通知

    --- eonx_docs ---标题:简介权重:0 --- eonx_docs --- 制作此软件包的目的在于简化EonX Notification Service中的通知分发,并且仅与此服务一起使用。 需要包(作曲家) 建议安装此软件包的方法是使用 : $ ...

    eventmachine-distributed-notification:监视 OS X 分布式通知的 EventMachine 扩展

    简介 require 'eventmachine-distributed-notification' 观看通知 class Watcher &lt; EM :: DistributedNotificationWatch def notify ( name , user_info ) puts 'now playing %s' % [ user_info [ 'Artist' ]...

    javasmack源码-xmpp-push-notification:xmpp消息推送

    简介:XMPP是一种基于XML的协议,它继承了在XML环境中灵活的发展性,有很强的可扩展性。包括上面讲的GCM服务器底层也是采用XMPP协议封装的。 优点:协议成熟、强大、可扩展性强、目前主要应用于许多聊天系统中,且已...

    使用DP ^ 3T iOS SDK的本地iOS应用可处理Apple的Exposure Notification框架-Swift开发

    使用DP ^ 3T iOS sdk处理来自Apple RadarCOVID iOS应用程序的Exposure Notification框架的本机iOS应用程序简介使用DP3T iOS SDK的RadarCOVID跟踪客户端的本机iOS实现用来指导解决方案的工具:XCode 11.6安装和入门该...

    RocketMQ_原理简介

    本文档旨在描述 RocketMQ 的多个关键特性的实现原理,并对消息中间件...文中主要引用了JMS 规范与CORBA Notification 规范,规范为我们设计系统指明了 方向,但是仍有不少问题规范没有提及,对于消息中间件又至关重要

    android-X:这是与Android X库兼容的iZooto的Notification SDK

    简介:本文档将讨论iZooto的Android SDK集成过程。 过程:以下过程是集成iZooto SDK的逐步指南。 将android-release.AAR(共享)文件复制到您项目的libs文件夹中。 打开app / build.gradle(模块:app)文件,并...

    openfalcon-swcollector

    简介: openfalcon监控系统的开源代码swcollector,用于监控 网络设备。 开发目的: 本代码在swcollector源代码中,增加了一个API接口 http://你部署swcollector的IP:10000/notification,供 别的项目调用它。比如...

    self-service-password-master.zip

    官方简介: Self Service Password is a PHP application that allows users to change their password in an LDAP directory. The application can be used on standard LDAPv3 directories (OpenLDAP, OpenDS, ...

    Flutter插件,用于创建通知-JavaScript开发

    LocalNotification Flutter插件插件已不再积极开发我目前没有计划继续积极开发此插件。 如果无法正常工作或缺少...简介LocalNotifications是在Android和iOS上创建通知的简便方法。 平台当前支持以下平台所有Android版本

    Android XMPP通讯自定义Packet&Provider

    摘要 在xmpp通信过程中,asmack中...androidpn (Android Push Notification)是一个基于XMPP协议的java开源Android push notification实现。它包含了完整的客户端和服务器端。 androidpn包括Server端和Client端,项目名

    QY-OpenSource:开源学院,帮助Android新手学习使用开源项目

    图文简介: 2014-11-07项目搭建 添加ResideMenu侧滑菜单,作为项目的主页 添加XListView 流畅的下拉刷新功能以及上拉加载更多的分页功能 添加ButterKnife视图注入库 可以对任意的View对象使用注入,当然了,需要提供...

Global site tag (gtag.js) - Google Analytics