android设置日期和时间的程序,android – 设置特定日期和时间的通知

使用在后台运行代码的类AlarmManager.将此代码放在主Activity中:

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.your layout);

boolean alarm = (PendingIntent.getBroadcast(this, 0, new Intent("ALARM"), PendingIntent.FLAG_NO_CREATE) == null);

if(alarm){

Intent itAlarm = new Intent("ALARM");

PendingIntent pendingIntent = PendingIntent.getBroadcast(this,0,itAlarm,0);

Calendar calendar = Calendar.getInstance();

calendar.setTimeInMillis(System.currentTimeMillis());

calendar.add(Calendar.SECOND, 3);

AlarmManager alarme = (AlarmManager) getSystemService(ALARM_SERVICE);

alarme.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),60000, pendingIntent);

}

}

创建一个新类,它将控制是否在一小时通知.

public class BroadcastManager extends BroadcastReceiver {

@Override

public void onReceive(Context context, Intent intent) {

try {

String yourDate = "04/01/2016";

String yourHour = "16:45:23";

Date d = new Date();

DateFormat date = new SimpleDateFormat("dd/MM/yyyy");

DateFormat hour = new SimpleDateFormat("HH:mm:ss");

if (date.equals(yourDate) && hour.equals(yourHour)){

Intent it = new Intent(context, MainActivity.class);

createNotification(context, it, "new mensage", "body!", "this is a mensage");

}

}catch (Exception e){

Log.i("date","error == "+e.getMessage());

}

}

public void createNotification(Context context, Intent intent, CharSequence ticker, CharSequence title, CharSequence descricao){

NotificationManager nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

PendingIntent p = PendingIntent.getActivity(context, 0, intent, 0);

NotificationCompat.Builder builder = new NotificationCompat.Builder(context);

builder.setTicker(ticker);

builder.setContentTitle(title);

builder.setContentText(descricao);

builder.setSmallIcon(R.drawable.yourIcon);

builder.setContentIntent(p);

Notification n = builder.build();

//create the notification

n.vibrate = new long[]{150, 300, 150, 400};

n.flags = Notification.FLAG_AUTO_CANCEL;

nm.notify(R.drawable.yourIcon, n);

//create a vibration

try{

Uri som = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

Ringtone toque = RingtoneManager.getRingtone(context, som);

toque.play();

}

catch(Exception e){}

}

在清单中把这个:

android:name="BroadcastManager"

android:label="BroadcastReceiverAux">

给予此许可:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值