Android Service服务

一行代码一行代码地抄写,写的时候也顺便想了想到底这代码的功能是什么。

这代码是深圳图书馆借来的书上的,今天学习到Android Service服务模块,

记下这些代码,看虽然似简单,但能通过这些代码学习到Android Service程序的步骤甚至原理。

 

main.xml 文件内容如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/hello"
    />
<Button android:text="开始服务" android:id="@+id/Button01" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
<Button android:text="停止服务" android:id="@+id/Button02" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
</LinearLayout>


 

 

AndroidManifest.xml 文件内容如下:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.demo.myService"
      android:versionCode="1"
      android:versionName="1.0">
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".myService"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
		<service android:enabled="true" android:name = ".excampleService"/>
    </application>
</manifest> 

 

myService.java文件内容如下:

package com.demo.myService;

import com.demo.myService.excampleService;

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 myService extends Activity {
    /** Called when the activity is first created. */
	Button btStart = null;
	Button btStop = null;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        //绑定元素
        btStart = (Button)findViewById(R.id.Button01);
        btStop = (Button)findViewById(R.id.Button02);
        
        //新建Service意图
        final Intent serviceIntent = new Intent(this,excampleService.class);
        
        //添加监听接口以及消息处理函数
        btStart.setOnClickListener(new OnClickListener()
        {
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				//单击开始服务按钮后启动服务
				startService(serviceIntent);
			}
        	
        });
        
        //添加监听接口以及消息处理函数
        btStop.setOnClickListener(new OnClickListener()
        {
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				//单击开始服务按钮后启动服务
				stopService(serviceIntent);
			}
        	
        });
    }
}


excampleService.java 文件内容如下:

package com.demo.myService;

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

public class excampleService extends Service {

	@Override
	public IBinder onBind(Intent arg0) {
		// TODO Auto-generated method stub
		return null;
	}
	
	//当服务第一次创建时调用该方法
	public void onCreate(){
		Log.d("MyService","onCreate");
		super.onCreate();
	}
	
	//当服务销毁时调用该方法
	public void onDestroy(){
		Log.d("MyService","onDestroy");
		super.onDestroy();
	}
	
	//当服务开始时调用该方法
	public void onStart(Intent intent,int startId){
		Log.d("MyService","onStart");
		super.onStart(intent,startId);
	}

}



 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值