• 我的小闹钟


    public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    //跳到服务
    Intent intent=new Intent(this,AlarmclockSerivce.class);
    startService(intent);

    }
    }

    public class AlarmclockSerivce extends IntentService {

    public AlarmclockSerivce() {
    super("重写父类IntentService");

    }

    @Override
    protected void onHandleIntent(Intent intent) {// 这里是子线程,且不用再关闭了
    AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);

    long triggerAtTime=SystemClock.elapsedRealtime()+1*60*1000;//1min后执行

    Intent i=new Intent(this,AlarmclockReceiver.class);
    PendingIntent pi=PendingIntent.getBroadcast(this, 0, i, 0);

    alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, triggerAtTime, pi);

    Log.i("当前时间", System.currentTimeMillis()+"");

    }

    public class AlarmclockReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {

    NotificationManager notificationManager = (NotificationManager) context
    .getSystemService(Context.NOTIFICATION_SERVICE);
    Notification notification = new Notification.Builder(context)
    .setTicker("闹钟提示").setContentTitle("起床时间")
    .setContentText("主人,今天有重要约会啊")
    .setSmallIcon(android.R.drawable.btn_star)
    .setVibrate(new long[]{0,2000,1000,2000,1000,2000,1000})
    .build();

    notificationManager.notify(1, notification);

    Intent i = new Intent(context, AlarmclockSerivce.class);
    context.startService(i);

    }

    }

  • 相关阅读:
    sql except 用法,找两个表中非共同拥有的
    ‘堆’出你的洪荒之力
    原来你是个这样的JVM
    变形词
    54题
    最大对称子数组
    java 线程之间通信以及notify与notifyAll区别。
    大型网站架构系列:消息队列
    剑指offer第10题
    & 和 && 区别
  • 原文地址:https://www.cnblogs.com/wangfeng520/p/5089126.html
Copyright © 2020-2023  润新知