主程序
package com.example.service_test;
package com.example.service_test;
import java.io.OutputStream;
import java.io.Reader;
import java.io.Writer;
import java.sql.Clob;
import java.sql.NClob;
import java.sql.SQLException;
import java.io.Reader;
import java.io.Writer;
import java.sql.Clob;
import java.sql.NClob;
import java.sql.SQLException;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity {
private Button start,stop;
//private Intent intent;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
start = (Button)findViewById(R.id.btn_start);
stop = (Button)findViewById(R.id.btn_stop);
final Intent intent = new Intent(this,FirstService.class);
start.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
startService(intent);
}
});
stop.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
stopService(intent);
finish();
}
});
}
// private void btn_start() {
// // TODO Auto-generated method stub
// startService(intent);
// }
//
// private void btn_stop() {
// // TODO Auto-generated method stub
// stopService(intent);
// }
}
Service:
xml:
xmlns:tools=""
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
>
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
android:id="@+id/btn_start"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="btn_start"
android:text="start"
/>
android:id="@+id/btn_stop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="btn_stop"
android:text="stop"
/>
AndroidManifest.xml
在
加入:
private Button start,stop;
//private Intent intent;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
start = (Button)findViewById(R.id.btn_start);
stop = (Button)findViewById(R.id.btn_stop);
final Intent intent = new Intent(this,FirstService.class);
start.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
startService(intent);
}
});
stop.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
stopService(intent);
finish();
}
});
}
// private void btn_start() {
// // TODO Auto-generated method stub
// startService(intent);
// }
//
// private void btn_stop() {
// // TODO Auto-generated method stub
// stopService(intent);
// }
}
Service:
package com.example.service_test;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.widget.Toast;
import android.content.Intent;
import android.os.IBinder;
import android.widget.Toast;
public class FirstService extends Service{
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
@Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
System.out.println("service is created!");
//Toast.makeText(FirstService, "service is created!", 1000).show();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// TODO Auto-generated method stub
System.out.println("service is started!");
//Toast.makeText(this, "service is started!", 1000).show();
return START_STICKY;
}
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
@Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
System.out.println("service is created!");
//Toast.makeText(FirstService, "service is created!", 1000).show();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// TODO Auto-generated method stub
System.out.println("service is started!");
//Toast.makeText(this, "service is started!", 1000).show();
return START_STICKY;
}
@Override
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
System.out.println("service is Destroy!");
//Toast.makeText(this, "service is Destroy!", 1000).show();
}
}
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
System.out.println("service is Destroy!");
//Toast.makeText(this, "service is Destroy!", 1000).show();
}
}
xml:
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
>
android:layout_height="wrap_content"
android:text="@string/hello_world" />
android:id="@+id/btn_start"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="btn_start"
android:text="start"
/>
android:id="@+id/btn_stop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="btn_stop"
android:text="stop"
/>
AndroidManifest.xml
在
加入: