android service实现循环定时提醒功能

Nafisa ·
更新时间:2024-09-21
· 821 次阅读

人每天都要喝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 public void onReceive(Context context, Intent intent) { Intent i = new Intent(context, LongRunningService.class); context.startService(i); } }

1)在apk应用被停止的情况下仍然能定时提醒,这里采用startService即可实现,使service常驻内存,即使Activity被杀死,依旧可以执行。

2)间隔时间提醒。这里采用的是Android的Alarm机制。

 Android中的定时任务一般有两种实现方式,一种是使用Java API里提供的Timer类,一种是使用Android的Alarm机制。这两种情况在多数情况下都能实现类似的效果,但Timer类有一个明显的短板,它并不太适用于那些需要长期在后台运行的定时任务。我们都知道,为了能让电池更耐用,每种手机都会有自己的休眠策略,Android手机就会在长时间不操作的情况下自动让CPU进入到睡眠状态,这就有可能导致Timer中的定时任务无法正常运行。而Alarm机制则不存在这种情况,它具有唤醒CPU的功能,即可以保证每次需要执行定时任务的时候CPU都能正常工作。需要注意,这里唤醒CPU和唤醒屏幕完全不是一个概念。

从Service代码中可以看出,onCreate()中完成对mediaPlayer的初始化(因为mediaPlayer只需要初始化一次),在onStartCommand()中开启一个新的线程,线程中通过handler发送一条空的消息,并且在handler的handleMessage()方法中完成AlertDialog的创建以及播放闹铃,要注意这里创建的是一个全局的AlertDialog。因为第一次开启任务的时候不需要新建一个AlertDialog(用户第一次开启任务的时候是设置好时间并且点击了“开启”,这个时候不需要创建Dialog)。

在onStartCommand()还执行了AlarmManager的初始化以及时间的设定,因为AlarmManager中第三个参数PendingIntent能够执行一个广播,所以还需要写一个广播接收者。

AlarmManager的取消:manager.cancel(PendingIntent pi);取消对应PendingIntent即可。

AlarmReceiver:这就很简单了,接收到广播之后开启再开启服务即可。这就详单与是一个死循环,服务开启后会定时发送广播,广播接收到之后又会开启服务。

因为时间有限,所以代码肯定有很多不完善之处,希望多多指教。

您可能感兴趣的文章:Android 使用viewpager实现无限循环(定时+手动)Android AlarmManager实现定时循环后台任务



android service service 循环 Android

需要 登录 后方可回复, 如果你还没有账号请 注册新账号