紧接着前面的那篇博客继续加上代码实现一个service
利用eclipse制作界面新版的ADT支持直接拖动,很方便的设计自己的UI
[img]http://dl.iteye.com/upload/attachment/599609/648c589b-7990-3d56-85b4-f5a946a53571.jpg[/img]
首先定义一个Activity
一下是service中的内容
在AndroidManifest中定义Service 和Activity
运行结果
[img]http://dl.iteye.com/upload/attachment/599611/69bd3732-7384-3d0f-b01c-4433b6f2b6c4.jpg[/img]
[img]http://dl.iteye.com/upload/attachment/599613/4ddcc85b-a478-3f78-9126-aaf3d6c752ff.jpg[/img]
利用eclipse制作界面新版的ADT支持直接拖动,很方便的设计自己的UI
[img]http://dl.iteye.com/upload/attachment/599609/648c589b-7990-3d56-85b4-f5a946a53571.jpg[/img]
首先定义一个Activity
package com.houyewei.myservice;
import android.app.Activity;
import android.app.Service;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
/*
* @author:Wynston Hou
* @email :
*/
public class MainActivity extends Activity {
private Button startBtn, stopBtn, bindBtn, unbindBtn;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 设置Activity的界面的布局
setContentView(R.layout.main);
// 通过findViewByid
startBtn = (Button) findViewById(R.id.startButton01);
stopBtn = (Button) findViewById(R.id.stopButton02);
bindBtn = (Button) findViewById(R.id.bindButton03);
unbindBtn = (Button) findViewById(R.id.unbindButton04);
// 添加监听
startBtn.setOnClickListener(startlistener);
stopBtn.setOnClickListener(stoplistener);
bindBtn.setOnClickListener(bindlistener);
unbindBtn.setOnClickListener(unbindlistener);
};
private OnClickListener startlistener = new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setAction("com.houyewei.myservice.action.MY_SERVICE");
startService(intent);
}
};
private OnClickListener stoplistener = new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setAction("com.houyewei.myservice.action.MY_SERVICE");
stopService(intent);
}
};
private ServiceConnection conn = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
Log.i("SERVICE", "连接成功");
Toast.makeText(MainActivity.this, "断开连接", Toast.LENGTH_LONG).show();
}
@Override
public void onServiceDisconnected(ComponentName name) {
// TODO Auto-generated method stub
}
};
private OnClickListener bindlistener = new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setAction("com.houyewei.myservice.action.MY_SERVICE");
bindService(intent, conn, Service.BIND_AUTO_CREATE);
}
};
private OnClickListener unbindlistener = new OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent();
intent.setAction("com.houyewei.myservice.action.MY_SERVICE");
unbindService(conn);
}
};
}
一下是service中的内容
package com.houyewei.myservice;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;
public class MyService extends Service {
public IBinder onBind(Intent intent) {
Log.i("SERVICE", "onBInd...................");
Toast.makeText(MyService.this, "onBInd.........", Toast.LENGTH_LONG)
.show();
return null;
}
public void onCreate() {
Log.i("SERVICE", "onCreate........");
Toast.makeText(MyService.this, "onCreatehaha ........",
Toast.LENGTH_LONG).show();
}
public void onStart(Intent intent, int startId) {
Log.i("SERVICE", "onStartoing..............");
Toast.makeText(MyService.this, "onStart....哈哈", Toast.LENGTH_LONG)
.show();
}
public void onDestory() {
Log.i("SERVICE", "Ondestory.........");
Toast.makeText(MyService.this, "OnDestory。。。。。hhaha", Toast.LENGTH_LONG)
.show();
}
}
在AndroidManifest中定义Service 和Activity
运行结果
[img]http://dl.iteye.com/upload/attachment/599611/69bd3732-7384-3d0f-b01c-4433b6f2b6c4.jpg[/img]
[img]http://dl.iteye.com/upload/attachment/599613/4ddcc85b-a478-3f78-9126-aaf3d6c752ff.jpg[/img]