2011-10-07 146 views
74

從我在Stack Exchange和其他地方看到的所有內容中,當Android OS啓動時,我已經正確設置了一切以啓動IntentService。不幸的是,它不是在啓動時啓動,我沒有得到任何錯誤。也許專家可以幫助...Android - 在啓動時啓動服務

清單:

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.phx.batterylogger" 
    android:versionCode="1" 
    android:versionName="1.0" 
    android:installLocation="internalOnly"> 

<uses-sdk android:minSdkVersion="8" /> 
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /> 
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 
<uses-permission android:name="android.permission.BATTERY_STATS" /> 

<application android:icon="@drawable/icon" android:label="@string/app_name"> 
    <service android:name=".BatteryLogger"/> 
    <receiver android:name=".StartupIntentReceiver"> 
     <intent-filter> 
      <action android:name="android.intent.action.BOOT_COMPLETED" /> 
     </intent-filter> 
    </receiver> 
</application> 

</manifest> 

廣播接收器的啓動:

package com.phx.batterylogger; 

import android.content.BroadcastReceiver; 
import android.content.Context; 
import android.content.Intent; 

public class StartupIntentReceiver extends BroadcastReceiver { 
    @Override 
    public void onReceive(Context context, Intent intent) { 
     Intent serviceIntent = new Intent(context, BatteryLogger.class); 
     context.startService(serviceIntent); 
    } 
} 

更新:我試過幾乎所有的下面的建議,和我說記錄如Log.v("BatteryLogger", "Got to onReceive, about to start service");到StartupIntentReceiver的onReceive處理程序,並且沒有任何記錄。所以它甚至沒有將它傳送給BroadcastReceiver。

我想我正在部署APK並正確測試,只需在Eclipse中運行調試,並且控制檯說它已成功將其安裝到位於\ BatteryLogger \ bin \ BatteryLogger.apk的我的Xoom平板電腦上。然後進行測試,我重新啓動平板電腦,然後查看DDMS中的日誌,並在操作系統設置中檢查運行服務。這一切聽起來是正確的,還是我錯過了什麼?再次,任何幫助非常感謝。

+1

你得到什麼問題,你沒有得到任何UI ..? –

+0

該服務從未開始,這就是問題所在。 – Gady

+0

你怎麼知道你的服務沒有開始,你打印日誌或類似的東西..? –

回答

230

那麼這裏是一個自動啓動應用程序

AndroidManifest文件

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="pack.saltriver" android:versionCode="1" android:versionName="1.0" 
    android:permission="android.permission.RECEIVE_BOOT_COMPLETED"> 

    <application android:icon="@drawable/icon" android:label="@string/app_name"> 

     <receiver android:name=".autostart"> 
      <intent-filter> 
       <action android:name="android.intent.action.BOOT_COMPLETED" /> 
      </intent-filter> 
     </receiver> 

     <activity android:name=".hello"></activity> 
     <service android:enabled="true" android:name=".service" /> 
    </application> 
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"></uses-permission> 
</manifest> 

autostart.java

public class autostart extends BroadcastReceiver 
{ 
    public void onReceive(Context context, Intent arg1) 
    { 
     Intent intent = new Intent(context,service.class); 
     context.startService(intent); 
     Log.i("Autostart", "started"); 
    } 
} 

service.java

的一個完整的例子
public class service extends Service 
{ 
    private static final String TAG = "MyService"; 
    @Override 
    public IBinder onBind(Intent intent) { 
     return null; 
    } 
    public void onDestroy() { 
     Toast.makeText(this, "My Service Stopped", Toast.LENGTH_LONG).show(); 
     Log.d(TAG, "onDestroy"); 
    } 

    @Override 
    public void onStart(Intent intent, int startid) 
    { 
     Intent intents = new Intent(getBaseContext(),hello.class); 
     intents.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
     startActivity(intents); 
     Toast.makeText(this, "My Service Started", Toast.LENGTH_LONG).show(); 
     Log.d(TAG, "onStart"); 
    } 
} 

hello.java - 每執行一次應用程序後啓動設備,就會彈出。

public class hello extends Activity 
{ 
    public void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     Toast.makeText(getBaseContext(), "Hello........", Toast.LENGTH_LONG).show(); 
    } 
} 
+6

+1的完整例子。也許我應該改變我的IntentService與onHandleIntent到一個普通的舊服務 – Gady

+4

這最終解決了它。問題在於我的服務是IntentService而不是普通的舊服務和服務代碼中的錯誤的組合。這個答案的完整性和登錄Android幫助我解決了我的問題。 – Gady

+2

