android service实现循环定时提醒功能
人每天都要喝8杯水才能保持健康,于是苦逼的程序员总是一遍代码就忘了时间,于是我突发奇想能不能开发一个apk能够实现固定的间隔时间定时提醒我要喝水了呢?
apk基本功能:
1)能够设置间隔时间 2)在apk应用被停止的情况下仍然能定时提醒 3)能够播放指定闹铃 4)能够及时终止提醒
效果图:
设置间隔
时间到后会跳出全局AlertDialog提示并且开始播放闹铃
即使APP被终止了,仍然能够提示
结束提示
废话不多说,直接上代码:
布局layout:
<?xml version="1.0" encoding="utf-8"?
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="bai.cslg.servicebestpractice.MainActivity"
android:baselineAligned="false"
android:orientation="vertical"
<LinearLayout
android:paddingTop="20dp"
android:layout_width="match_parent"
android:layout_height="70dp"
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="5"
android:padding="10dp"
android:gravity="center_vertical"
android:text="请设置提示时间间隔:"
android:textSize="20sp"/
<EditText
android:id="@+id/time"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"/
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:gravity="center_vertical"
android:text="分"
android:textSize="20sp"/
</LinearLayout
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
<Button
android:id="@+id/start_serice"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="开启"/
<Button
android:id="@+id/stop_serice"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="结束"/
</LinearLayout
</LinearLayout
MainActivity代码: *因为要服务常驻后台,就不需要BindService,直接StartService即可 package bai.cslg.servicebestpractice; import android.content.Context; import android.content.Intent; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; public class MainActivity extends AppCompatActivity implements View.OnClickListener{ private Context mContext = MainActivity.this; private Button startService; private Button stopService; private EditText time; public static int TIME; //记录时间间隔 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); startService = (Button) findViewById(R.id.start_serice); stopService = (Button) findViewById(R.id.stop_serice); time = (EditText) findViewById(R.id.time); startService.setOnClickListener(this); stopService.setOnClickListener(this); @Override public void onClick(View view) { switch (view.getId()){ case R.id.start_serice: Intent startIntent = new Intent(this,LongRunningService.class); TIME = Integer.parseInt(time.getText().toString().trim()); //通过Intent将时间间隔传递给Service startIntent.putExtra("Time",TIME); Toast.makeText(MainActivity.this,"开始提醒",Toast.LENGTH_SHORT).show(); startService(startIntent); break; case R.id.stop_serice: Intent stopIntent = new Intent(this,LongRunningService.class); Toast.makeText(MainActivity.this,"结束提醒",Toast.LENGTH_SHORT).show(); stopService(stopIntent); break; }
Service代码:
package bai.cslg.servicebestpractice;
import android.app.AlarmManager;
import android.app.AlertDialog;
import android.app.PendingIntent;
import android.app.Service;
import android.content.DialogInterface;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.os.SystemClock;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.WindowManager;
import java.io.File;
import java.io.IOException;
import java.util.Date;
* Created by baiqihui on 2016/9/21.
public class LongRunningService extends Service {
public int anHour; //记录间隔时间
public int number = 0; //记录alertdialog出现次数
private MediaPlayer mediaPlayer = new MediaPlayer();
AlarmManager manager;
PendingIntent pi;
private Handler mHandler = new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
switch (msg.what){
case 1:
if (!mediaPlayer.isPlaying()){
mediaPlayer.start();
AlertDialog.Builder builder = new AlertDialog.Builder(LongRunningService.this);
builder.setTitle("提醒");
builder.setMessage("该补水啦" + (number-1));
builder.setCancelable(false);
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
mediaPlayer.reset();
initMediaPlayer();
final AlertDialog dialog = builder.create();
dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
dialog.show();
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
@Override
public void onCreate() {
super.onCreate();
initMediaPlayer();
private void initMediaPlayer() {
File file = new File("/storage/emulated/0/naoling","music.mp3");
try {
mediaPlayer.setDataSource(file.getPath());
mediaPlayer.prepare();
} catch (IOException e) {
e.printStackTrace();
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
if (number!=0) {
new Thread(new Runnable() {
@Override
public void run() {
Log.e("bai", "executed at " + new Date().toString());
mHandler.sendEmptyMessage(1);
}).start();
manager = (AlarmManager) getSystemService(ALARM_SERVICE);
int time = intent.getIntExtra("Time",2);
anHour = time*60*1000;
Log.e("bai","Time:"+time+"anhour:"+anHour);
long triggerAtTime = SystemClock.elapsedRealtime()+(anHour);
Intent i = new Intent(this,AlarmReceiver.class);
pi = PendingIntent.getBroadcast(this,0,i,0);
manager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP,triggerAtTime,pi);
number++;
return super.onStartCommand(intent, flags, startId);
@Override
public void onDestroy() {
super.onDestroy();
mediaPlayer.release();
manager.cancel(pi);
}
AlarmReceiver代码:
package bai.cslg.servicebestpractice;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
* Created by baiqihui on 2016/9/21.
public class AlarmReceiver extends BroadcastReceiver {
@Override