设置定时显示的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 stub
super.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 stub
Timer 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;
}
@Override
public void run() {
// TODO Auto-generated method stub
Notification(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>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值