android 自定义通知

package cn.qing.learndemo;


import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.TaskStackBuilder;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.app.NotificationCompat;
import android.view.View;
import android.widget.RemoteViews;




public class NotificationActivity extends AppCompatActivity {


    private NotificationManager notificationManager;




    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_notification);
        notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        System.out.println("classLoader:"+Thread.currentThread().getContextClassLoader());


    }


    private void simpleNotification() {
        Bitmap icon = BitmapFactory.decodeResource(getResources(), R.mipmap.icon);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 100, new Intent(this, MainActivity.class),
                PendingIntent.FLAG_ONE_SHOT);


        NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
        builder.setContentTitle("通知title")
                .setContentText("最新通知栏消息,来自通知")
                .setWhen(System.currentTimeMillis())
                .setOngoing(true)
                .setLargeIcon(icon)
                .setDefaults(Notification.DEFAULT_ALL)
                .setSmallIcon(R.mipmap.ic_question)
                .setContentIntent(pendingIntent);


        Notification notification = builder.build();
        notification.flags = Notification.FLAG_AUTO_CANCEL;
        notificationManager.notify(1, notification);
    }


    public void showSimpleNotification(View view) {


        simpleNotification();
    }


    public void showMutilNotification(View view) {
        Bitmap icon = BitmapFactory.decodeResource(getResources(), R.mipmap.icon);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 100, new Intent(this, NotificationActivity.class),
                PendingIntent.FLAG_ONE_SHOT);


        Intent resultIntent = new Intent(this, NotificationActivity.class);
        resultIntent.setFlags(Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
        TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
        stackBuilder.addParentStack(MainActivity.class);
        stackBuilder.addNextIntent(resultIntent);


        PendingIntent pi = stackBuilder.getPendingIntent(100, PendingIntent.FLAG_UPDATE_CURRENT);


        String msg = "最新通知栏消息,来自通知,这里还有很多很多内容需要显示,这里还有很多很多内容需要显示,这里还有很多很多内容需要显示,这里还有很多很多内容需要显示,这里还有很多很多内容需要显示";
        NotificationCompat.BigTextStyle bigTextStyle = new android.support.v4.app.NotificationCompat.BigTextStyle();
        bigTextStyle.bigText(msg);
        bigTextStyle.setSummaryText("点击查看更多详情");
        bigTextStyle.setBigContentTitle("通知");


        NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
        builder.setContentTitle("通知")
                .setWhen(System.currentTimeMillis())
                .setOngoing(true)
                .setLargeIcon(icon)
                .setContentText(msg)
                .setStyle(bigTextStyle)
                .setDefaults(Notification.DEFAULT_ALL)
                .setSmallIcon(R.mipmap.ic_question)
                .setContentIntent(pi);


        Notification notification = builder.build();
        notification.flags = Notification.FLAG_AUTO_CANCEL;
        notificationManager.notify(2, notification);
    }


    public void showPriceNotification(View view) {
        Bitmap icon = BitmapFactory.decodeResource(getResources(), R.mipmap.icon);
        Bitmap priceBitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.my_top_bg);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 100, new Intent(this, NotificationActivity.class),
                PendingIntent.FLAG_ONE_SHOT);


        String msg = "最新通知栏消息,来自通知,这里还有很多很多内容需要显示,这里还有很多很多内容需要显示,这里还有很多很多内容需要显示,这里还有很多很多内容需要显示,这里还有很多很多内容需要显示";


        NotificationCompat.BigPictureStyle bigPictureStyle = new android.support.v4.app.NotificationCompat.BigPictureStyle();
        bigPictureStyle.setBigContentTitle("通知");
        bigPictureStyle.setSummaryText("点击查看更多详情");
        bigPictureStyle.bigLargeIcon(icon);
        bigPictureStyle.bigPicture(priceBitmap);


        NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
        builder.setContentTitle("通知")
                .setWhen(System.currentTimeMillis())
                .setOngoing(true)
                .setLargeIcon(icon)
                .setContentText(msg)
                .setStyle(bigPictureStyle)
                .setDefaults(Notification.DEFAULT_ALL)
                .setSmallIcon(R.mipmap.ic_question)
                .setContentIntent(pendingIntent);


        Notification notification = builder.build();
        notification.flags = Notification.FLAG_AUTO_CANCEL;
        notificationManager.notify(3, notification);
    }


    public void showSelfNotification(View view) {
        Bitmap icon = BitmapFactory.decodeResource(getResources(), R.mipmap.icon);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 100, new Intent(this, MainActivity.class),
                PendingIntent.FLAG_ONE_SHOT);


        RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.notification);
        String msg = "最新通知栏消息,来自通知,这里还有很多很多内容需要显示,这里还有很多很多内容需要显示,这里还有很多很多内容需要显示,这里还有很多很多内容需要显示,这里还有很多很多内容需要显示";


        NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
        builder.setWhen(System.currentTimeMillis())
                .setContentTitle("通知")
                .setContentText(msg)
                .setLargeIcon(icon)
                .setContentIntent(pendingIntent)
                .setDefaults(Notification.DEFAULT_ALL)
                .setSmallIcon(R.mipmap.ic_question);


        Notification notification = builder.build();
        notification.flags = Notification.FLAG_AUTO_CANCEL;
        if (Build.VERSION.SDK_INT >= 16) {
            notification.bigContentView = remoteViews;
        }
        notificationManager.notify(5, notification);


    }




    public static final int PLAYER_NOTIFY_ID = 10;


    /**
     * 一个播放器通知
     *
     * @param view
     */
    public void showPlayerNotification(View view) {


        RemoteViews normalView = new RemoteViews(getPackageName(), R.layout.player_notification_normal);
        RemoteViews bigView = new RemoteViews(getPackageName(), R.layout.player_notification_big);


        Intent closeIntent = new Intent(PlayerBroadCaseReceiver.ACTION_PLAYER_NOTIFICATION);
        closeIntent.putExtra(PlayerBroadCaseReceiver.KEY_OPTIONS, "close");
        PendingIntent closePendingIntent = PendingIntent.getBroadcast(this, 1, closeIntent, PendingIntent.FLAG_UPDATE_CURRENT);


        Intent playIntent = new Intent(PlayerBroadCaseReceiver.ACTION_PLAYER_NOTIFICATION);
        playIntent.putExtra(PlayerBroadCaseReceiver.KEY_OPTIONS, "play");
        PendingIntent playPendingIntent = PendingIntent.getBroadcast(this, 2, playIntent, PendingIntent.FLAG_UPDATE_CURRENT);


        Intent preIntent = new Intent(PlayerBroadCaseReceiver.ACTION_PLAYER_NOTIFICATION);
        preIntent.putExtra(PlayerBroadCaseReceiver.KEY_OPTIONS, "pre");
        PendingIntent prePendingIntent = PendingIntent.getBroadcast(this, 3, preIntent, PendingIntent.FLAG_UPDATE_CURRENT);


        Intent nextIntent = new Intent(PlayerBroadCaseReceiver.ACTION_PLAYER_NOTIFICATION);
        nextIntent.putExtra(PlayerBroadCaseReceiver.KEY_OPTIONS, "next");
        PendingIntent nextPendingIntent = PendingIntent.getBroadcast(this, 4, nextIntent, PendingIntent.FLAG_UPDATE_CURRENT);




        normalView.setOnClickPendingIntent(R.id.ivClose, closePendingIntent);
        normalView.setOnClickPendingIntent(R.id.ivPlay, playPendingIntent);
        normalView.setOnClickPendingIntent(R.id.ivNext, nextPendingIntent);


        bigView.setOnClickPendingIntent(R.id.ivClose, closePendingIntent);
        bigView.setOnClickPendingIntent(R.id.ivPlay, playPendingIntent);
        bigView.setOnClickPendingIntent(R.id.ivNext, nextPendingIntent);
        bigView.setOnClickPendingIntent(R.id.ivPre, prePendingIntent);


        NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
        builder.setWhen(System.currentTimeMillis())
                .setDefaults(Notification.DEFAULT_ALL)
                .setSmallIcon(R.mipmap.icon);
        Notification notification = builder.build();
        notification.flags = Notification.FLAG_NO_CLEAR;
        notification.contentView = normalView;
        if (Build.VERSION.SDK_INT >= 16) {
            notification.bigContentView = bigView;
        }
        notificationManager.notify(PLAYER_NOTIFY_ID, notification);
    }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值