不给 Activity setContentView(......);也就是不给他设置布局文件,
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
System.out.println("MainActivity OnCreate()....");
System.out.println("准备开启服务");
Intent intent = new Intent(MainActivity.this,TestService.class);
startService(intent);
}
}
android:theme="@android:style/Theme.NoDisplay"
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:theme="@android:style/Theme.NoDisplay"
>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name="com.example.backgroundservice.TestService" >
</service>
</application>
可以Ctrl+左键点进去看看这个主题是怎么写的:
<!-- Default theme for activities that don't actually display a UI; that
is, they finish themselves before being resumed. -->
<style name="Theme.NoDisplay">
<item name="android:windowBackground">@null</item>
<item name="android:windowContentOverlay">@null</item>
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowAnimationStyle">@null</item>
<item name="android:windowDisablePreview">true</item>
<item name="android:windowNoDisplay">true</item>
</style>