当前位置:首页 > 编程开发

Android后台程序应用技巧分享

webgou17年前 (2010-08-08)编程开发188

Android手机操作系统是由谷歌推出的一款开源的基于Linux平台的操作系统,深受广大编程爱好者的喜爱。在Android系统中我们一直在接触着前台界面程序,其实在一开始接触Android时就听说了,程序就有有界面和无界面之分。

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

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

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

启动服务:

  1. // Implicitly start a Service  
  2. startService(new Intent(MyService.MY_ACTION));  
  3. // Explicitly start a Service  
  4. startService(new Intent(this, MyService.class)); 

停止服务:

  1. stopService(new Intent(this, MyService.class)); 

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

  1. @Override  
  2. public void onCreate()   
  3. public IBinder onBind(Intent intent)  
  4. public void onStart(Intent intent, int startId)  
  5. 。。。 

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

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

1. Toast

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


实现代码是:

  1. Context context = getApplicationContext();  
  2. String msg = “To the bride an groom!”;  
  3. int duration = Toast.LENGTH_SHORT;  
  4. Toast toast = Toast.makeText(context, msg, duration);  
  5. int offsetX = 0;  
  6. int offsetY = 0;  
  7. toast.setGravity(Gravity.BOTTOM, offsetX, offsetY);  
  8. toast.show(); 

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

2. Notifications

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

实现代码:

  1. String svcName = Context.NOTIFICATION_SERVICE;  
  2. NotificationManager notificationManager;  
  3. notificationManager = (NotificationManager)getSystemService(svcName);  
  4. // Choose a drawable to display as the status bar icon  
  5. int icon = R.drawable.icon;  
  6. // Text to display in the status bar when the notification is launched  
  7. String tickerText = “Notification”;  
  8. // The extended status bar orders notification in time order  
  9. long when = System.currentTimeMillis();  
  10. Notification notification = new Notification(icon, tickerText, when);  
  11. Context context = getApplicationContext();  
  12. // Text to display in the extended status window  
  13. String expandedText = “Extended status text”;  
  14. // Title for the expanded status  
  15. String expandedTitle = “Notification Title”;  
  16. // Intent to launch an activity when the extended text is clicked  
  17. Intent intent = new Intent(this, MyActivity.class);  
  18. PendingIntent launchIntent = PendingIntent.getActivity(context, 0, intent, 0);  
  19. notification.setLatestEventInfo(context, expandedTitle,expandedText,launchIntent); 

触发方式:

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

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

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

  1. // This method is called on the main GUI thread.  
  2. private void mainProcessing() {  
  3. // 主程序中启动线程.  
  4. Thread thread = new Thread(null, doBackgroundThreadProcessing, 
    “Background”);  
  5. thread.start();  
  6. }  
  7. // Runnable that executes the background processing method.  
  8. private Runnable doBackgroundThreadProcessing = new Runnable() {  
  9. public void run() {  
  10. //线程执行内容。。。  
  11. }  
  12. };

扫描二维码推送至手机访问。

版权声明:本文由知了博客发布,如需转载请注明出处。

本文链接:https://www.webgou.info/?id=236

标签: androidSDK
分享给朋友:

“Android后台程序应用技巧分享” 的相关文章

sandy 摄像机

SandyCam Help.The ViewFinderThis is the camera view of the Sandy world.The ViewFinder has a crosshair, that can be turned on or off, using the check b…

定时器

来源:window程序设计 Microsoft Windows定时器是一种输入设备,它周期性地在每经过一个指定的时间间隔后就通知应用程序一次。您的程序将时间间隔告诉Windows,例如「每10秒钟通知我一声」,然后Windows给您的程序发送周期性发生的WM_TIMER消息以表示时间到了…

Android 使用极光/友盟推送,APP进程在杀死后收不到推送的处理方法(转)

 为什么会存在这样的 问题,刚开始的时候我也搞不清楚,之前用极光的时候杀死程序后也会收到推送,但最近重新再去集成时就完全不好使了,这我就纳闷了,虽然Google在高版本上的Android上面不建议线程守护,所以将进程有关的Service都杀掉了,但旧版本还是有这个功能的啊 !真尼玛,后来猜…

跟踪wince遇到的问题

今天,测试报告的几个问题,非常的诡异,把 问题记录下来. 1.wince直接运行explorer.exe,模拟导航,出现黑屏 2.运行我们的应用程序,模拟导航,出现内存只剩下2M. 3.出现读取注册表出错,通过在数字电视下,打大灯,旋音量很容易. 第一个问题,硬件工程师,用电压表量电压正常,…

DirectShow编程(3.2)- 关于DirectShow - Filter Graph和它的组件

3.2. Filter Graph和它的组件    这一节描述了DirectShow的主要组件,为DirectShow应用程序和DirectShow Filter开发者提供一个介绍。应用程序开发者可以忽略掉很多底层部分,但是,了解底层对于理解DirectShow架构还是…

char,short ,int ,long,long long,unsigned long long数据范围

 速查表:char -128 ~ +127 (1 Byte)...…

发表评论

访客

看不清,换一张

◎欢迎参与讨论,请在这里发表您的看法和观点。