Home>

premise:Considering that I always pay attention to time every time I type a code or play a game,Without paying attention to the computer for more than 3 hours,The damage to the eyes is still quite large,Severe myopia, but it will be passed on to future children,Maybe his wife ran away with others.

So, to protect the eyes,I made the following application:

Effect after opening:

There is a background reminder after the time is up:

OK, let's talk about the main points of knowledge involved in making such an app:

service:Using service, when the program is running in the background,Can also make corresponding reminders,And does not affect the phone to do other work.

alarmmanager:This knowledge is mainly used for timing.The details are written in the comments of the code.

notification:This knowledge is used as a notification display.Specific can refer to another article:

mainactivity:

import android.app.activity;
import android.content.intent;
import android.os.bundle;
import android.view.window;
import android.widget.toast;
public class mainactivity extends activity {
 private intent intent;
 @override
 protected void oncreate (bundle savedinstancestate) {
  super.oncreate (savedinstancestate);
  //Cancel the title bar
  requestwindowfeature (window.feature_no_title);
  //Because it is mainly used for testing,And easy for newbies to understand,So the activity_main layout is very simple
  setcontentview (r.layout.activity_main);
  intent=new intent (this, longrunningservice.class);
  //Open and close the service
  startservice (intent);
  //Set a toast to remind users that the reminder function has begun
  toast.maketext (mainactivity.this, "The reminder feature is turned on,Closing the interface will cancel the reminder.
", toast.length_long) .show ();
 }
 @override
 protected void ondestroy () {
  super.ondestroy ();
  //After the activity is closed,Close service
  stopservice (intent);
 }
}

longrunningservice:

import android.app.alarmmanager;
import android.app.pendingintent;
import android.app.service;
import android.content.intent;
import android.os.ibinder;
import android.os.systemclock;
public class longrunningservice extends service {
 @override
 public ibinder onbind (intent intent) {
  return null;
 }
 @override
 public int onstartcommand (intent intent, int flags, int startid) {
  alarmmanager manager=(alarmmanager) getsystemservice (alarm_service);
  //Readers can modify the minutes here to change the reminder interval
  //here is set to start every 90 minutes
  //This is the number of milliseconds for 90 minutes
  int minutes=90 * 60 * 1000;
  //systemclock.elapsedrealtime () represents the time elapsed since 00:00 on January 1, 1970
  long triggerattime=systemclock.elapsedrealtime () + minutes;
  //Set the alarmreceiver service here
  intent i=new intent (this, alarmreceiver.class);
  pendingintent pi=pendingintent.getbroadcast (this, 0, i, 0);
  //elapsed_realtime_wakeup indicates that the starting time of the scheduled task is counted from the system startup.
And will wake up the cpu.
  manager.set (alarmmanager.elapsed_realtime_wakeup, triggerattime, pi);
  return super.onstartcommand (intent, flags, startid);
 }
 @override
 public void ondestroy () {
  super.ondestroy ();
  //Close alarmmanager after service ends
  alarmmanager manager=(alarmmanager) getsystemservice (alarm_service);
  intent i=new intent (this, alarmreceiver.class);
  pendingintent pi=pendingintent.getbroadcast (this, 0, i, 0);
  manager.cancel (pi);
 }
}

alarmreceiver:

import android.app.notification;
import android.app.notificationmanager;
import android.content.broadcastreceiver;
import android.content.context;
import android.content.intent;
public class alarmreceiver extends broadcastreceiver {
 @override
 public void onreceive (context context, intent intent) {
  //Set the notification content and turn on when the onreceive () function is executed
  notificationmanager manager=(notificationmanager) context.getsystemservice (context.notification_service);
  notification notification=new notification (r.drawable.ic_launcher, "Too much time using a computer! Idiot!"
  , system.currenttimemillis ());
  notification.setlatesteventinfo (context, "Go to rest !!!",    "Be sure to protect your eyes,Otherwise passed on to the child,My wife runs with others.
", null);
  notification.defaults=notification.default_all;
  manager.notify (1, notification);
  //Open the longrunningservice service again,So that
  intent i=new intent (context, longrunningservice.class);
  context.startservice (i);
 }
}

activity_main:

<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:padding="15dp"
 android:orientation="vertical"
 >
 <textview
  android:layout_marginbottom="20dp"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:text="Eye protection timing reminder"
  android:textsize="30sp"
  android:gravity="center_horizontal"
  />
 <textview
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:text="Reminder interval:"
  android:textsize="25sp"
  />
 <textview
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:text="90 minutes"
  android:textsize="25sp"
  />
 <textview
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:text="Reminder Music:"
  android:textsize="25sp"
  />
 <textview
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:text="System default music"
  android:textsize="25sp"
  />
</linearlayout>

Don't forget to register the service in the androidmanifest!

androidmanifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
 package="com.example.servicebestpractice"
 android:versioncode="1"
 android:versionname="1.0">
 <uses-sdk
  android:minsdkversion="14"
  android:targetsdkversion="17" />
 <application
  android:allowbackup="true"
  android:icon="@ drawable/ic_launcher"
  android:label="@ string/app_name"
  android:theme="@ style/apptheme">
  <activity
   android:name="com.example.servicebestpractice.mainactivity"
   android:label="@ string/app_name">
   <intent-filter>
    <action android:name="android.intent.action.main" />
    <category android:name="android.intent.category.launcher" />
   </intent-filter>
  </activity>
  <service android:name=". Longrunningservice">
  </service>
  <receiver android:name=". Alarmreceiver">
  </receiver>
 </application>
</manifest>

There is a point to note here,The original code of the author is to automatically start the service when the activity is started, and automatically destroy the service when the activity is destroyed.Nothing is wrongAnd the reminders within 10 minutes basically work normally.

ButIf there is a problem of not reminding when it is reminded for a long time! why?

Because Android optimizes memory,Reduce power consumption,Will automatically clean up the memory,Will clean up the background service.

  • Previous In-depth analysis of the usage and differences of sprintf and printf functions in PHP
  • Next Solution to writing java programs under Eclipse does not automatically generate Rjava files and packages