免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1446 | 回复: 0
打印 上一主题 下一主题

Jollen 的 Android 教學,#8: 沒有 UI 的 Service [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-07-03 16:12 |只看该作者 |倒序浏览

到目前為止,我們都著重在 Activity 以及 UI 的介紹,在 Android 應用程式裡,有一種沒有 UI
的類別(android.app.Service),稱之為 Service。簡單來說,Service 是一個 background
process(背景程序),透過背景程序,我們可以實作一些不需要 UI 的功能,例如:在背景撥放音樂。
以下是利用 Eclipse 環境自動產生的類別 'MokoService':
               
               
                import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
public class MokoService extends Service {
    @Override
    public IBinder onBind(Intent intent) {
        // TODO Auto-generated method stub
        return null;
    }
}
MokoService 類別繼承自 android.app.Service,幾個有關 Service 的重要觀念如下:
1. Service 物件以 separated process 的方式執行,這表示 Service 與 UI(Activity)並不在同一個 process 裡執行,而是個自在不同的 process 執行。
2. Android 應用程式是在 Activity 裡啟動與停止 Service。
3. 覆載(override)onStart() 方法(method)在 Service 被啟動時,執行我們想要的背景功能。
4. 覆載 onDestroy() 方法在 Service 被停止時,停止執行中的背景功能。
以下是一個加入 onStart 與 onDestroy 的  MokoService 實作:
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
public class MokoService extends Service {
    @Override
    public IBinder onBind(Intent intent) {
        // TODO Auto-generated method stub
        return null;
    }
   
    @Override
    public void onStart(Intent intent, int startId) {
        
    }
   
    @Override
    public void onDestroy() {
        
    }
}


本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u/14644/showart_1985063.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP