有问题...
act:
package com.example.service3;
import android.os.Bundle;
import android.os.IBinder;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
public class Service3Activity extends Activity {
private Intent intent = new Intent();
private Service3.mBinder binder;
private ServiceConnection sConnection = new ServiceConnection() {
public void onServiceDisconnected(ComponentName name) {
// TODO Auto-generated method stub
System.out.println("--ServiceDisconnected--");
}
public void onServiceConnected(ComponentName name, IBinder service) {
// TODO Auto-generated method stub
System.out.println("--ServiceConnected--");
binder = (Service3.mBinder)service;
}
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_service3);
intent.setAction("android.service");
Button button1 = (Button)findViewById(R.id.button1);
button1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
bindService(intent, sConnection, BIND_AUTO_CREATE);
}
});
Button button2 = (Button)findViewById(R.id.button2);
button2.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
unbindService(sConnection);
}
});
Button button3 = (Button)findViewById(R.id.button3);
button3.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(Service3Activity.this,
"Service鐨刢ounter" +
binder.getCounter(),
Toast.LENGTH_LONG).show();
}
});
}
}
service:
package com.example.service3;
import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
public class Service3 extends Service {
private int counter = 0;
private boolean bRunning = true;
private mBinder binder = new mBinder();
public class mBinder extends Binder{
public int getCounter(){
return counter;
}
}
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return binder;
}
@Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
new Thread(new Runnable() {
public void run() {
// TODO Auto-generated method stub
while (!bRunning) {
try {
Thread.sleep(1000);
} catch (Exception e) {}
counter++;
}
}
}) .start();
// Log.v("counter", "鍊间负锛? + binder.getCounter());
}
@Override
public boolean onUnbind(Intent intent) {
// TODO Auto-generated method stub
return super.onUnbind(intent);
}
@Override
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
bRunning = false;
}
}
androidxml:
</activity>
<service android:name="Service3">
<intent-filter>
<action android:name="android.service"/>
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</service>
</application>
layout:
<RelativeLayout 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" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="34dp"
android:text="启动Service" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/button1"
android:layout_below="@+id/button1"
android:text="停止Service" />
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/button2"
android:layout_centerHorizontal="true"
android:layout_marginTop="17dp"
android:text="获取数据" />
</RelativeLayout>