Notification

权限设置:
<uses-permission android:name="android.permission.FLASHLIGHT"/>//闪光灯
    <uses-permission android:name="android.permission.VIBRATE"/>//震动
package com.example.notification;

import android.R.integer;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Notification.*;
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;


public class MainActivity extends Activity {
	private Button button1;
	private Button button2;
	NotificationManager manager;
	int notification_id;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        manager=(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
        button1=(Button) findViewById(R.id.button1);
        button1.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				notification();
			}
		});
        button2=(Button) findViewById(R.id.button2);
        button2.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				cancel();
			}
		});
    }
//构造notofication并发送到通知栏
    private void notification(){
    	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("我来自Notification");//设置通知内容
    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.build();//4.1以上版本用
    Notification notification=builder.getNotification();//4.1以下版本用
    manager.notify(notification_id,notification);
    }
    //取消通知栏通知
    private void cancel(){
    	manager.cancel(notification_id);
    }



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值