android后台通知,Android后台定时提醒功能实现
前提:考慮到自己每次在敲代碼或者打游戲的時候總是會不注意時間,一不留神就對著電腦連續3個小時以上,對眼睛的傷害還是挺大的,重度近視了可是會遺傳給將來的孩子的呀,可能老婆都跟別人跑了。
于是,為了保護眼睛,便做了個如下的應用:
打開后效果:
時間到之后有后臺提醒:
好了,接下來說一下做這樣一個APP主要涉及到的知識點:
Service:使用service,便可以在程序即使后臺運行的時候,也能夠做出相應的提醒,并且不影響手機進行其他工作。
AlarmManager:此知識點主要是用來計時,具體的在代碼的注釋中寫的很詳細。
notification:此知識點就是用作通知的顯示了,具體的可以參考另一篇文章:
MainActivity:
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Window;
import android.widget.Toast;
public class MainActivity extends Activity {
private Intent intent;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//取消標題欄
requestWindowFeature(Window.FEATURE_NO_TITLE);
//由于主要是用于測試,并且便于新手理解,所以activity_main布局寫的很簡單
setContentView(R.layout.activity_main);
intent = new Intent(this, LongRunningService.class);
//開啟關閉Service
startService(intent);
//設置一個Toast來提醒使用者提醒的功能已經開始
Toast.makeText(MainActivity.this,"提醒的功能已經開啟,關閉界面則會取消提醒。",Toast.LENGTH_LONG).show();
}
@Override
protected void onDestroy() {
super.onDestroy();
//在Activity被關閉后,關閉Service
stopService(intent);
}
}
LongRunningService:
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.os.SystemClock;
public class LongRunningService extends Service {
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
AlarmManager manager = (AlarmManager) getSystemService(ALARM_SERVICE);
//讀者可以修改此處的Minutes從而改變提醒間隔時間
//此處是設置每隔90分鐘啟動一次
//這是90分鐘的毫秒數
int Minutes = 90*60*1000;
//SystemClock.elapsedRealtime()表示1970年1月1日0點至今所經歷的時間
long triggerAtTime = SystemClock.elapsedRealtime() + Minutes;
//此處設置開啟AlarmReceiver這個Service
Intent i = new Intent(this, AlarmReceiver.class);
PendingIntent pi = PendingIntent.getBroadcast(this, 0, i, 0);
//ELAPSED_REALTIME_WAKEUP表示讓定時任務的出發時間從系統開機算起,并且會喚醒CPU。
manager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, triggerAtTime, pi);
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onDestroy() {
super.onDestroy();
//在Service結束后關閉AlarmManager
AlarmManager manager = (AlarmManager) getSystemService(ALARM_SERVICE);
Intent i = new Intent(this, AlarmReceiver.class);
PendingIntent pi = PendingIntent.getBroadcast(this, 0, i, 0);
manager.cancel(pi);
}
}
AlarmReceiver:
import android.app.Notification;
import android.app.NotificationManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class AlarmReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
//設置通知內容并在onReceive()這個函數執行時開啟
NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification=new Notification(R.drawable.ic_launcher,"用電腦時間過長了!白癡!"
,System.currentTimeMillis());
notification.setLatestEventInfo(context, "快去休息!!!",
"一定保護眼睛,不然遺傳給孩子,老婆跟別人跑啊。", null);
notification.defaults = Notification.DEFAULT_ALL;
manager.notify(1, notification);
//再次開啟LongRunningService這個服務,從而可以
Intent i = new Intent(context, LongRunningService.class);
context.startService(i);
}
}
activity_main:
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="15dp"
android:orientation="vertical"
>
android:layout_marginBottom="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="護眼定時提醒"
android:textSize="30sp"
android:gravity="center_horizontal"
/>
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="提醒間隔時間:"
android:textSize="25sp"
/>
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="90分鐘"
android:textSize="25sp"
/>
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="提醒音樂:"
android:textSize="25sp"
/>
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="系統默認音樂"
android:textSize="25sp"
/>
千萬不要忘了在AndroidManifest中注冊Service!
AndroidManifest:
package="com.example.servicebestpractice"
android:versionCode="1"
android:versionName="1.0" >
android:minSdkVersion="14"
android:targetSdkVersion="17" />
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
android:name="com.example.servicebestpractice.MainActivity"
android:label="@string/app_name" >
此處有個不得不提的注意點,筆者原來的代碼是在Activity開啟的時候自動開啟Service,在Activity摧毀的時候自動摧毀Service,看上去好像可以運行,沒有什么錯誤,并且在10分鐘內的提醒基本都能夠正常運行。
但是倘若在比較長的時間提醒的時候就會出現不提醒的問題了!為什么呢?
因為android為了優化內存,減少耗電,是會自動清理內存的,會把后臺的Service給清理掉。
以上就是本文的全部內容,希望對大家的學習有所幫助。
總結
以上是生活随笔為你收集整理的android后台通知,Android后台定时提醒功能实现的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 计算机玩游戏不能全屏,如果电脑游戏无法全
- 下一篇: android百度地图附近的poi,An