1.布局文件(activity_main.xml)

<?xml version="1.0" encoding="utf-8"?>    

 

2.代码

1.MainActivity

public class MainActivity extends AppCompatActivity {    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        Button btnStart = findViewById(R.id.start);        Button btnCancel = findViewById(R.id.cancel);        btnStart.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                Intent intent = new Intent(MainActivity.this, MyService.class);                startService(intent);            }        });        btnCancel.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                AlarmManager manager = (AlarmManager) getSystemService(ALARM_SERVICE);                Intent i = new Intent(MainActivity.this, MyReceiver.class);                PendingIntent pi = PendingIntent.getBroadcast(MainActivity.this, 0, i, 0);                manager.cancel(pi);            }        });    }}

2.MyService

public class MyService extends Service {    public MyService() {    }    @Override    public IBinder onBind(Intent intent) {        return null;    }    @Override    public int onStartCommand(Intent intent, int flags, int startId) {        Log.d("MyService", "onStartCommand()");        AlarmManager manager = (AlarmManager) getSystemService(ALARM_SERVICE);        Calendar cal = Calendar.getInstance();        cal.set(Calendar.DAY_OF_YEAR, 0);        cal.set(Calendar.HOUR_OF_DAY, 12);        cal.set(Calendar.SECOND, 0);        cal.set(Calendar.MINUTE, 0);        cal.set(Calendar.MILLISECOND, 0);        //六日        long time = 60 * 60 * 24 * 10 * 1000;        long rTime = cal.getTimeInMillis() + time - System.currentTimeMillis();        //SystemClock.elapsedRealtime()表示1970年1月1日0点至今所经历的时间        long triggerAtTime = SystemClock.elapsedRealtime() + rTime;        //此处设置开启AlarmReceiver这个Service        Intent i = new Intent(this, MyReceiver.class);        PendingIntent pi = PendingIntent.getBroadcast(this, 0, i, 0);        //ELAPSED_REALTIME_WAKEUP表示让定时任务的出发时间从系统开机算起,并且会唤醒CPU。        manager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, triggerAtTime, pi);        return super.onStartCommand(intent, flags, startId);    }    @Override    public void onDestroy() {        super.onDestroy();        Log.d("MyService", "onDestroy()");    }}

3.MyReceiver

public class MyReceiver extends BroadcastReceiver {    private Context context = null;    private int REQUEST_CODE_CAMERA = 1;    @Override    public void onReceive(Context context, Intent intent) {        this.context = context;                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {            String channelId = "remind";            String channelName = "提醒消息";            int importance = NotificationManager.IMPORTANCE_HIGH;            createNotificationChannel(channelId, channelName, importance);            NotificationManager manager = (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE);            Intent tmpintent = new Intent(context, MainActivity.class);            PendingIntent pIntent = PendingIntent.getActivity(context, 100, tmpintent, 0);            Notification notification = new NotificationCompat.Builder(context, "remind")//创建通知消息实例                    .setContentTitle("收到一条提醒消息")                    .setContentText("提醒内容")                    .setWhen(System.currentTimeMillis())//通知栏显示时间                    .setSmallIcon(R.mipmap.ic_launcher)//通知栏小图标                    .setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.mipmap.ic_launcher_round))//通知栏大图标                    .setContentIntent(pIntent)//关联点击通知栏跳转页面                    .setAutoCancel(true)//设置点击通知栏消息后,通知消息自动消失                    //.setVibrate(new long[]{0, 1000, 1000, 1000}) //通知栏消息震动                    //.setLights(Color.GREEN, 1000, 2000) //通知栏消息闪灯(亮一秒间隔两秒再亮)                    .setDefaults(NotificationCompat.DEFAULT_ALL) //通知栏提示音、震动、闪灯等都设置为默认                    .build();            manager.notify(REQUEST_CODE_CAMERA, notification);        } else {            NotificationManager manager = (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE);            Intent tmpintent = new Intent(context, MainActivity.class);            PendingIntent pIntent = PendingIntent.getActivity(context, 100, tmpintent, 0);            Notification notification = new Notification.Builder(context)                    .setContentTitle("收到一条提醒消息")                    .setContentText("提醒内容")                    .setSmallIcon(R.mipmap.ic_launcher)                    .setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.mipmap.ic_launcher_round))                    .setContentIntent(pIntent)                    .setAutoCancel(true)                    .setDefaults(Notification.DEFAULT_ALL)                    .build();            manager.notify(REQUEST_CODE_CAMERA, notification);        }        wakeUpAndUnlock();    }    @RequiresApi(api = Build.VERSION_CODES.O)    private void createNotificationChannel(String channelId, String channelName, int importance) {        NotificationChannel channel = new NotificationChannel(channelId, channelName, importance);        NotificationManager notificationManager = (NotificationManager) context.getSystemService(                NOTIFICATION_SERVICE);        notificationManager.createNotificationChannel(channel);    }    //唤醒屏幕并解锁    public void wakeUpAndUnlock() {        KeyguardManager km = (KeyguardManager) context.getSystemService(Context.KEYGUARD_SERVICE);        KeyguardManager.KeyguardLock kl = km.newKeyguardLock("unLock");        //解锁        kl.disableKeyguard();        //获取电源管理器对象        PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);        //获取PowerManager.WakeLock对象,后面的参数|表示同时传入两个值,最后的是LogCat里用的Tag        @SuppressLint("InvalidWakeLockTag")        PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.ACQUIRE_CAUSES_WAKEUP | PowerManager.SCREEN_DIM_WAKE_LOCK, "bright");        //点亮屏幕        wl.acquire();        //释放        wl.release();    }}

4.AndroidManifest.xml

 

 

更多相关文章

  1. Android显示横幅样式通知
  2. android 跳转到应用通知设置界面
  3. Android UI 之时间与日期控件
  4. Android Framework分析 ---- 1消息处理机制 java层
  5. Android的消息机制,用Android线程间通信的Message机制,Android中Ha
  6. android 时间获取以及时间格式化
  7. Android即时消息介绍
  8. Android Handler消息机制从原理到应用详解

随机推荐

  1. Android的线程使用来更新UI----Thread、H
  2. 教你如何修改Android默认字体大小和设置
  3. android两次点击的焦点问题
  4. Could not find method android() for ar
  5. 转:Android电话系统之-rild
  6. android中Parcelable接口的使用
  7. Android中为APP创建快捷方式的原理(自己的
  8. AndroidHttpClient使用Cookie应用分析
  9. android的selector背景选择器
  10. Android(安卓)路由框架ARouter详细使用教