六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 39|回复: 0

设置定时显示的Notification

[复制链接]

升级  32%

28

主题

28

主题

28

主题

秀才

Rank: 2

积分
98
 楼主| 发表于 2013-1-15 02:37:50 | 显示全部楼层 |阅读模式
注意就是Notification的构造函数的when参数的作用不是定时的作用,所以要运用TimerTask和Timer~
package com.et.TestUi;import java.util.Calendar;import java.util.Date;import java.util.Timer;import java.util.TimerTask;import android.app.Activity;import android.app.AlertDialog;import android.app.DatePickerDialog;import android.app.Dialog;import android.app.Notification;import android.app.NotificationManager;import android.app.PendingIntent;import android.app.TimePickerDialog;import android.app.AlertDialog.Builder;import android.app.TimePickerDialog.OnTimeSetListener;import android.content.DialogInterface;import android.content.Intent;import android.os.Bundle;import android.view.LayoutInflater;import android.view.View;import android.view.View.OnClickListener;import android.view.ViewGroup.LayoutParams;import android.widget.AbsoluteLayout;import android.widget.Button;import android.widget.DatePicker;import android.widget.ImageButton;import android.widget.LinearLayout;import android.widget.TextView;import android.widget.TimePicker;public class TestUi extends Activity {public NotificationManager mNotificationManager;private Button button;protected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);setContentView(R.layout.main);mNotificationManager =(NotificationManager) getSystemService(NOTIFICATION_SERVICE);button = (Button) findViewById(R.id.button);button.setOnClickListener(new OnClickListener() {public void onClick(View v) {// TODO Auto-generated method stubTimer timer = new Timer();timer.schedule(new setNotification("tickrText", "Notititle", "Noticontent"), new Date());}});}public void Notification(String tickrText,String Notititle,String Noticontent){Notification notification = new Notification(R.drawable.stat_notify_more,tickrText,System.currentTimeMillis());PendingIntent contentIntent = PendingIntent.getActivity(this, 0,null, 0);notification.setLatestEventInfo(this, Notititle, Noticontent, contentIntent);notification.defaults = Notification.DEFAULT_ALL;mNotificationManager.notify(0, notification);}public class setNotification extends TimerTask{String tickrText;String Notititle;String Noticontent;public setNotification(String tickrText,String Notititle,String Noticontent){this.tickrText = tickrText;this.Notititle = Notititle;this.Noticontent = Noticontent;}@Overridepublic void run() {// TODO Auto-generated method stubNotification(tickrText,Notititle,Noticontent);}}}

点击button5秒后显示
xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="vertical"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    >    <Button android:id="@+id/button"    android:layout_width="wrap_content"    android:layout_height="wrap_content" android:text="Button"  /></LinearLayout>
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表