1、
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="s.s.t"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".MainActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name=".Broacast" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" /><!-- 系统启动 -->
<action android:name="android.intent.action.SIG_STR" /><!-- 电话的信号强度已经改变 -->
<action android:name="android.intent.action.CONFIGURATION_CHANGED" /><!-- 设备的配置信息已经改变 -->
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
<action android:name="android.net.wifi.WIFI_STATE_CHANGED" />
<action android:name="android.net.conn.BACKGROUND_DATA_SETTING_CHANGED" />
</intent-filter>
</receiver>
<service android:name=".ServiceTest"></service>
</application>
<uses-sdk android:minSdkVersion="8" />
</manifest>
2、
package s.s.t;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button btnStart = (Button)this.findViewById(R.id.buttonstart);
Button btnEnd = (Button)this.findViewById(R.id.buttonstop);
btnStart.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(MainActivity.this,ServiceTest.class);
startService(intent);
}
});
btnEnd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(MainActivity.this,ServiceTest.class);
stopService(intent);
}
});
}
}
3、
package s.s.t;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class Broacast extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
Intent intente = new Intent(context,ServiceTest.class);
context.startService(intente);
}
}
4、
package s.s.t;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;
public class ServiceTest extends Service {
private int tag = 1;
@Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
Log.e("ServiceTest", "onCreate");
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// TODO Auto-generated method stub
Log.e("ServiceTest", tag+"");
tag = tag+1;
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onDestroy() {
// TODO Auto-generated method stub
Log.e("ServiceTest", "onDestroy");
super.onDestroy();
}
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
}