+1爲詳細的例子。如果沒有活動,這也會起作用嗎? – balas

1

看起來非常相似,mine,但我用的是全包名的接收器:

<receiver android:name=".StartupIntentReceiver"> 

我:

<receiver android:name="com.your.package.AutoStart"> 
+0

沒有修復它。不過謝謝。 – Gady

+0

是的,雖然有點排序你的問題,所以更好地發佈了一個你好世界的例子。 –

1

我已經沒有完整的包成功,你知道呼叫鏈正在中斷?如果您使用Log()進行調試,那麼它在什麼時候不再起作用?

我認爲它可能在您的IntentService中,這一切都看起來不錯。

+0

我添加了'Log.v(「BatteryLogger」,「有onReceive,即將開始服務」);'onReceive處理程序,它從不出現在日誌中。因此,聽衆失敗(?) – Gady

+0

當手機啓動時,廣播接收機是什麼啓動的?謝謝! –

+0

男孩。幾年後我還沒有碰到過Android。對不起@Ruchir – Phix

3

以下應該工作。我已驗證。可能是你的問題在別的地方。

public class MyReceiver extends BroadcastReceiver{ 

    @Override 
    public void onReceive(Context context, Intent intent) { 
     Log.d("TAG", "MyReceiver"); 
     Intent serviceIntent = new Intent(context, Test1Service.class); 
     context.startService(serviceIntent); 
    } 
} 




public class Test1Service extends Service { 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate() { 
     super.onCreate(); 
     Log.d("TAG", "Service created."); 
    } 

    @Override 
    public int onStartCommand(Intent intent, int flags, int startId) { 
     Log.d("TAG", "Service started."); 
     return super.onStartCommand(intent, flags, startId); 
    } 

    @Override 
    public void onStart(Intent intent, int startId) { 
     super.onStart(intent, startId); 
     Log.d("TAG", "Service started."); 
    } 
    @Override 
    public IBinder onBind(Intent arg0) { 
     return null; 
    } 
} 




<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
     package="com.test" 
     android:versionCode="1" 
     android:versionName="1.0" 
     android:installLocation="internalOnly"> 
    <uses-sdk android:minSdkVersion="8" /> 

    <application android:icon="@drawable/icon" android:label="@string/app_name"> 

    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /> 
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 
    <uses-permission android:name="android.permission.BATTERY_STATS" 
    /> 
<!--  <activity android:name=".MyActivity"> 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 
       <category android:name="android.intent.category.LAUNCHER"></category> 
      </intent-filter> 
     </activity> --> 
     <service android:name=".Test1Service" 
        android:label="@string/app_name" 
        > 
     </service> 
     <receiver android:name=".MyReceiver"> 
      <intent-filter> 
       <action android:name="android.intent.action.BOOT_COMPLETED" /> 
      </intent-filter> 
     </receiver> 
    </application> 
</manifest> 
+0

謝謝你,但它仍然無法正常工作。日誌甚至不顯示它使它進入StartupIntentReceiver。任何其他想法? – Gady

+0

請參閱我的問題的更新。 – Gady

+1

你的代碼在模擬器中工作嗎?你能夠在模擬器中接收意圖嗎? – Vivek

2

服務可能會被越來越關閉它完成,由於設備會啓動後睡覺前。您需要先獲得喚醒鎖。幸運的是,Support library gives us a class要做到這一點:

public class SimpleWakefulReceiver extends WakefulBroadcastReceiver { 
    @Override 
    public void onReceive(Context context, Intent intent) { 
     // This is the Intent to deliver to our service. 
     Intent service = new Intent(context, SimpleWakefulService.class); 

     // Start the service, keeping the device awake while it is launching. 
     Log.i("SimpleWakefulReceiver", "Starting service @ " + SystemClock.elapsedRealtime()); 
     startWakefulService(context, service); 
    } 
} 

然後,在你的服務,確保釋放喚醒鎖:

@Override 
    protected void onHandleIntent(Intent intent) { 
     // At this point SimpleWakefulReceiver is still holding a wake lock 
     // for us. We can do whatever we need to here and then tell it that 
     // it can release the wakelock. 

... 
     Log.i("SimpleWakefulReceiver", "Completed service @ " + SystemClock.elapsedRealtime()); 
     SimpleWakefulReceiver.completeWakefulIntent(intent); 
    } 

不要忘記添加WAKE_LOCK權限:

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /> 
<uses-permission android:name="android.permission.WAKE_LOCK" /> 
+0

我有同樣的問題。你介意幫助我嗎?謝謝! http://stackoverflow.com/questions/35373525/starting-my-service –