zoukankan      html  css  js  c++  java
  • Android的状态栏通知(Notification)

    通知用于在状态栏显示消息,消息到来时以图标方式表示,如下:

    如果需要查看消息,可以拖动状态栏到屏幕下方即可查看消息.

    1、Layout布局文件:

    <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"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        tools:context=".MainActivity" >
    
        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignLeft="@+id/button1"
            android:layout_alignRight="@+id/button1"
            android:layout_below="@+id/button1"
            android:layout_marginTop="46dp"
            android:onClick="test1"
            android:text="@string/text_notifi" />
    
        <Button
            android:id="@+id/button2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignRight="@+id/button1"
            android:layout_below="@+id/button1"
            android:layout_marginTop="94dp"
            android:onClick="clearNoti"
            android:text="@string/text_clear" />
    
    </RelativeLayout>
    


    2、string.xml

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
    
        <string name="app_name">lession16-notifi</string>
        <string name="action_settings">Settings</string>
        <string name="hello_world">Hello world!</string>
        
        <string name="text_notifi">Notification应用案例</string>
        <string name="text_clear">清除通知</string>
    
    </resources>
    


    3、MainActivity

    package com.example.lession16_notifi;
    
    import android.os.Bundle;
    import android.app.Activity;
    import android.app.Notification;
    import android.app.NotificationManager;
    import android.app.PendingIntent;
    import android.view.Menu;
    import android.view.View;
    
    public class MainActivity extends Activity {
    	private NotificationManager notificationManager;
    
    	@Override
    	protected void onCreate(Bundle savedInstanceState) {
    		super.onCreate(savedInstanceState);
    		setContentView(R.layout.activity_main);
    		notificationManager=(NotificationManager) getSystemService(NOTIFICATION_SERVICE);	}
    
    	@Override
    	public boolean onCreateOptionsMenu(Menu menu) {
    		// Inflate the menu; this adds items to the action bar if it is present.
    		getMenuInflater().inflate(R.menu.main, menu);
    		return true;
    	}
    	
    	public void test1(View v){
    		showNotification("来短信了", "5557", "I love you", R.drawable.ic_launcher, R.drawable.ic_launcher);
    	}
    	
    	public void showNotification(String tickerText,String contentTitle,String contentText,int iconId,int notiId){
    		//创建一个Notification
    		Notification notification=new Notification();
    		//设置通知  消息  图标
    		notification.icon=iconId;
    		//设置发出消息的内容
    		notification.tickerText=tickerText;
    		//设置发出通知的时间
    		notification.when=System.currentTimeMillis();
    		
    		//设置显示通知时的默认的发声、震动、Light效果
    		notification.defaults=Notification.DEFAULT_VIBRATE;//震动
    		//Notification notification = new Notification(R.drawable.ic_launcher, "有新的消息", System.currentTimeMillis());
    		
    		//3步:PendingIntent android系统负责维护
    		PendingIntent pendingIntent=PendingIntent.getActivity(this, 0, getIntent(), 0);
    		//4步: 设置更加详细的信息
    		notification.setLatestEventInfo(this, contentTitle, contentText, pendingIntent);
    		//5步:使用notificationManager对象的notify方法 显示Notification消息   需要制定 Notification的标识
    		notificationManager.notify(notiId, notification);
    	}
    	
    	public void clearNoti(View v){
    		notificationManager.cancelAll();//清除所有
    	}
    
    }
    


    注:模拟器要实现震动需要加权限:DEFAULT_VIBRATE

  • 相关阅读:
    EasyUI的DataGrid 打印导出
    js 验证各种格式类型的正则表达式
    双机热备方案
    使用 IDEA 创建 Maven Web 项目 (异常) Disconnected from the target VM, address: '127.0.0.1:59770', transport: 'socket'
    MySQL 常用函数
    使用 IDEA 创建 Maven Web 项目 (四) 让 WEB 应用跑起来
    使用 IDEA 创建 Maven Web 项目 (二) 搭建 WEB 项目框架
    使用 IDEA 创建 Maven Web 项目 (三) 编写一个简单的 WEB 应用
    Android 开发环境 —— Eclipse 启动时报错:Error when loading the SDK
    使用 EasyUI 创建左侧导航菜单
  • 原文地址:https://www.cnblogs.com/jiangu66/p/3155455.html
Copyright © 2011-2022 走看看