一行代码一行代码地抄写,写的时候也顺便想了想到底这代码的功能是什么。
这代码是深圳图书馆借来的书上的,今天学习到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);
}
}