`
GrantWoo
  • 浏览: 1479 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
最近访客 更多访客>>
社区版块
存档分类
最新评论

NotificationManager与Notification的应用

阅读更多

本博客内容选摘自《Google Android SDK 开发范例大全》第五章第8节(第170页),如需详细了解请查看原书籍

 

下面通过模拟MSN在线状态的切换,在切换状态时改变状态栏上的状态小图标和提示文本的示例来学习NotificationManager与Notification的应用。

代码如下:

 

StateBarNotifyActivity.java

package com.st;

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.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;

public class StateBarNotifyActivity extends Activity {
	
	private NotificationManager nm = null;
	private Spinner spinner = null;
	private ArrayAdapter<String> adapter = null;
	
	private static final String ONLINE="在线";
	private static final String BUSY="忙碌";
	private static final String AWAY="离开";
	private static final String OFFLINE="离线";
	
	private static final String[]stateList = {ONLINE,BUSY,AWAY,OFFLINE};
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        
        spinner = (Spinner)findViewById(R.id.spinner);
        nm = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);

        adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, stateList);
        spinner.setAdapter(adapter);
        
        spinner.setOnItemSelectedListener(new Spinner.OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> parent, View view,
					int position, long id) {
				// TODO Auto-generated method stub
				Log.i("OnItemSelectedListener", "position="+position+", id="+id);
				if(stateList[position].equals(ONLINE)){
					setNotifyType(R.drawable.online,ONLINE);
				}else if(stateList[position].equals(BUSY)){
					setNotifyType(R.drawable.busy,BUSY);
				}else if(stateList[position].equals(AWAY)){
					setNotifyType(R.drawable.away,AWAY);
				}else if(stateList[position].equals(OFFLINE)){
					setNotifyType(R.drawable.offline,OFFLINE);
				}
			}

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

			
		});
        
    }

    /**
     * 发出通知
     * */
	protected void setNotifyType(int statePngId, String showText) {
		// TODO Auto-generated method stub
		
		Intent notifyIntent = new Intent(this,NotifyActivity.class);
		notifyIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		
		PendingIntent pintent = PendingIntent.getActivity(this, 0, notifyIntent, 0);
		
		Notification notification = new Notification();
		
		notification.icon = statePngId;//设置图标
		notification.tickerText = showText;//设置文本提示
		notification.defaults = Notification.DEFAULT_LIGHTS;//设置默认声音-Notification.DEFAULT_SOUND,还可以是其他的值:屏幕发亮-Notification.DEFAULT_LIGHTS;手机震动-Notification.DEFAULT_VIBRATE;包含以上三种动作-Notification.DEFAULT_ALL
		notification.setLatestEventInfo(this, "MSN登录状态", showText, pintent);
		
		//发出通知
		nm.notify(0, notification);
	}
}

 

 NotifyActivity.java

package com.st;

import android.app.Activity;
import android.os.Bundle;
import android.widget.Toast;

public class NotifyActivity extends Activity {

	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		
		Toast.makeText(this, "这是模拟MSN登录状态的程序", Toast.LENGTH_LONG).show();
		finish();
	}
}

 

 main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/hello"
    />
    
    <Spinner
    	android:id="@+id/spinner"
    	android:layout_width="fill_parent"
    	android:layout_height="wrap_content"
    	>
    </Spinner>
</LinearLayout>

 

 strings.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="hello">模拟MSN切换登录状态的程序</string>
    <string name="app_name">StateBarNotify</string>
</resources>

 

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.st"
      android:versionCode="1"
      android:versionName="1.0">
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".StateBarNotifyActivity"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        
        <activity android:name=".NotifyActivity"></activity>

    </application>


</manifest> 

  

 

分享到:
评论

相关推荐

    Android 在状态栏添加Notification信息图标及提示.rar

    这个例子演示Android 在状态栏添加Notification信息图标及提示,相信大家对这个功能已经不陌生了,手机中安装的APP,一般都会在后台运行,时不时会在手机顶部的状态栏中显示应用的图标,滑出状态栏会看到详细的信息...

    UpdateApk.zip自动检测更新apkDemo

    这个自动检测升级Apk的功能,相信每个应用都会使用到。...其中使用Service、BroadcastReceiver、OkHttp下载、Gson解析json、NotificationManager、Notification 运用的知识也挺多的,也可以功大家学习使用。

    Android中通过Notification&NotificationManager实现消息通知

    notification是一种让你的应用程序在没有开启情况下或在后台运行警示用户。它是看不见的程序组件(Broadcast Receiver,Service和不活跃的Activity)警示用户有需要注意的事件发生的最好途径。  1、新建一个android...

    Android应用开发详解

    Broadcast Receiver Android 广播事件处理 Broadcast Receiver,讲述了广播事件处理机制、Notification、NotificationManager和AlarmManager的使用 第9章 Android中的数据存取 Android中的数据存取,讲述了Android...

    Notifications.Wpf:WPF的吐司通知

    通知文件WPF吐司通知。安装: Install-Package Notifications.Wpf用法:通过任务栏的通知: var notificationManager = new NotificationManager ();notificationManager . Show ( new ...应用程序窗口内

    Google Android SDK开发范例大全(PDF高清完整版3)(4-3)

    5.8 状态栏的图标与文字提醒——NotificationManager与Notification对象的应用 5.9 搜索手机通讯录自动完成——使用ContentResolver 5.10 取得联系人资料——Provider.Contact的使用 5.11 制作有图标的文件资源管理...

    Google Android SDK开发范例大全(PDF完整版4)(4-4)

    5.8 状态栏的图标与文字提醒——NotificationManager与Notification对象的应用 5.9 搜索手机通讯录自动完成——使用ContentResolver 5.10 取得联系人资料——Provider.Contact的使用 5.11 制作有图标的文件资源管理...

    Android 移动应用开发 使用Notification通知 及NotificationChannel的使用

    NotificationManager manager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE); 2.使用一个Builder构造器来创建一个Notification对象 Notification notification = new NotificationCompat.Builder...

    Google Android SDK开发范例大全(PDF高清完整版1)(4-1)

    5.8 状态栏的图标与文字提醒——NotificationManager与Notification对象的应用 5.9 搜索手机通讯录自动完成——使用ContentResolver 5.10 取得联系人资料——Provider.Contact的使用 5.11 制作有图标的文件资源管理...

    Android编程开发之NotiFication用法详解

    在帮助文档中,是这么说的, notification类表示一个持久的通知,将提交给用户使用NotificationManager。已添加的Notification.Builder,使其更容易构建通知。 notification是一种让你的应用程序在没有开启情况下或...

    收到广播后启动一个应用程序

    BroadcastReceiver中启动Activity,broadcast不会直接显示一个用户界面,而是启动一个activity来响应它们所接受到的信息或是使用 NotificationManager警示用户。Notification有很多方式能引起用户的注意。

    《Android应用开发揭秘》附带光盘代码.

     4.2.21 状态栏提示(Notification、NotificationManager)  4.2.22 对话框中的进度条(ProgressDialog)  4.3 界面布局  4.3.1 垂直线性布局  4.3.2 水平线性布局  4.3.3.相对布局(RelativeLayout)  4.3.4 表单...

    iOS推送之本地通知UILocalNotification

    本地的Notification由iOS下NotificationManager统一管理,只需要将封装好的本地Notification对象加入到系统Notification管理机制队列中,系统会在指定的时间激发将本地Notification,应用只需设计好处理Notification...

    Android中Notification通知用法详解

    Notification的作用 通知(Notification)是Android系统中比较有特色的一个功能。当某个应用程序希望向用户发出一些提示信息,而该...NotificationManager manager=(NotificationManager) getSystemService(NOTIFICATION

    Google Android SDK开发范例大全的目录

    5.8 状态栏的图标与文字提醒——NotificationManager与Notification对象的应用-p178 5.9 搜索手机通讯录自动完成——使用ContentResolver-p82 5.10 取得联系人资料——Provider.Contact的使用 5.11 制作有图标的文件...

    Google+Android+SDK开发范例大全

    5.7 图文可视化提醒——Toast与LinearLayoutView 5.8 状态栏的图标与文字提醒——NotificationManager与Notification对象的应用 5.9 搜索手机通讯录自动完成——使用ContentResolver 5.10 取得联系人资料——...

    Google Android SDK开发范例大全(完整版附部分源码).pdf

    5.8 状态栏的图标与文字提醒——NotificationManager与Notification对象的应用 5.9 搜索手机通讯录自动完成——使用ContentResolver 5.10 取得联系人资料——Provider.Contact的使用 5.11 制作有图标的文件资源...

Global site tag (gtag.js) - Google Analytics