码迷,mamicode.com
首页 > 其他好文 > 详细

Notification发送通知

时间:2015-08-21 00:21:20      阅读:233      评论:0      收藏:0      [点我收藏+]

标签:notification   notificationmanager   

今天学习并测试了Notification组件,这个组件在应用中也经常用到。在这里写了一个简单的Demo。

Notification是显示在状态栏的消息----位于手机屏幕的最上方。
程序一般通过NotificationManager服务来发送Notification。
Notification发送Notification的步骤
1、调用getSystemService(NOTIFICATION_SERVICE)方法获取系统
NotificationManager服务
2、通过构造器创建一个Notification对象
3、为Notification设置各种属性
4、通过NotificationManerger发送Notification


在AndroidManifest.xml文件中设置权限:

 <!-- 设置闪光灯权限-->
    <uses-permission android:name="android.permission.FLASHLIGHT"></uses-permission>
    <!--设置震动权限-->
    <uses-permission android:name="ANDROID.PERMISSION.VIBRATE"></uses-permission>

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:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
<Button
    android:id="@+id/startNotify"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="开启通知"
    />
    <Button
        android:id="@+id/stopNotify"
        android:layout_width="wrap_content"
        android:layout_toRightOf="@+id/startNotify"
        android:layout_height="wrap_content"
        android:text="关闭通知"/>
</RelativeLayout>
MainActivity.java

package lzl.edu.com.notificationtest;

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.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;

public class MainActivity extends Activity implements View.OnClickListener{

    private static final int NOTIFICATION = 1;
    Button startNotify,stopNotify;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        startNotify =(Button)findViewById(R.id.startNotify);
        stopNotify=(Button)findViewById(R.id.stopNotify);
        startNotify.setOnClickListener(this);
        stopNotify.setOnClickListener(this);
    }
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.startNotify:
                Intent intent = new Intent(MainActivity.this,NextActivity.class);
                PendingIntent pi = PendingIntent.getActivity(MainActivity.this, 0, intent, 0);
                //创建一个Notification
                Notification notification = new Notification();
                //为Notification设置图标
                notification.icon = R.mipmap.ic_launcher;
                //设置文本内容
                notification.tickerText="启动另一个应用的通知";
                //设置发送年时间
                notification.when = System.currentTimeMillis();
                //设置声音
                notification.defaults = notification.DEFAULT_SOUND;
                //设置默认声音、震动、闪光灯
                notification.defaults=notification.DEFAULT_ALL;
                notification.setLatestEventInfo(MainActivity.this,"一条应用的通知","点击查看",pi);
                //获取系统的NotificationManerger服务
                NotificationManager notificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
                notificationManager.notify(NOTIFICATION,notification);
                break;
            case R.id.stopNotify:
                NotificationManager notificationManager1 = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
                    notificationManager1.cancel(NOTIFICATION);
                break;
            default:break;
        }
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

}



版权声明:本文为博主原创文章,未经博主允许不得转载。

Notification发送通知

标签:notification   notificationmanager   

原文地址:http://blog.csdn.net/u011521890/article/details/47816781

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!