- 论坛徽章:
- 0
|
Android 实现一个Service应用
紧接着前面的那篇博客继续加上代码实现一个service
利用eclipse制作界面新版的ADT支持直接拖动,很方便的设计自己的UI
首先定义一个Activity
Java代码- 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);
-
- }
- };
-
- }
- 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中的内容
Java代码- 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();
-
- }
-
- }
- 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
运行结果
|
|