public class AActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Intent intent=new Intent(this,AService.class);
startService(intent);
}
}public class AService extends Service { @Override
public void onCreate() {
super.onCreate();
System.out.println("create");
} @Override
public IBinder onBind(Intent intent) { return null;
} @Override
public void onDestroy() {
super.onDestroy();
System.out.println("destroy");
} @Override
public void onStart(Intent intent, int startId) {
super.onStart(intent, startId);
System.out.println("start");
}}
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="yjc.a" android:versionCode="1" android:versionName="1.0">
<uses-sdk android:minSdkVersion="10" /> <application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".AActivity" 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=".AService" android:enabled="true" />
</application>
</manifest>
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Intent intent=new Intent(this,AService.class);
startService(intent);
}
}public class AService extends Service { @Override
public void onCreate() {
super.onCreate();
System.out.println("create");
} @Override
public IBinder onBind(Intent intent) { return null;
} @Override
public void onDestroy() {
super.onDestroy();
System.out.println("destroy");
} @Override
public void onStart(Intent intent, int startId) {
super.onStart(intent, startId);
System.out.println("start");
}}
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="yjc.a" android:versionCode="1" android:versionName="1.0">
<uses-sdk android:minSdkVersion="10" /> <application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".AActivity" 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=".AService" android:enabled="true" />
</application>
</manifest>
如果正确的话,服务应该是启动过了,如果再次启动应该是启动onCommonStart这个方法