文/大大大大峰哥
概述
現(xiàn)在我們使用服務(wù)的時(shí)候予借,有時(shí)候會遇到Android內(nèi)存不足的情況,這個時(shí)候很有可能Service就被進(jìn)行了內(nèi)存回收了昌跌,如果我們希望我們的Service一直存在仰禀,就需要運(yùn)用到前臺服務(wù)的技術(shù)。使用該方法的APP比如:假裝情侶避矢,墨跡天氣等等悼瘾。
前臺服務(wù)與普通服務(wù)最大的區(qū)別就是前臺服務(wù)需要有一個通知信息欄也就是Notification。
代碼
package com.example.demo5;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.Service;
import android.content.Intent;
import android.os.Build;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.support.annotation.RequiresApi;
/**
* Created by TangZhiFeng on 2016/12/26.
*/
public class ProService extends Service {
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
@Override
public void onCreate() {
NotificationManager notificationManager= (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Notification notification=new Notification.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("Title")
.setContentText("ProService in Run")
.build();
notificationManager.notify(1,notification);
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
}