人每天都要喝8杯水才能保持健康,于是苦逼的程序員總是一遍代碼就忘了時間,于是我突發(fā)奇想能不能開發(fā)一個apk能夠?qū)崿F(xiàn)固定的間隔時間定時提醒我要喝水了呢?
apk基本功能:
1)能夠設(shè)置間隔時間 2)在apk應(yīng)用被停止的情況下仍然能定時提醒 3)能夠播放指定鬧鈴 4)能夠及時終止提醒
效果圖:
設(shè)置間隔

時間到后會跳出全局AlertDialog提示并且開始播放鬧鈴

即使APP被終止了,仍然能夠提示

結(jié)束提示

廢話不多說,直接上代碼:
布局layout:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android/198548.html">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="請設(shè)置提示時間間隔:" 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="結(jié)束"/> </LinearLayout></LinearLayout>
MainActivity代碼:
/**因為要服務(wù)常駐后臺,就不需要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,"結(jié)束提醒",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出現(xiàn)次數(shù) 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("該補(bǔ)水啦" + (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應(yīng)用被停止的情況下仍然能定時提醒,這里采用startService即可實現(xiàn),使service常駐內(nèi)存,即使Activity被殺死,依舊可以執(zhí)行。
2)間隔時間提醒。這里采用的是Android的Alarm機(jī)制。
Android中的定時任務(wù)一般有兩種實現(xiàn)方式,一種是使用Java API里提供的Timer類,一種是使用Android的Alarm機(jī)制。這兩種情況在多數(shù)情況下都能實現(xiàn)類似的效果,但Timer類有一個明顯的短板,它并不太適用于那些需要長期在后臺運(yùn)行的定時任務(wù)。我們都知道,為了能讓電池更耐用,每種手機(jī)都會有自己的休眠策略,Android手機(jī)就會在長時間不操作的情況下自動讓CPU進(jìn)入到睡眠狀態(tài),這就有可能導(dǎo)致Timer中的定時任務(wù)無法正常運(yùn)行。而Alarm機(jī)制則不存在這種情況,它具有喚醒CPU的功能,即可以保證每次需要執(zhí)行定時任務(wù)的時候CPU都能正常工作。需要注意,這里喚醒CPU和喚醒屏幕完全不是一個概念。
從Service代碼中可以看出,onCreate()中完成對mediaPlayer的初始化(因為mediaPlayer只需要初始化一次),在onStartCommand()中開啟一個新的線程,線程中通過handler發(fā)送一條空的消息,并且在handler的handleMessage()方法中完成AlertDialog的創(chuàng)建以及播放鬧鈴,要注意這里創(chuàng)建的是一個全局的AlertDialog。因為第一次開啟任務(wù)的時候不需要新建一個AlertDialog(用戶第一次開啟任務(wù)的時候是設(shè)置好時間并且點擊了“開啟”,這個時候不需要創(chuàng)建Dialog)。
在onStartCommand()還執(zhí)行了AlarmManager的初始化以及時間的設(shè)定,因為AlarmManager中第三個參數(shù)PendingIntent能夠執(zhí)行一個廣播,所以還需要寫一個廣播接收者。
AlarmManager的取消:manager.cancel(PendingIntent pi);取消對應(yīng)PendingIntent即可。
AlarmReceiver:這就很簡單了,接收到廣播之后開啟再開啟服務(wù)即可。這就詳單與是一個死循環(huán),服務(wù)開啟后會定時發(fā)送廣播,廣播接收到之后又會開啟服務(wù)。
因為時間有限,所以代碼肯定有很多不完善之處,希望多多指教。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持VEVB武林網(wǎng)。
新聞熱點
疑難解答
圖片精選