标签:flags out extends width com 继承 post service button点击
在布局文件加入两个button
<Button
android:id="@+id/start_service"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="32dp"
android:onClick="dostartService"
android:text="启动simpleService" />
<Button
android:id="@+id/stop_service"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@id/start_service"
android:layout_marginTop="51dp"
android:onClick="dostopService"
android:text="停止simpleService" />
第二步创建SimpleService类继承Servicepackage com.example.simpleservice;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;
public class SimpleService extends Service{
private String tag ="simpleService";
@Override
public void onCreate() {
Log.d(tag, "simpleserviece ->onCreate()");
super.onCreate();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.d(tag, "simpleserviece ->onStartCommand");
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onDestroy() {
Log.d(tag, "simpleserviece ->onDestroy()");
super.onDestroy();
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
}第四步在MainActivity中增加button点击方法:
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void dostartService(View v){
Intent intent = new Intent(this, SimpleService.class);
startService(intent);
}
public void dostopService(View v){
Intent intent = new Intent(this, SimpleService.class);
stopService(intent);
}
}
标签:flags out extends width com 继承 post service button点击
原文地址:http://www.cnblogs.com/gavanwanggw/p/7190560.html