Android后台程序应用技巧分享

本文介绍了Android后台程序的概念及其启动和停止的方法。通过示例代码展示了如何使用Intent启动服务,并讨论了服务的生命期回调方法。此外,还介绍了两种常用的通知方式:Toast和Notifications。

Android后台程序就是这类无界面的程序,它在后台执行,没有影响你的界面。比如短信监听程序,执行在后台,当有短信时才给你们提示,振动或声音;比如闹钟,设定好时间后,在定时通知你;再比如mp3播放器,选择好音乐后,在待在后台唱着,当有电话来时,自动暂停,完后再继续播放。

其实分析下来,我们不难发现,Android后台程序跟前台程序是一样的,也就是在执行我们指定的程序,只是留给我们两个问题,1.因为没有界面,我们会问,怎么启动,怎么终止?2.因为没有界面,这程序如何通知我们一些信息或状态。

前面的学习让我们知道,一个Activity想Call另一个Activity时,只需要能过中介人Intent就可以了,同样我们与服务处理类打交道也是通过Intent来实现,当然,界面类是继承着Activity,而服务类则是继承着Service类。

启动服务:

  1. //ImplicitlystartaService
  2. startService(newIntent(MyService.MY_ACTION));
  3. //ExplicitlystartaService
  4. startService(newIntent(this,MyService.class));

停止服务:

  1. stopService(newIntent(this,MyService.class));

同样,跟Activity一样的生命期中,系统也会自动跟据不同的状态来调用继承函数:

  1. @Override
  2. publicvoidonCreate()
  3. publicIBinderonBind(Intentintent)
  4. publicvoidonStart(Intentintent,intstartId)
  5. 。。。

在实际的开发中,我们一般都不会直接写一个服务类,一般都会写一个与Android后台程序相配套的前台程序,一般的程序总会有一些配置吧~~,然后这个界面中就可以很方便地来控制后台程序的运作。

我们来回答第二个问题,就是在服务中我们怎么发起一个通知给用户,在Andorid中,提供了以下几种方式:

1. Toast

这是一个无模式的小窗体,会将显示的信息显示在首页面中:


实现代码是:

  1. Contextcontext=getApplicationContext();
  2. Stringmsg=“Tothebrideangroom!”;
  3. intduration=Toast.LENGTH_SHORT;
  4. Toasttoast=Toast.makeText(context,msg,duration);
  5. intoffsetX=0;
  6. intoffsetY=0;
  7. toast.setGravity(Gravity.BOTTOM,offsetX,offsetY);
  8. toast.show();

当然,你也可以显示更杂的,可以将一个控制直接当成一个Toast显示出来,也可以自定义一个控件显示出来,自定义控件的强大是大家都知道的~~

2. Notifications

这种方式是系统中比较通用的模式,通过这种方式你可以使系统:将一个图标在状态条上闪,让机器震动,发出声音等。

实现代码:

  1. StringsvcName=Context.NOTIFICATION_SERVICE;
  2. NotificationManagernotificationManager;
  3. notificationManager=(NotificationManager)getSystemService(svcName);
  4. //Chooseadrawabletodisplayasthestatusbaricon
  5. inticon=R.drawable.icon;
  6. //Texttodisplayinthestatusbarwhenthenotificationislaunched
  7. StringtickerText=“Notification”;
  8. //Theextendedstatusbarordersnotificationintimeorder
  9. longwhen=System.currentTimeMillis();
  10. Notificationnotification=newNotification(icon,tickerText,when);
  11. Contextcontext=getApplicationContext();
  12. //Texttodisplayintheextendedstatuswindow
  13. StringexpandedText=“Extendedstatustext”;
  14. //Titlefortheexpandedstatus
  15. StringexpandedTitle=“NotificationTitle”;
  16. //Intenttolaunchanactivitywhentheextendedtextisclicked
  17. Intentintent=newIntent(this,MyActivity.class);
  18. PendingIntentlaunchIntent=PendingIntent.getActivity(context,0,intent,0);
  19. notification.setLatestEventInfo(context,expandedTitle,expandedText,launchIntent);

触发方式:

  1. intnotificationRef=1;
  2. notificationManager.notify(notificationRef,notification);

学会了Activity再写个Android后台程序也就不难了!!

这里顺便再提一下,在Android系统中也提供了多线程编程,我们知道不管是前台还是后台程序,都有生命期的,当程序不活动时,我们想继续让程序执行,这里我们需要用到线程了,在Android系统中使用线程,跟我们直接写java线程程序非常想似:

  1. //ThismethodiscalledonthemainGUIthread.
  2. privatevoidmainProcessing(){
  3. //主程序中启动线程.
  4. Threadthread=newThread(null,doBackgroundThreadProcessing,
    “Background”);
  5. thread.start();
  6. }
  7. //Runnablethatexecutesthebackgroundprocessingmethod.
  8. privateRunnabledoBackgroundThreadProcessing=newRunnable(){
  9. publicvoidrun(){
  10. //线程执行内容。。。
  11. }
  12. };
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值