android的Service的实例

发布时间:2020-07-20 21:09:23 作者:HHT15927087748
来源:网络 阅读:619

package com.android.service;

import android.app.IntentService;
import android.content.Intent;

public class HelloIntentService extends IntentService{

    public HelloIntentService() {
        super("HelloIntentService");
        // TODO Auto-generated constructor stub
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        // TODO Auto-generated method stub
        System.out.println("休息8秒");
        
        try{
            Thread.sleep(8000);
        }catch(InterruptedException e){
            e.printStackTrace();
        }
    }

    public void onDestory(){
        System.out.println("执行onHandleIntent之后会自动调用!");
        super.onDestroy();
        
    }
    
    
}


package com.android.service;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;

public class HelloService extends Service{

    @Override
    public IBinder onBind(Intent intent) {
        // TODO Auto-generated method stub
        return null;
    }
    
    public void onCreate(){
        super.onCreate();
        
        
        
    }

    public void onDestory(){
        
        super.onDestroy();
        
    }
    
    public void onStart(Intent intent,int startId) {
        // TODO Auto-generated method stub
        System.out.println("启动Service,休眠10秒");
        
        try{
            Thread.sleep(8000);
        }catch(InterruptedException e){
            e.printStackTrace();
        }
    }

    
    
}



package com.android.service;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button button=(Button)findViewById(R.id.button1);
        button.setOnClickListener(new OnClickListener() {
            
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Intent intent=new Intent(
                        MainActivity.this,HelloIntentService.class);
                startService(intent);
            }
        });
   

    Button button2=(Button)findViewById(R.id.button2);
    button.setOnClickListener(new OnClickListener() {
        
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Intent intent=new Intent(
                    MainActivity.this,HelloIntentService.class);
            stopService(intent);
        }
    });
    
    Button button3=(Button)findViewById(R.id.button3);
    button.setOnClickListener(new OnClickListener() {
        
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Intent intent=new Intent(
                    MainActivity.this,HelloService.class);
            startService(intent);
        }
    });
    
}
    

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
    
}


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    tools:context=".MainActivity" >

    
    <Button
        android:id="@+id/button1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="启动IntentService"
        
        
        
        
        />
    <Button
        android:id="@+id/button2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="停止IntentService"
        
        
        
        
        />
    <Button
        android:id="@+id/button3"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="启动Service"
        
        
        
        
        />
    
    
   
</LinearLayout>

推荐阅读:
  1. Android中service的组件是什么
  2. android的service

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

实例 service android

上一篇:云计算学习路线教程代码笔记: if嵌套功能语法解析

下一篇:过程的通讯:共享存储、音讯传递和管道通讯

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》