人每天都要喝8杯水才能保持健康,于是苦逼的程序員總是一遍代碼就忘了時(shí)間,于是我突發(fā)奇想能不能開發(fā)一個(gè)apk能夠?qū)崿F(xiàn)固定的間隔時(shí)間定時(shí)提醒我要喝水了呢?
創(chuàng)新互聯(lián)自2013年起,先為江州等服務(wù)建站,江州等地企業(yè),進(jìn)行企業(yè)商務(wù)咨詢服務(wù)。為江州企業(yè)網(wǎng)站制作PC+手機(jī)+微官網(wǎng)三網(wǎng)同步一站式服務(wù)解決您的所有建站問題。
apk基本功能:
1)能夠設(shè)置間隔時(shí)間 2)在apk應(yīng)用被停止的情況下仍然能定時(shí)提醒 3)能夠播放指定鬧鈴 4)能夠及時(shí)終止提醒
效果圖:
設(shè)置間隔
時(shí)間到后會(huì)跳出全局AlertDialog提示并且開始播放鬧鈴
即使APP被終止了,仍然能夠提示
結(jié)束提示
廢話不多說,直接上代碼:
布局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="請(qǐng)?jiān)O(shè)置提示時(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代碼:
/* *因?yàn)橐?wù)常駐后臺(tái),就不需要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; //記錄時(shí)間間隔 @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將時(shí)間間隔傳遞給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; //記錄間隔時(shí)間 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)用被停止的情況下仍然能定時(shí)提醒,這里采用startService即可實(shí)現(xiàn),使service常駐內(nèi)存,即使Activity被殺死,依舊可以執(zhí)行。
2)間隔時(shí)間提醒。這里采用的是Android的Alarm機(jī)制。
Android中的定時(shí)任務(wù)一般有兩種實(shí)現(xiàn)方式,一種是使用Java API里提供的Timer類,一種是使用Android的Alarm機(jī)制。這兩種情況在多數(shù)情況下都能實(shí)現(xiàn)類似的效果,但Timer類有一個(gè)明顯的短板,它并不太適用于那些需要長(zhǎng)期在后臺(tái)運(yùn)行的定時(shí)任務(wù)。我們都知道,為了能讓電池更耐用,每種手機(jī)都會(huì)有自己的休眠策略,Android手機(jī)就會(huì)在長(zhǎng)時(shí)間不操作的情況下自動(dòng)讓CPU進(jìn)入到睡眠狀態(tài),這就有可能導(dǎo)致Timer中的定時(shí)任務(wù)無法正常運(yùn)行。而Alarm機(jī)制則不存在這種情況,它具有喚醒CPU的功能,即可以保證每次需要執(zhí)行定時(shí)任務(wù)的時(shí)候CPU都能正常工作。需要注意,這里喚醒CPU和喚醒屏幕完全不是一個(gè)概念。
從Service代碼中可以看出,onCreate()中完成對(duì)mediaPlayer的初始化(因?yàn)閙ediaPlayer只需要初始化一次),在onStartCommand()中開啟一個(gè)新的線程,線程中通過handler發(fā)送一條空的消息,并且在handler的handleMessage()方法中完成AlertDialog的創(chuàng)建以及播放鬧鈴,要注意這里創(chuàng)建的是一個(gè)全局的AlertDialog。因?yàn)榈谝淮伍_啟任務(wù)的時(shí)候不需要新建一個(gè)AlertDialog(用戶第一次開啟任務(wù)的時(shí)候是設(shè)置好時(shí)間并且點(diǎn)擊了“開啟”,這個(gè)時(shí)候不需要?jiǎng)?chuàng)建Dialog)。
在onStartCommand()還執(zhí)行了AlarmManager的初始化以及時(shí)間的設(shè)定,因?yàn)锳larmManager中第三個(gè)參數(shù)PendingIntent能夠執(zhí)行一個(gè)廣播,所以還需要寫一個(gè)廣播接收者。
AlarmManager的取消:manager.cancel(PendingIntent pi);取消對(duì)應(yīng)PendingIntent即可。
AlarmReceiver:這就很簡(jiǎn)單了,接收到廣播之后開啟再開啟服務(wù)即可。這就詳單與是一個(gè)死循環(huán),服務(wù)開啟后會(huì)定時(shí)發(fā)送廣播,廣播接收到之后又會(huì)開啟服務(wù)。
因?yàn)闀r(shí)間有限,所以代碼肯定有很多不完善之處,希望多多指教。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。
網(wǎng)站欄目:androidservice實(shí)現(xiàn)循環(huán)定時(shí)提醒功能
當(dāng)前URL:http://chinadenli.net/article12/jgijdc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站營(yíng)銷、網(wǎng)站收錄、響應(yīng)式網(wǎng)站、定制開發(fā)、商城網(wǎng)站、網(wǎng)站設(shè)計(jì)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)