android 服务实例,android的Service的实例

android的Service的实例

发布时间:2020-07-20 21:09:23

来源:51CTO

阅读:559

作者:HHT15927087748

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;

}

}

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:orientation="vertical"

tools:context=".MainActivity" >

android:id="@+id/button1"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="启动IntentService"

/>

android:id="@+id/button2"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="停止IntentService"

/>

android:id="@+id/button3"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="启动Service"

/>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值