zoukankan      html  css  js  c++  java
  • 定制用户自己的信息通知类型

    package src.com;

    import android.app.Notification;
    import android.app.NotificationManager;
    import android.app.PendingIntent;
    import android.app.Service;
    import android.content.Context;
    import android.content.Intent;
    import android.os.IBinder;
    import android.widget.RemoteViews;

    public class NotifyService extends Service {

    private static final int HELO_ID = 1;

    @Override
    public IBinder onBind(Intent intent) {
    // TODO Auto-generated method stub
    return null;
    }

    @Override
    public void onCreate() {
    // TODO Auto-generated method stub
    super.onCreate();
    // Use with getSystemService(String) to retrieve a NotificationManager for informing the user of background events.
    String ns = Context.NOTIFICATION_SERVICE;
    //得到提醒管理对象
    NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);
    CharSequence tickerText = "记得缴钱";
    //String tickerText = "记得缴钱";
    long when = System.currentTimeMillis();//Returns the current system time in milliseconds since January 1, 1970 00:00:00 UTC.

    //Notification A class that represents how a persistent notification is to be presented to the user using the NotificationManager.
    Notification notification = new Notification(R.drawable.ic_launcher, tickerText, when);

    /*
    * 定制用户信息通知的版型
    * 利用RemoteViews创建一个view来指定给contentView
    */
    RemoteViews contentView = new RemoteViews(getPackageName(),R.layout.toast_robot);
    notification.contentView = contentView;

    //Context context = getApplicationContext();//Return the full application info for this context's package.
    //CharSequence contentTitle = "Libro来的提醒";
    //CharSequence contentText = "点击进入缴费清单";
    Intent notificationIntent = new Intent(this, BillList.class);
    //getActivity得到PendingIntent实例,即将进行某种操作
    //getActivity(Context context, int requestCode, Intent intent, int flags)
    //Retrieve a PendingIntent that will start a new activity, like calling Context.startActivity(Intent).
    PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);

    //setLatestEventInfo(Context context, CharSequence contentTitle, CharSequence contentText, PendingIntent contentIntent)
    //notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
    notification.contentIntent = contentIntent;

    //notify(int id, Notification notification)
    //Post a notification to be shown in the status bar.
    mNotificationManager.notify(HELO_ID, notification);////发出状态栏通知
    }

    }

    不一定非要用到setLatestEventInfo方法。

    不过要自定义用户的pendding下拉列表中的用户提醒的话,可以采用RemoteViews

    package src.com;

    import android.app.Notification;
    import android.app.NotificationManager;
    import android.app.PendingIntent;
    import android.app.Service;
    import android.content.Context;
    import android.content.Intent;
    import android.os.IBinder;
    import android.widget.RemoteViews;

    public class NotifyService extends Service {

    private static final int HELO_ID = 1;

    @Override
    public IBinder onBind(Intent intent) {
    // TODO Auto-generated method stub
    return null;
    }

    @Override
    public void onCreate() {
    // TODO Auto-generated method stub
    super.onCreate();
    // Use with getSystemService(String) to retrieve a NotificationManager for informing the user of background events.
    String ns = Context.NOTIFICATION_SERVICE;
    //得到提醒管理对象
    NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);
    CharSequence tickerText = "记得缴钱";
    //String tickerText = "记得缴钱";
    long when = System.currentTimeMillis();//Returns the current system time in milliseconds since January 1, 1970 00:00:00 UTC.

    //Notification A class that represents how a persistent notification is to be presented to the user using the NotificationManager.
    Notification notification = new Notification(R.drawable.ic_launcher, tickerText, when);

    /*
    * 定制用户信息通知的版型
    * 利用RemoteViews创建一个view来指定给contentView
    */
    //getPackageName()指向了applicatio下的包
    RemoteViews contentView = new RemoteViews(getPackageName(),R.layout.toast_robot);
    notification.contentView = contentView;

    //Context context = getApplicationContext();//Return the full application info for this context's package.
    //CharSequence contentTitle = "Libro来的提醒";
    //CharSequence contentText = "点击进入缴费清单";
    Intent notificationIntent = new Intent(this, BillList.class);
    //getActivity得到PendingIntent实例,即将进行某种操作
    //getActivity(Context context, int requestCode, Intent intent, int flags)
    //Retrieve a PendingIntent that will start a new activity, like calling Context.startActivity(Intent).
    PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);

    //setLatestEventInfo(Context context, CharSequence contentTitle, CharSequence contentText, PendingIntent contentIntent)
    //notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
    notification.contentIntent = contentIntent;

    //notify(int id, Notification notification)
    //Post a notification to be shown in the status bar.
    mNotificationManager.notify(HELO_ID, notification);////发出状态栏通知
    }

    }

  • 相关阅读:
    ingress高可用--使用DaemonSet方式部署ingress-nginx
    flask-Migrate模块
    flask 框架 前端和后端请求超时问题
    linux crontab执行python脚本问题
    python 装饰器
    Python 中实现装饰器时使用 @functools.wraps 的理由
    jquery ui dialog 中使用select2 导致select2的input失去焦点的解决方法
    Django之ModelForm组件
    Django之Model操作
    Java环境变量,真的还有必要配吗?
  • 原文地址:https://www.cnblogs.com/xingmeng/p/2425206.html
Copyright © 2011-2022 走看看