Android 建立單獨的服務運行在後台(無介面)

來源:互聯網
上載者:User

標籤:入口   sim   保留   應用   --   start   log   介面   cat   

轉自:56481934

 

今天項目有個需求是,開啟一個服務單獨運行在後台,而且還不能有介面,在度娘搜尋了一圈也沒發現可以完美解決的方法,然後自己嘗試解決的方法,開始的思路是,把介面幹掉,也就是activity,然後將開啟Service的操作放在Application中,結果運行程式,在控制台報錯了。

 

因為我把AndroidManifest.xml中的主Activity的配置給幹掉了,而程式找不到應用的入口,所有就無法開啟應用,這種方法行不通。

 

然後我就想,把Activity保留,但是我不給它 setContentView(......);也就是不給他設定布局檔案,

 

 

[java] view plain copy 
  1. public class MainActivity extends Activity {  
  2.   
  3.     @Override  
  4.     protected void onCreate(Bundle savedInstanceState) {  
  5.         super.onCreate(savedInstanceState);  
  6.           
  7.         System.out.println("MainActivity  OnCreate()....");  
  8.           
  9.         System.out.println("準備開啟服務");  
  10.         Intent intent = new Intent(MainActivity.this,TestService.class);    
  11.         startService(intent);    
  12.     }  
  13. }  

 


運行程式,程式開啟了,服務也運行了,但是有個問題就是,介面也出來了,為什麼呢?

 

 

原因是在AndroidManifest.xml中Application節點中這個這行代碼android:theme="@style/AppTheme",既然是主題的問題導致介面的出現,那麼是想android是否提供了不顯示介面的主題?尋找後問題終於解決了,解決方案:在資訊清單檔中,主activity的配置中添加這行代碼

 

android:theme="@android:style/Theme.NoDisplay"

代碼:

 

[java] view plain copy 
  1. <application  
  2.        android:allowBackup="true"  
  3.        android:icon="@drawable/ic_launcher"  
  4.        android:label="@string/app_name"  
  5.        android:theme="@style/AppTheme" >  
  6.        <activity  
  7.            android:name=".MainActivity"  
  8.            android:label="@string/app_name"  
  9.            android:theme="@android:style/Theme.NoDisplay"  
  10.            >  
  11.            <intent-filter>  
  12.                <action android:name="android.intent.action.MAIN" />  
  13.   
  14.                <category android:name="android.intent.category.LAUNCHER" />  
  15.            </intent-filter>  
  16.        </activity>  
  17.   
  18.        <service android:name="com.example.backgroundservice.TestService" >  
  19.        </service>  
  20.    </application>  


我們還可以Ctrl+左鍵點進去看看這個主題是怎麼寫的:

 

[java] view plain copy 
  1. <!-- Default theme for activities that don‘t actually display a UI; that  
  2.         is, they finish themselves before being resumed.  -->  
  3.    <style name="Theme.NoDisplay">  
  4.        <item name="android:windowBackground">@null</item>  
  5.        <item name="android:windowContentOverlay">@null</item>  
  6.        <item name="android:windowIsTranslucent">true</item>  
  7.        <item name="android:windowAnimationStyle">@null</item>  
  8.        <item name="android:windowDisablePreview">true</item>  
  9.        <item name="android:windowNoDisplay">true</item>  
  10.    </style>  


運行程式,服務開啟了,介面也不顯示,完美解決了後台啟動服務的進程。

 

Android 建立單獨的服務運行在後台(無介面)

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.