Android后台定时提醒功能实现

2019-12-10 19:26:07王振洲

activity_main:

<LinearLayout xmlns:android="http://www.easck.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="护眼定时提醒" 
  android:textSize="30sp" 
  android:gravity="center_horizontal" 
  /> 
  
 
 <TextView 
  android:layout_width="match_parent" 
  android:layout_height="wrap_content" 
  android:text="提醒间隔时间:" 
  android:textSize="25sp" 
  /> 
 
 <TextView 
  android:layout_width="match_parent" 
  android:layout_height="wrap_content" 
  android:text="90分钟" 
  android:textSize="25sp" 
  /> 
 
 <TextView 
  android:layout_width="match_parent" 
  android:layout_height="wrap_content" 
  android:text="提醒音乐:" 
  android:textSize="25sp" 
  /> 
 <TextView 
  android:layout_width="match_parent" 
  android:layout_height="wrap_content" 
  android:text="系统默认音乐" 
  android:textSize="25sp" 
  /> 
</LinearLayout> 

千万不要忘了在AndroidManifest中注册Service!
AndroidManifest:

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://www.easck.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>