zoukankan      html  css  js  c++  java
  • [Android Pro] Notification的使用

    Android 4.0以前: 

    1: 普通的notification

    private static final int NOTIFY_ID = 0;
    notificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
        private void showNotification(Store store) {
            Notification notification = new Notification();
            notification.flags |= Notification.FLAG_SHOW_LIGHTS;
            notification.flags |= Notification.FLAG_AUTO_CANCEL;
            notification.defaults = Notification.DEFAULT_ALL;
            notification.icon = R.drawable.ic_launch;
            notification.when = System.currentTimeMillis();
    
            Intent intent = new Intent(this,AlarmActivity.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
            intent.putExtra("store", store);
            PendingIntent contentIntent = PendingIntent.getActivity(this, 0,intent, PendingIntent.FLAG_UPDATE_CURRENT);//FLAG_ONE_SHOT
    
             //Change the name of the notification here
            notification.setLatestEventInfo(this, store.getStoreName()+"("+store.getDistance()+")", store.getAddress(), contentIntent);
            notificationManager.notify(NOTIFY_ID, notification);
        }

    2: 将服务service设置为前台notification

    public class MyService extends Service {
    
        @Override
        public void onCreate() {
            super.onCreate();
            setServiceForground();
        }
    
        @SuppressWarnings("deprecation")
        public void setServiceForground() {
            Notification notification = new Notification(R.drawable.ic_launcher,
            "my_service_name", System.currentTimeMillis());
        //要添加newtask PendingIntent p_intent
    = PendingIntent.getActivity(this, 0, new Intent( this, MainActivity.class), 0); notification.setLatestEventInfo(this, "MyServiceNotification", "MyServiceNotification is Running!", p_intent); startForeground(0x1982, notification); }
    @Override
    public IBinder onBind(Intent intent) { return null; } }

    Android 4.0 以后: 

    将服务service设置为前台notification 

    public class MyService extends Service {
    
        @Override
        public void onCreate() {
            super.onCreate();
            setServiceForground();
        }
    
        @Override
        public void onDestroy() {
            super.onDestroy();
        }
    
    
        public void setServiceForground() {
            Notification.Builder build = new Notification.Builder(this);
    //      PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(
    //            this, MainActivity.class), 0);
    //      build.setContentIntent(contentIntent);
            build.setSmallIcon(R.drawable.ic_launcher);    
            build.setContentTitle("MyServiceNotification");
            build.setContentText("MyServiceNotification is Running!");
            startForeground(0x1982, build.build());
        }
    
        @Override
        public IBinder onBind(Intent intent) {
            return null;
        }
    }
  • 相关阅读:
    nginx 正向代理 反向代理 负载均衡
    nginx配置文件常用基本配置指令
    92)http 和https协议入门
    tp6--nginx下pathinfo配置
    tp6省略url里的index.php
    echarts鼠标移上去显示数据
    composer 下载thinkphp6失败
    tp5写入cookie失效
    4.15 Spring Cloud理论基础
    4.14 SpringBoot理论基础
  • 原文地址:https://www.cnblogs.com/0616--ataozhijia/p/4184620.html
Copyright © 2011-2022 走看看