日拱一卒(九)

1.前台服务与普通服务的区别?如何创建前台服务?

前台服务:不会由于系统内存不足的原因导致被回收

普通服务:会由于系统内存不足的原因导致被回收

创建前台服务:

public class MyService extends Service {
	……
	@Override
	public void onCreate() {
		super.onCreate();
		Notification notification = new Notification(R.drawable.ic_launcher,
"Notification comes", System. currentTimeMillis());
		Intent notificationIntent = new Intent(this, MainActivity.class);
		PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
		notification.setLatestEventInfo(this, "This is title", "This is content", pendingIntent);
		startForeground(1, notification);
		Log.d("MyService", "onCreate executed");
	}
	……
}


1.Service,stopService,bindService,而unBindService只能调用一次,否则抛异常。

2.多次startService其onCreate只在第一次调用,onStartCommand,onStart每次都调用;多次bindService,onBind只执行一次。

3.如有bindService,在Activity销毁之前必须调用onUnbindService,否则抛异常。

4.service是否销毁的条件:startService必须stopService,bindService必须unBindService;开启绑定和解绑停止的顺序不一定一致。即:

开始先start后bind,结束先stop后unbind与先unbind后stop效果相同。

2.IntentService与普通Service的区别:

Service :Service的代码都是默认运行在主线程,在此开启用于处理耗时操作的线程,需要手动停止服务

IntentService:onHandleIntent()运行在子线程中,且子线程会在服务销毁的时候停掉。

3.最佳实践之Android定时任务设计:

Java Timer 和Android 的Alarm都可以实现。Timer有一个明显的短板,它并不太适用于那些需要长期在后台运行的定时任务。每种手机都会有自己的休眠策略,Android手机会在长时间不操作的情况下自动让CPU进入到睡眠状态,这就有可能导致Timer中的定时任务无法正常运行。而Alarm机制则不存在这种情况,它具有唤醒CPU的功能,即可以保证每次需要执行定时任务的时候CPU都能正常工作。需要注意,这里唤醒CPU和唤醒屏幕完全不是同一个概念,千万不要产生混淆。

ELAPSED_REALTIME:让定时任务的触发时间从系统开机开始算起,但不会唤醒CPU

ELAPSED_REALTIME_WAKEUP:同样表示让定时任务的触发时间从系统开机开始算起,但会唤醒CPU

RTC:让定时任务的触发时间从1970110点开始算起,但不会唤醒CPU

RTC_WAKEUP:让定时任务的触发时间从1970110点开始算起,但会唤醒CPU

SystemClock.elapsedRealtime():获取到系统开机至今所经历时间的毫秒数

System.currentTimeMillis():获取到1970110点至今所经历时间的毫秒数。

public class LongRunningService extends Service {

	@Override
	public IBinder onBind(Intent intent) {
		return null;
	}

	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {
		new Thread(new Runnable() {
			@Override
			public void run() {
				Log.d("LongRunningService", "executed at " + new Date(). toString());
			}
		}).start();
		AlarmManager manager = (AlarmManager) getSystemService(ALARM_SERVICE);
		int anHour = 60 * 60 * 1000;  // 这是一小时的毫秒数
		long triggerAtTime = SystemClock.elapsedRealtime() + anHour;
		Intent i = new Intent(this, AlarmReceiver.class);
		PendingIntent pi = PendingIntent.getBroadcast(this, 0, i, 0);
		manager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, triggerAtTime, pi);
		return super.onStartCommand(intent, flags, startId);
	}

}
public class AlarmReceiver extends BroadcastReceiver {

	@Override
	public void onReceive(Context context, Intent intent) {
		Intent i = new Intent(context, LongRunningService.class);
		context.startService(i);
	}

}


注意:alarm从Android 4.4版本变得不准AlarmManagersetExact()方法来替代set()方法,就可以保证任务准时执行了




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值