Tears_fg

导航

Android_Notification

xml文件:

<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="com.example.notificationdemo.MainActivity" >
    <LinearLayout 
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:gravity="center_horizontal"
        >
        <Button 
            android:id="@+id/send_btn"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="send Message"/>
        <Button 
            android:id="@+id/cancel_btn"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="cancel Message"
            />
    </LinearLayout>
</RelativeLayout>

源代码:

package com.example.notificationdemo;

import android.app.Activity;
import android.app.Notification;
import android.app.Notification.Builder;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

/**
 * Notification是显示在手机状态栏的消息(手机状态栏位于手机最顶端),代表一种全局效果的通知 通知栏的内容:图标,标题,内容,时间,点击后响应
 * 如何实现通知栏: 获取notificationManager 显示通知栏:notify(id,notification) 取消通知栏:cancel(id)
 * 构造Notification并设置显示内容 通知栏通知可以设置声音提示,指示灯以及震动效果,后两者注意需添加权限
 * 
 * 
 * @author Administrator
 *
 */
public class MainActivity extends Activity implements OnClickListener {

    private Button send;
    private Button cancel;
    private NotificationManager manager;
    private int NOTIFICATION_ID;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        send = (Button) findViewById(R.id.send_btn);
        cancel = (Button) findViewById(R.id.cancel_btn);
        send.setOnClickListener(this);
        cancel.setOnClickListener(this);
        manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);//系统常用服务
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        switch (v.getId()) {
        case R.id.send_btn:
            sendNotification();
            break;
        case R.id.cancel_btn:
            manager.cancel(NOTIFICATION_ID);//取消发送的通知
            break;
        }
    }

    /**
     * 构造notification并发送到通知栏
     */
    private void sendNotification() {
        // TODO Auto-generated method stub
        Intent intent = new Intent(this, MainActivity.class);
        PendingIntent pintent = PendingIntent.getActivity(this, 0, intent, 0);
        Builder builder = new Notification.Builder(this);
        builder.setSmallIcon(R.drawable.ic_launcher);// 设置图标
        builder.setTicker("hello");// 设置手机状态栏的提示
        // 拖拽开手机状态栏提示的内容
        builder.setWhen(System.currentTimeMillis());// 设置时间
        builder.setContentTitle("通知");// 设置标题
        builder.setContentText("内容");// 设置内容
        builder.setContentIntent(pintent);// 设置点击之后的响应,意图

        /*
         * builder.setDefaults(Notification.DEFAULT_SOUND);//设置声音
         * builder.setDefaults(Notification.DEFAULT_LIGHTS);//设置指示灯
         * builder.setDefaults(Notification.DEFAULT_VIBRATE);//设置震动
         */
        builder.setDefaults(Notification.DEFAULT_ALL);
        Notification notification = builder.getNotification();// 4.1以下
        // builder.build();//4.1及以上
        manager.notify(NOTIFICATION_ID, notification);//发送通知到通知栏
    }

}

 

posted on 2016-06-14 12:59  Tears_fg  阅读(187)  评论(0编辑  收藏  举报