操作通知栏记录

package com.example.android.apis.app;

import com.example.android.apis.R;
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.View;
import android.widget.Button;
import android.widget.RemoteViews;

public class StatusBarNotifications extends Activity {

	//用这个增加、去掉通知栏记录
    private NotificationManager mNotificationManager;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.status_bar_notifications);
        Button button;
        mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        //仅仅显示图片
        button = (Button) findViewById(R.id.happy);
        button.setOnClickListener(new Button.OnClickListener() {
            public void onClick(View v) {
                setMood(R.drawable.stat_happy, R.string.status_bar_notifications_happy_message,
                        false);
            }
        });
        //显示图片及文字
        button = (Button) findViewById(R.id.happyMarquee);
        button.setOnClickListener(new Button.OnClickListener() {
            public void onClick(View v) {
                setMood(R.drawable.stat_happy, R.string.status_bar_notifications_happy_message,
                        true);
            }
        });
        //显示图片及文字
        button = (Button) findViewById(R.id.happyViews);
        button.setOnClickListener(new Button.OnClickListener() {
            public void onClick(View v) {
                setMoodView(R.drawable.stat_happy, R.string.status_bar_notifications_happy_message);
            }
        });
        //图片及文字 与上面的有区别
        button = (Button) findViewById(R.id.defaultSound);
        button.setOnClickListener(new Button.OnClickListener() {
            public void onClick(View v) {
                setDefault(Notification.DEFAULT_SOUND);
            }
        });
        //清除通知栏里的图片及文字
        button = (Button) findViewById(R.id.clear);
        button.setOnClickListener(new Button.OnClickListener() {
            public void onClick(View v) {
                mNotificationManager.cancel(R.layout.status_bar_notifications);
            }
        });
    }
    private PendingIntent makeMoodIntent(int moodId) {
        PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
                new Intent(this, NotificationDisplay.class)
                        .setFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
                        .putExtra("moodimg", moodId),
                PendingIntent.FLAG_UPDATE_CURRENT);
        return contentIntent;
    }
    
    private void setMood(int moodId, int textId, boolean showTicker) {
        CharSequence text = getText(textId);
        String tickerText = showTicker ? getString(textId) : null;
        Notification notification = new Notification(moodId, tickerText,
                System.currentTimeMillis());
        notification.setLatestEventInfo(this, getText(R.string.status_bar_notifications_mood_title),
                       text, makeMoodIntent(moodId));
        mNotificationManager.notify(R.layout.status_bar_notifications, notification);
    }

    private void setMoodView(int moodId, int textId) {
        Notification notif = new Notification();
        notif.contentIntent = makeMoodIntent(moodId);
        CharSequence text = getText(textId);
        notif.tickerText = text;
        notif.icon = moodId;
        RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.status_bar_balloon);
        contentView.setTextViewText(R.id.text, text);
        contentView.setImageViewResource(R.id.icon, moodId);
        notif.contentView = contentView;
        mNotificationManager.notify(R.layout.status_bar_notifications, notif);
    }
    
    private void setDefault(int defaults) {
        PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
                new Intent(this, StatusBarNotifications.class), 0);
        CharSequence text = getText(R.string.status_bar_notifications_happy_message);
        final Notification notification = new Notification(
                R.drawable.stat_happy,
                text,
                System.currentTimeMillis());
        notification.setLatestEventInfo(
                this,
                getText(R.string.status_bar_notifications_mood_title),
                text,
                contentIntent); 

        notification.defaults = defaults;
        
        mNotificationManager.notify(
                   R.layout.status_bar_notifications, 
                   notification);
    }    
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值