码迷,mamicode.com
首页 > 移动开发 > 详细

Android Service

时间:2014-11-28 22:45:10      阅读:416      评论:0      收藏:0      [点我收藏+]

标签:des   android   style   blog   http   io   ar   os   使用   

      Android Service 类似于Windows 的 Windows Service,由于Windows 服务的作用大家都很熟悉了,这里就不介绍了。今天写了一个简单的例子,介绍Android Service 的使用方式。Android 界面有两个按钮,一个是启动服务,一个是停止服务。

      ServiceActivity

package com.example.androidexample;

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

public class ServiceActivity extends Activity {

	private Button btnStartService;
	
	private Button btnStopService;
	
	private Intent intent;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		super.setContentView(R.layout.activity_service);
		
		intent = new Intent();
		
		intent.setAction("My_Action");
		
		btnStartService = (Button)super.findViewById(R.id.btnStartService);
		
		btnStopService = (Button)super.findViewById(R.id.btnStopService);
		
		btnStartService.setOnClickListener(startServiceListener);
		
		btnStopService.setOnClickListener(stopServiceListener);
	}
	
	OnClickListener startServiceListener = new OnClickListener(){

		@Override
		public void onClick(View arg0) {
			// TODO Auto-generated method stub
			startService(intent);
		}
	};
	
	OnClickListener stopServiceListener = new OnClickListener(){

		@Override
		public void onClick(View arg0) {
			// TODO Auto-generated method stub
			stopService(intent);
		}
	};
}

FirstService

package com.example.business;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.widget.Toast;
import com.example.androidexample.*;

public class FirstService extends Service {

	@Override
	public void onCreate() {
		// TODO Auto-generated method stub
		super.onCreate();
		System.out.println("Service is created.");
		//Toast.makeText(new ServiceActivity(), "Service is created", Toast.LENGTH_LONG).show();
	}

	@Override
	public void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		System.out.println("Service is destoryed.");
		
	}

	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {
		System.out.println("Service is started.");
		// TODO Auto-generated method stub
		return super.onStartCommand(intent, flags, startId);
	}

	@Override
	public IBinder onBind(Intent arg0) {
		// TODO Auto-generated method stub
		return null;
	}

}

 布局文件 activity_service.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

     <Button 
            android:id="@+id/btnStartService"
          	android:layout_width="match_parent"
          	android:layout_height="wrap_content"
          	android:text="@string/StartService"/>
    <Button 
            android:id="@+id/btnStopService"
          	android:layout_width="match_parent"
          	android:layout_height="wrap_content"
          	android:text="@string/StopService"/>

</LinearLayout>

 运行效果

bubuko.com,布布扣

bubuko.com,布布扣

Android Service

标签:des   android   style   blog   http   io   ar   os   使用   

原文地址:http://www.cnblogs.com/fuhui-jurassic/p/4129490.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!