• 定制用户自己的信息通知类型


    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);////发出状态栏通知
    }

    }

  • 相关阅读:
    含字母数字的字符串排序算法,仿Windows文件名排序算法
    WCF、WPF、Silverlight和区别(转)
    线程组的介绍
    python基础字符串的修改
    c语言
    python 字典
    单元测试相关
    python列表
    如何才能设计出好的测试用例
    字符串查找
  • 原文地址:https://www.cnblogs.com/xingmeng/p/2425206.html
Copyright © 2020-2023  润新知