3、Android服务开发全解析

Android服务开发详解

Android服务开发全解析

1. 服务终止与通知

在服务销毁时,我们需要停止对 LocationListener 对象的更新,这会停止所有的后台处理。同时,我们要通知用户服务正在终止。以下是相关代码:

Notification(android.R.drawable.stat_notify_more,
"GPS Tracking", System.currentTimeMillis());
notify.flags |= Notification.FLAG_AUTO_CANCEL;
Intent toLaunch = new Intent(getApplicationContext(),
ServiceControl.class);
PendingIntent intentBack =
PendingIntent.getActivity(getApplicationContext(),
0, toLaunch, 0);
notify.setLatestEventInfo(getApplicationContext(),
"GPS Tracking", "Tracking stopped", intentBack);
notifier.notify(GPS_NOTIFY, notify);
super.onDestroy();

无论 start 方法被调用多少次, onDestroy() 方法只会被调用一次。

服务必须在 AndroidManifest.xml 权限文件

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值