ET焖猪仔 发表于 2013-2-7 10:02:21

设置定时显示的Notification

注意就是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>
页: [1]
查看完整版本: 设置定时显示的Notification