@@ -52,7 +52,7 @@ Service是Android中实现程序后台运行的解决方案,它非常适用于
52
52
当前组件调用unbindService\(\) ,想要解除与service的绑定时系统调用此方法(** 一次调用** ,一旦解除绑定后,下次再调用unbindService\(\) 会抛出异常)。
53
53
54
54
** OnDestory\(\) **
55
- 系统在service不再被使用并要销毁时调用此方法(** 一次调用** ). service应在此方法中释放资源,比如线程,已注册的侦听器,接收器等等.这是service收到的最后一个调用。
55
+ 系统在service不再被使用并要销毁时调用此方法(** 一次调用** )。 service应在此方法中释放资源,比如线程,已注册的侦听器,接收器等等.这是service收到的最后一个调用。
56
56
57
57
下面介绍三种不同情况下Service的生命周期情况。
58
58
@@ -249,7 +249,8 @@ private ServiceConnection con = new ServiceConnection() {
249
249
250
250
由于后台服务优先级相对比较低,当系统出现内存不足的情况下,它就有可能会被回收掉,所以前台服务就是来弥补这个缺点的,它可以一直保持运行状态而不被系统回收。
251
251
252
- ** 创建服务类**
252
+ ** 创建服务类**
253
+
253
254
前台服务创建很简单,其实就在Service的基础上创建一个Notification,然后使用Service的startForeground\(\) 方法即可启动为前台服务。
254
255
255
256
``` java
@@ -306,14 +307,13 @@ startService(new Intent(this, ForeService.class));
306
307
首先是用一般的PendingIntent来进行跳转
307
308
308
309
``` java
309
- mBuilder = new NotificationCompat .Builder (this ). setContent(view)
310
- .setSmallIcon(R . drawable. icon). setTicker(" 新资讯" )
311
- .setWhen(System . currentTimeMillis())
312
- .setOngoing(false )
313
- .setAutoCancel(true );
314
- Intent intent = new Intent (this , NotificationShow . class);
315
- PendingIntent pendingIntent = PendingIntent . getActivity(this , 0 ,
316
- intent, PendingIntent . FLAG_UPDATE_CURRENT );
310
+ mBuilder = new NotificationCompat .Builder (this ). setContent(view)
311
+ .setSmallIcon(R . drawable. icon). setTicker(" 新资讯" )
312
+ .setWhen(System . currentTimeMillis())
313
+ .setOngoing(false )
314
+ .setAutoCancel(true );
315
+ Intent intent = new Intent (this , NotificationShow . class);
316
+ PendingIntent pendingIntent = PendingIntent . getActivity(this , 0 , intent, PendingIntent . FLAG_UPDATE_CURRENT );
317
317
mBuilder. setContentIntent(pendingIntent);
318
318
```
319
319
@@ -322,20 +322,19 @@ mBuilder.setContentIntent(pendingIntent);
322
322
在运行效果上来看,首先发送了一条Notification到通知栏上,然后这时,退出程序,即MainActivity已经不存在了,回到home主菜单,看到Notification仍然存在,当然,我们还没有点击或者cancel它,现在去点击Notification,跳转到NotificationShow界面,然后我们按下Back键,发现直接回到home菜单了。现在大多数android应用都是在通知栏中如果有Notification通知的话,点击它,然后会直接跳转到对应的应用程序的某个界面,这时如果回退,即按下Back键,会返回到该应用程序的主界面,而不是系统的home菜单。所以用上面这种PendingIntent的做法达不到目的。这里我们使用TaskStackBuilder来做。
323
323
324
324
``` java
325
- mBuilder = new NotificationCompat .Builder (this ). setContent(view)
326
- .setSmallIcon(R . drawable. icon). setTicker(" 新资讯" )
327
- .setWhen(System . currentTimeMillis())
328
- .setOngoing(false )
329
- .setAutoCancel(true );
330
- Intent intent = new Intent (this , NotificationShow . class);
331
- TaskStackBuilder stackBuilder = TaskStackBuilder . create(this );
332
- stackBuilder. addParentStack(NotificationShow . class);
333
- stackBuilder. addNextIntent(intent);
334
- PendingIntent pendingIntent = stackBuilder. getPendingIntent(0 ,
335
- PendingIntent . FLAG_UPDATE_CURRENT );
336
- // PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,
337
- // intent, PendingIntent.FLAG_UPDATE_CURRENT);
338
- mBuilder. setContentIntent(pendingIntent);
325
+ mBuilder = new NotificationCompat .Builder (this )
326
+ .setContent(view)
327
+ .setSmallIcon(R . drawable. icon). setTicker(" 新资讯" )
328
+ .setWhen(System . currentTimeMillis())
329
+ .setOngoing(false )
330
+ .setAutoCancel(true );
331
+ Intent intent = new Intent (this , NotificationShow . class);
332
+ TaskStackBuilder stackBuilder = TaskStackBuilder . create(this );
333
+ stackBuilder. addParentStack(NotificationShow . class);
334
+ stackBuilder. addNextIntent(intent);
335
+ PendingIntent pendingIntent = stackBuilder. getPendingIntent(0 , PendingIntent . FLAG_UPDATE_CURRENT );
336
+ // PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
337
+ mBuilder. setContentIntent(pendingIntent);
339
338
```
340
339
341
340
显示用TaskStackBuilder.create\( this\) 创建一个stackBuilder实例,接下来addParentStack\(\) ;
@@ -349,7 +348,7 @@ mBuilder = new NotificationCompat.Builder(this).setContent(view)
349
348
那么我们就在manifest文件中添加这个属性
350
349
351
350
``` java
352
- < activity
351
+ < activity
353
352
android: name= " com.lvr.service.NotificationShow"
354
353
android: parentActivityName= " .MainActivity" >
355
354
< / activity>
0 commit comments