浅谈AndroidService服务的高级技巧-创新互联
1 前台服务

因为服务的优先级较低,所以当系统内存不足时,可能会回收正在后台运行的服务。如果若要避免服务被回收,可以使用前台服务。
前台服务会一直有一个图标在系统的状态栏中显示,下拉状态栏可以看到更加详细的信息,类似于消息通知效果。
public class FirstService extends Service {
private static final String TAG = "FirstService";
@Override
public void onCreate() {
super.onCreate();
Log.d(TAG, "onCreate");
//设置为前台服务
Intent intent = new Intent(this, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
Notification notification = new NotificationCompat.Builder(this)
.setContentTitle("梅西生涯大尴尬 战法国能否破荒?")
.setContentText("世界杯1/8决赛,法国对阵阿根廷,法国队主帅德尚将迎来80战里程碑,成为队史执教场次最多的主教练,高卢雄鸡能否保持过去40年世界杯遇南美球队不败的金身,格里兹曼能否找回最佳状态,梅西能否打破此前世界杯淘汰赛666分钟的进球荒,都是此役的关键看点。")
.setWhen(System.currentTimeMillis())
.setSmallIcon(R.mipmap.ic_launcher)
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher))
.setContentIntent(pendingIntent)
.build();
startForeground(1,notification);
}
}
分享标题:浅谈AndroidService服务的高级技巧-创新互联
当前地址:http://www.jxjierui.cn/article/dohcpc.html


咨询
建站咨询
