notification的使用

来源:互联网 发布:mac怎么打开mpp文件 编辑:程序博客网 时间:2024/05/18 05:11
notification的使用
1.获取到一个notification服务
2.准备一个notification的信息
3.准备notification的具体信息和点击事件
4.把构建的notification传给notification管理器

public class MainActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
    
    public void click(View view){
    //1.获取到一个 notification的服务 
    NotificationManager manager = (NotificationManager) this.getSystemService(NOTIFICATION_SERVICE);
    //2 .准备一个notification的信息 
    Notification notification = new Notification(R.drawable.notification, "我是notification", System.currentTimeMillis());
    //3. 准备notification的具体信息 和 点击事件
    notification.flags=Notification.FLAG_NO_CLEAR;//设置点击"清除"的操作
    
    Intent intent = new Intent();
    intent.setAction(Intent.ACTION_CALL);
    intent.setData(Uri.parse("tel:110"));
    //延期的意图 
    PendingIntent contentIntent = PendingIntent.getActivity(this, 0, intent, 0);
    notification.setLatestEventInfo(this, "我是title", "我是文本", contentIntent);
    
    //4.把我们构建的notification传个notification管理器
    manager.notify(0, notification);
    }
    
     //自定义一个notification
    public void click1(View view){
    //1.获取到一个 notification的服务 
    NotificationManager manager = (NotificationManager) this.getSystemService(NOTIFICATION_SERVICE);
    // 远程的view
    Notification notification = new Notification();
    RemoteViews rv = new RemoteViews(getPackageName(), R.layout.noti);  //要准备一个notification的布局文件
    rv.setTextViewText(R.id.text, "text");   //给远程的控件设置属性.使用到进程间的通信
    rv.setTextViewText(R.id.title, "title");
    //2.创建一个显示的notification

    Intent intent = new Intent();
    intent.setAction(Intent.ACTION_CALL);
    intent.setData(Uri.parse("tel:110"));
    //延期的意图 
    PendingIntent contentIntent = PendingIntent.getActivity(this, 0, intent, 0);
    //3.指定显示的内容
    notification.contentView = rv;
    notification.contentIntent = contentIntent;
    notification.icon = R.drawable.notification;
    notification.tickerText="haha";
    notification.flags = Notification.FLAG_AUTO_CANCEL;
    manager.notify(0, notification);
    
    }
    
}
原创粉丝点击