在 LOG 中显示进程 package my.android.AppServer; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.Button; public class AppServer extends Activity implements android.view.View.OnClickListener { private Button mButton1, mButton2; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); mButton1 = (Button) findViewById(R.id.button1_id); mButton1.setOnClickListener(this); mButton2 = (Button) findViewById(R.id.button2_id); mButton2.setOnClickListener(this); } public void onClick(View v) { // TODO Auto-generated method stub switch (v.getId()) { case R.id.button1_id: Intent i = new Intent(this, mService.class); i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); startService(i); break; case R.id.button2_id: Intent i2 = new Intent(this, mService.class); stopService(i2); break; default: break; } } } package my.android.AppServer; import android.app.Service; import android.content.Intent; import android.os.Handler; import android.os.IBinder; import android.util.Log; public class mService extends Service { private Handler objHandler = new Handler(); private int intCounter = 0; private Runnable mTasks = new Runnable() { public void run() { intCounter++; Log.i("hippo", "counter:" + Integer.toString(intCounter)); objHandler.postDelayed(mTasks, 1000); } }; @Override public void onStart(Intent intent, int startId) { objHandler.postDelayed(mTasks, 1000); super.onStart(intent, startId); } @Override public void onCreate() { super.onCreate(); } @Override public IBinder onBind(Intent intent) { // TODO Auto-generated method stub return null; } @Override public void onDestroy() { objHandler.removeCallbacks(mTasks); super.onDestroy(); } } <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /> <Button android:id="@+id/button1_id" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/button1_label" /> <Button android:id="@+id/button2_id" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/button2_label" /> </LinearLayout> <?xml version="1.0" encoding="utf-8"?> <resources> <string name="hello">Hello World, AppServer!</string> <string name="app_name">AppServer</string> <string name="button1_label">start</string> <string name="button2_label">stop</string> </resources> <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="my.android.AppServer" android:versionCode="1" android:versionName="1.0"> <application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name=".AppServer" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <service android:name=".mService" android:exported="true" android:process=":remote" > </service> </application> <uses-sdk android:minSdkVersion="7" /> </manifest>