2011-09-01 118 views
0

有誰知道爲什麼我的服務無法啓動時,我啓動我的Android手機?我正在嘗試將我的服務鏈接到我的活動課程,當活動崩潰時,服務將再次重新啓動活動。我的服務類是如下爲什麼我的服務在啓動期間不啓動?

public class Restarter extends Service { 

@Override 
public IBinder onBind(Intent intent) { 
    return null; 
} 


@Override 
public void onCreate() { 
    super.onCreate(); 
    Log.v("Restarter", "Restarter Created"); 
} 

@Override 
    public int onStartCommand(Intent intent, int flags, int startId) 
    { 
    Stamp_Client sc = new Stamp_Client(); 
     System.out.println("Restarter Service Started"); 
     while(true){ 
      System.out.println("Checking for crash..."); 
     int instance; 
     instance = sc.nbInstances(); 
     if(instance == 0){ 
      Intent myIntent = 
      new Intent(Restarter.this,Stamp_Client.class); 
      Restarter.this.startActivity(myIntent); 
     } 
     try { 
      Thread.sleep(10000); 

     } catch (InterruptedException e) { 

      e.printStackTrace(); 
     } 

     // We want this service to continue running until it is explicitly 
     // stopped, so return sticky. 
     return START_STICKY; 
    } 

    } } 

我的清單是如下

<?xml version="1.0" encoding="utf-8"?> 
    <manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.i2r.cas.mofa.main" 
    android:versionCode="1" 
    android:versionName="1.0"> 
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"> 
    </uses-permission> 
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission> 
<uses-permission android:name="android.permission.UPDATE_DEVICE_STATS"> 
</uses-permission> 
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"></uses-permission> 
<uses-permission android:name="android.permission.WAKE_LOCK"></uses-permission> 
<uses-permission android:name="android.permission.READ_LOGS" /> 
<uses-permission android:name="android.permission.INTERNET"></uses-permission> 
<uses-permission android:name="android.permission.READ_PHONE_STATE"> 
</uses-permission> 
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"> 
</uses-permission> 

    <application android:icon="@drawable/icon" android:label="@string/app_name"> 
    <activity android:name=".Stamp_Client" 
       android:label="@string/app_name" 
       android:windowSoftInputMode="adjustResize"   
       android:configChanges="orientation|keyboardHidden"> 


    <service android:enabled="true" android:name=".Restarter" > 
<intent-filter> 
    <action android:name="com.i2r.cas.mofa.main.Restarter"> 
    </action> 
</intent-filter> 
    </service>  


    <receiver android:name=".StartAtBootServiceReceiver"> 
<intent-filter> 
    <action android:name="android.intent.action.BOOT_COMPLETED"> 
    </action> 
    <category android:name="android.intent.category.HOME"> 
    </category> 
</intent-filter> 
    </receiver> 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 
      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 
    </application> 

    </manifest> 

有誰知道爲什麼沒有得到啓動期間叫什麼名字?謝謝!

回答

0

在您的清單文件中,您以錯誤的方式聲明元素。

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
package="com.i2r.cas.mofa.main" 
android:versionCode="1" 
android:versionName="1.0"> 
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"> 
</uses-permission> 
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission> 
<uses-permission android:name="android.permission.UPDATE_DEVICE_STATS"> 
</uses-permission> 
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"></uses-permission> 
<uses-permission android:name="android.permission.WAKE_LOCK"></uses-permission> 
<uses-permission android:name="android.permission.READ_LOGS" /> 
<uses-permission android:name="android.permission.INTERNET"></uses-permission> 
<uses-permission android:name="android.permission.READ_PHONE_STATE"> 
</uses-permission> 
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"> 
</uses-permission> 
<application android:icon="@drawable/icon" android:label="@string/app_name"> 
<activity android:name=".Stamp_Client" 
     android:label="@string/app_name" 
     android:windowSoftInputMode="adjustResize"   
     android:configChanges="orientation|keyboardHidden"> 
    <intent-filter> 
     <action android:name="android.intent.action.MAIN" /> 
     <category android:name="android.intent.category.LAUNCHER" /> 
    </intent-filter> 
</activity> 

<service android:enabled="true" android:name=".Restarter" > 
    <intent-filter> 
    <action android:name="com.i2r.cas.mofa.main.Restarter"> 
     </action> 
    </intent-filter> 
</service>  
<receiver android:name=".StartAtBootServiceReceiver"> 
    <intent-filter> 
      <action android:name="android.intent.action.BOOT_COMPLETED"> 
       </action> 
      <category android:name="android.intent.category.HOME"> 
      </category> 
     </intent-filter> 
</receiver> 
</application> 
</manifest> 

在你StartBootServiceReceiver調用startService(服務類的意圖)方法

相關問題