1.Service的第一个例子
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" >
<Button
android:id="@+id/bindbutton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="绑定Service" />
<Button
android:id="@+id/unbindbutton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="解除绑定" />
<Button
android:id="@+id/getcount"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="获取数据" />
</LinearLayout>
<service android:name=".BindService">
<intent-filter >
<action android:name="com.example.android_service01.BINDSERVICE"/>
</intent-filter>
</service>
package com.example.android_service01;
import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
public class BindService extends Service {
private int count;
private boolean quit;
private MyBinder myBinder = new MyBinder();
public class MyBinder extends Binder {
public int getCount() {
return count;
}
}
public BindService() {
// TODO Auto-generated constructor stub
}
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
System.out.println("--Service is binded!");
return myBinder;
}
public void onCreate() {
super.onCreate();
System.out.println("--Service is created");
new Thread() {
public void run() {
while (!quit) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
count++;
}
}
}.start();
}
@Override
public boolean onUnbind(Intent intent) {
// TODO Auto-generated method stub
System.out.println("--Service is unBinded!");
return super.onUnbind(intent);
}
@Override
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
System.out.println("--Service is onDestroy!");
}
}
package com.example.android_service01;
import com.example.android_service01.BindService.MyBinder;
import android.os.Bundle;
import android.os.IBinder;
import android.app.Activity;
import android.app.Service;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends Activity {
private Button button1;
private Button button2;
private Button button3;
private BindService.MyBinder myBinder;
private ServiceConnection conn=new ServiceConnection() {
@Override
public void onServiceDisconnected(ComponentName name) {
// TODO Auto-generated method stub
System.out.println("--onServiceDisconnected");
}
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
// TODO Auto-generated method stub
System.out.println("--onServiceConnected");
myBinder=(MyBinder) service;
}
};
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button1 = (Button) findViewById(R.id.bindbutton);
button2 = (Button) findViewById(R.id.unbindbutton);
button3 = (Button) findViewById(R.id.getcount);
final Intent intent=new Intent();
intent.setAction("com.example.android_service01.BINDSERVICE");
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
bindService(intent, conn, Service.BIND_AUTO_CREATE);
}
});
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
unbindService(conn);
}
});
button3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(MainActivity.this, "获取count值:"+myBinder.getCount(), Toast.LENGTH_SHORT).show();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}