关于Android Service组件在多线程应用的理解

    Android Service组件在Google Android SDK官网上的定义是这样的:
    A Service is an application component representing either an application's desire to perform a longer-running operation while not interacting with the user or to supply functionality for other applications to use. Each service class must have a corresponding declaration in its package's AndroidManifest.xml. Services can be started with Context.startService() and Context.bindService().
    Note that services, like other application objects, run in the main thread of their hosting process. This means that, if your service is going to do any CPU intensive (such as MP3 playback) or blocking (such as networking) operations, it should spawn its own thread in which to do that work. More information on this can be found in Processes and Threads. The IntentService class is available as a standard implementation of Service that has its own thread where it schedules its work to be done.
    在中文中,他的表述是服务。Android赋予了Service比处于不活动状态的活动更高的优先级,这样,在系统请求资源的时候,他们被终止的可能行更小。事实上,如果运行时过早的终止一个已启动的服务,那么只要有足够的资源可用,则运行时就会重新启动它。在极端情况中,服务的终止将会显着的影响用户体验,从而导致软件设计上的UI与UE的互动缺失,在这些情况中,可以把服务的优先级提升到与前台的活动相同的位置。通过使用服务,可以保证应用程序持续的运行,并对事件作出响应,即使他们被主动地使用也是此。
    Service运行时没有专门的GUI,但是,与活动以及广播接收器一样,他们仍然应该在应用程序进程的主进程执行。下面将为大家简要介绍一下Android中使用Java进线程调用的使用方法。
    1.创建一个线程
    [java] view plaincopy
    /**
    * @Copyright by Alfred Z.Zheng, Cindigo. 2011-09. Wuhan.
    * An innovative club in Huazhong Univ. of Sci. & Tech.
    * Prj. name: org.cindigo.javatestthread1
    */
    class Mythread implements Runnable{
    //Mythread实现了Runnable
    int count;
    String thrdName;
    Mythread(String name){
    count = 0;
    thrdName = name;
    }
    public void run(){
    //线程运行起点
    System.out.println(thrdName + "start a thread.");
    try{
    do{
    Thread.sleep(1000);
    System.out.println("@At " + thrdName + ", result is " + count);
    count++;
    } while(count < 10);
    }
    catch(InterruptException exc){
    System.out.println(thrdName + " interrupted.");
    }
    System.out.println(thrdName + " timing up!");
    }
    }
    class UseThreads{
    public static void main(String args[]){
    System.out.println("Starting a main thread...");
    //创建一个可运行的对象
    MyThread myth = new MyThread("Activity No.1");
    //在该对象上构造一个线程
    Thread newth = new Thread(myth);
    newth.start();
    do{
    System.out.println("Activity No.2");
    try{
    Thread.sleep(500);
    }catch(InterruptException exc){
    System.out.println("Activity No.3");
    }
    }while(myth.count != 10);
    System.out.println("Activity No.4");
    }
    }
    2.创建多个线程
    [java] view plaincopy
    /**
    * @Copyright by Alfred Z.Zheng, Cindigo. 2011-09. Wuhan.
    * An innovative club in Huazhong Univ. of Sci. & Tech.
    * Prj. name: org.cindigo.javatestthread2
    */
    //创建多重线程
    class MyThread implements Runnable{
    int count;
    Thread thrd;
    //建立新线程
    MyThread(String name){
    thrd = new Thread(this, name);
    count = 0;
    thrd.start(); //开启线程
    }
    //开启新的主线程
    public void run(){
    System.out.println(thrd.getName() + " starting.");
    try{
    do{
    Thread.sleep(500);
    System.out.println("In " + thrd.getName() + ", count is " + count);
    count++;
    }while(count < 10);
    }
    catch(InterruptException exc){
    System.out.println(thrd.getName() + " interrupted.");
    }
    System.out.println(thrd.getName() + " timing up!");
    }
    }
    class UseThreadsImpoved{
    public static void main(String arg[]{
    System.out.println("Main thread starting.");
    //现在线程在创建时启动
    Mythread myth = new MyThread("My Activity No.1");
    do{
    System.out.println(".");
    try{
    Thread.sleep(1000);
    }
    catch(InterruptException exc){
    System.out.println("My Activity No.2");
    }
    }while(myth.count != 10);
    System.out.println("My Activity No.2");
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值