Notification使用



 

代码:
package com.my;

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.provider.ContactsContract.CommonDataKinds.Note;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RemoteViews;
import android.widget.RemoteViews.RemoteView;
import android.widget.TextView;

public class NotificationActivity extends Activity implements OnClickListener {
    private Button btnNotify_all;

    private Button btnNotify_lights;

    private Button btnNotify_sound;

    private Button btnNotify_vibrate;

    private Button btnCancelAll;

    private Button btnCustomview;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        TextView textView = (TextView)findViewById(R.id.tv);
        textView.setText("Buhh");
        btnNotify_all = (Button)findViewById(R.id.btnNotify_all);
        btnNotify_all.setOnClickListener(this);
        btnNotify_lights = (Button)findViewById(R.id.btnNotify_lights);
        btnNotify_lights.setOnClickListener(this);
        btnNotify_sound = (Button)findViewById(R.id.btnNotify_sound);
        btnNotify_sound.setOnClickListener(this);
        btnNotify_vibrate = (Button)findViewById(R.id.btnNotify_vibrate);
        btnNotify_vibrate.setOnClickListener(this);
        btnCancelAll = (Button)findViewById(R.id.btnCancelAll);
        btnCancelAll.setOnClickListener(this);
        btnCustomview = (Button)findViewById(R.id.btnCustomView);
        btnCustomview.setOnClickListener(this);

    }

    private void showSimple(int id) {
        // 第一步
        NotificationManager notificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
        // 第二步
        Notification notification = new Notification(R.drawable.icon, "new message",
                System.currentTimeMillis());
        notification.defaults = id;
        // 第三步
        Intent intent = new Intent(this, NotificationActivity.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
        // 第四步
        notification.setLatestEventInfo(this, "message", "hellow", pendingIntent);
        // 第五步
        notificationManager.notify(R.layout.main, notification);
    }

    private void showLedLight() {
        // 第一步
        NotificationManager notificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
        // 第二步
        Notification notification = new Notification(R.drawable.icon, "new message",
                System.currentTimeMillis());
        // notification.defaults = Notification.DEFAULT_LIGHTS;
        notification.ledARGB = 0xffffff;
        notification.ledOnMS = 300;
        notification.ledOffMS = 1000;
        notification.flags = Notification.FLAG_SHOW_LIGHTS;
        // 第三步
        Intent intent = new Intent(this, NotificationActivity.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
        // 第四步
        notification.setLatestEventInfo(this, "message", "hellow", pendingIntent);
        // 第五步
        notificationManager.notify(R.layout.main, notification);
    }

    private void cancelAll() {
        NotificationManager manager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
        manager.cancelAll();
    }

    private void customView() {
        NotificationManager notificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
        Notification notification = new Notification(R.drawable.icon, "new message",
                System.currentTimeMillis());
        RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.mynotificationview);
        contentView.setImageViewResource(R.id.ivShow, R.drawable.icon);
        notification.contentView = contentView;
        Intent notificationIntent = new Intent(this, NotificationActivity.class);
        PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
        notification.contentIntent = contentIntent;
        notificationManager.notify(0, notification);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btnNotify_all:
                showSimple(Notification.DEFAULT_ALL);
                break;
            case R.id.btnNotify_lights:
                showLedLight();
                break;
            case R.id.btnNotify_sound:
                showSimple(Notification.DEFAULT_SOUND);
                break;
            case R.id.btnNotify_vibrate:
                showSimple(Notification.DEFAULT_VIBRATE);
                break;
            case R.id.btnCustomView:
                customView();
                break;
            case R.id.btnCancelAll:
                cancelAll();
                break;
            default:
                break;
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值