1、工程目录
2、MainActivity.java
package com.example.test_service;
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;
import android.app.Activity;
import android.app.Service;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
public class MainActivity extends Activity {
private Button startBtn, stopBtn, bindBtn, unbindBtn;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
startBtn = (Button) findViewById(R.id.button01);
stopBtn = (Button) findViewById(R.id.button02);
bindBtn = (Button) findViewById(R.id.button03);
unbindBtn = (Button) findViewById(R.id.button04);
startBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent intent = new Intent();
intent.setAction("com.example.test_service.action.MY_SERVICE");
startService(intent);
}
});
stopBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent intent = new Intent();
intent.setAction("com.example.test_service.action.MY_SERVICE");
stopService(intent);
}
});
final ServiceConnection conn = new ServiceConnection() {
@Override
public void onServiceDisconnected(ComponentName arg0) {
// TODO Auto-generated method stub
Log.i("SERVICE", "断开连接");
Toast.makeText(MainActivity.this, "断开连接!", Toast.LENGTH_LONG)
.show();
}
@Override
public void onServiceConnected(ComponentName arg0, IBinder arg1) {
// TODO Auto-generated method stub
Log.i("SERVICE", "连接成功");
Toast.makeText(MainActivity.this, "连接成功!", Toast.LENGTH_LONG)
.show();
}
};
bindBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent intent = new Intent();
intent.setAction("com.example.test_service.action.MY_SERVICE");
bindService(intent, conn, Service.BIND_AUTO_CREATE);
}
});
unbindBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent intent = new Intent();
intent.setAction("com.example.test_service.action.MY_SERVICE");
unbindService(conn);
}
});
}
}
3、MyService.java
package com.example.test_service;
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{
@Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
Log.i("SERVICE", "on create.................");
Toast.makeText(MyService.this,"on create .........." , Toast.LENGTH_LONG).show();
}
@Override
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
Log.i("SERVICE", "on destroy.................");
Toast.makeText(MyService.this,"on destroy .........." , Toast.LENGTH_LONG).show();
}
@Override
@Deprecated
public void onStart(Intent intent, int startId) {
// TODO Auto-generated method stub
super.onStart(intent, startId);
Log.i("SERVICE", "on start.................");
Toast.makeText(MyService.this,"on start .........." , Toast.LENGTH_LONG).show();
}
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
Log.i("SERVICE", "on bind.................");
Toast.makeText(MyService.this,"on bind .........." , Toast.LENGTH_LONG).show();
return null;
}
}
4、main.xml
<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/button01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="启动Service" />
<Button
android:id="@+id/button02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="停止Service" />
<Button
android:id="@+id/button03"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="绑定Service" />
<Button
android:id="@+id/button04"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="解绑Service" />
</LinearLayout>
5、Android Menifest.xml中添加Service的注册
<service android:name="MyService" >
<intent-filter>
<action android:name="com.example.test_service.action.MY_SERVICE" />
</intent-filter>
</service>
6、打印输出