2012-10-26 58 views
2

我目前正在一個android項目上,我試圖啓動一個服務,當服務已經開始運行一些代碼來初始化一些東西。服務正在啓動,但OnCreate或OnStart沒有被調用

以下是我用於該服務的代碼。

Context context; 
    PowerManager.WakeLock wakeLock; 

    public PowerDetectionService(Context context) 
    { 
     this.context = context; 
    } 

    public PowerDetectionService() 
    {} 

    public void onCreate() 
    { 
     super.onCreate(); 
     PowerManager pm = (PowerManager)getApplication().getApplicationContext().getSystemService(Context.POWER_SERVICE); 
     wakeLock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK, "ScreenStay"); 
    } 

    public void onStart(Intent intent, int startId) 
    { 
     super.onStart(intent, startId); 
     PowerManager pm = (PowerManager)getApplication().getApplicationContext().getSystemService(Context.POWER_SERVICE); 
     wakeLock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK, "ScreenStay"); 
    } 

    @Override 
    public IBinder onBind(Intent intent) { 

     return null; 
    } 

    public void receivedPowerConnected() 
    { 
     try 
     { 
      Toast.makeText(context, "Power connected", Toast.LENGTH_LONG).show(); 
      wakeLock.acquire(); 
     } 
     catch (Exception ex) 
     { 
      Toast.makeText(context, ex.toString(), Toast.LENGTH_LONG).show(); 
     } 
    } 

    public void receivedPowerDisconnected() 
    { 
     try 
     { 
      Toast.makeText(context, "Power disconnected", Toast.LENGTH_LONG).show(); 
      wakeLock.release(); 
     } 
     catch (Exception ex) 
     { 
      Toast.makeText(context, ex.toString(), Toast.LENGTH_LONG).show(); 
     } 
    } 

由於該位代碼從不在oncreate或onstart中執行,喚醒鎖始終爲空。我試圖把它放在綁定函數中,但仍然沒有快樂。

當我進入android設置,我可以看到我的應用程序有服務運行,但我需要該代碼在任何工作之前被初始化。

感謝您提供的任何幫助。

UPDATE 我發現功能被稱爲感謝以前的評論。出於某種原因,調試器不會被解僱。

下面是顯示如何根據請求創建服務器的代碼。

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    startService(this); 
} 

public void onResume() 
{ 
    super.onResume(); 
    startService(this); 
} 

private void startService(Context context) 
{ 
    Intent service = new Intent(context, PowerDetectionService.class); 
    context.startService(service); 
} 

UPDATE 2 如下面要求是所有的啓動服務,並執行之後鎖定的代碼。

下面是啓動服務

public class MainActivity extends Activity { 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     StartPowerService(this); 
     //getActionBar().setDisplayHomeAsUpEnabled(true); 
    } 

    public void onResume() 
    { 
     super.onResume(); 
     StartPowerService(this); 
    } 

    private void StartPowerService(Context context) 
    { 
     Intent service = new Intent(context, PowerDetectionService.class); 
     startService(service); 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     getMenuInflater().inflate(R.menu.activity_main, menu); 
     return true; 
    } 


    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     switch (item.getItemId()) { 
      //case android.R.id.home: 
      // NavUtils.navigateUpFromSameTask(this); 
      // return true; 
     } 
     return super.onOptionsItemSelected(item); 
    } 

} 

下面是類服務

public class PowerDetectionService extends Service { 

    Context context; 
    PowerManager.WakeLock wakeLock; 

    public PowerDetectionService(Context context) 
    { 
     this.context = context; 
    } 

    public PowerDetectionService() 
    {} 

    public void onCreate() 
    { 
     super.onCreate(); 
     Log.d("SERVICE", "ON CREATE CALLED"); 
     PowerManager pm = (PowerManager)getApplication().getApplicationContext().getSystemService(Context.POWER_SERVICE); 
     wakeLock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK, "ScreenStay"); 
    } 

    public int OnStartCommand(Intent intent, int flags, int startId) 
    { 
     Log.d("SERVICE", "ONSTARTCOMMAND Called"); 
     PowerManager pm = (PowerManager)getApplication().getApplicationContext().getSystemService(Context.POWER_SERVICE); 
     wakeLock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK, "ScreenStay"); 
     return START_STICKY; 
    } 

    public void onStart(Intent intent, int startId) 
    { 
     super.onStart(intent, startId); 
     Log.d("SERVICE", "ON START CALLED"); 
     PowerManager pm = (PowerManager)getApplication().getApplicationContext().getSystemService(Context.POWER_SERVICE); 
     wakeLock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK, "ScreenStay"); 
    } 

    @Override 
    public IBinder onBind(Intent intent) { 

     return null; 
    } 

    public void receivedPowerConnected() 
    { 
     try 
     { 
      Toast.makeText(context, "Power connected", Toast.LENGTH_LONG).show(); 
      wakeLock.acquire(); 
     } 
     catch (Exception ex) 
     { 
      Toast.makeText(context, ex.toString(), Toast.LENGTH_LONG).show(); 
     } 
    } 

    public void receivedPowerDisconnected() 
    { 
     try 
     { 
      Toast.makeText(context, "Power disconnected", Toast.LENGTH_LONG).show(); 
      wakeLock.release(); 
     } 
     catch (Exception ex) 
     { 
      Toast.makeText(context, ex.toString(), Toast.LENGTH_LONG).show(); 
     } 
    } 
} 

而且下面是mainfest文件中的主要活動。

<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.BoardiesITSolutions.ScreeenStay" 
    android:versionCode="1" 
    android:versionName="1.0" > 

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

    <application 
     android:icon="@drawable/ic_launcher" 
     android:label="@string/app_name" 
     android:theme="@style/AppTheme" > 
     <activity 
      android:name=".MainActivity" 
      android:label="@string/title_activity_main" > 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 
       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
     <service android:name="PowerDetectionService" 
      android:process=":ScreenStay" 
      android:icon="@drawable/ic_launcher" 
      android:label="Screen Stay"> 
     </service> 
     <receiver android:name="BroadcastReceiveDetection"> 
      <intent-filter> 
       <action android:name="android.intent.action.BOOT_COMPLETED" /> 
       <action android:name="android.intent.action.ACTION_POWER_CONNECTED" /> 
       <action android:name="android.intent.action.ACTION_POWER_DISCONNECTED" /> 
      </intent-filter> 
     </receiver> 
    </application> 
</manifest> 

希望這會有所幫助。

+0

你確定你的方法onCreate()和onStart沒有被調用嗎?由於其他原因,您的喚醒鎖可能爲空。 –

+0

我已經在它們上面放置了斷點,它們從未被解僱。但我只是把Log.d放在代碼中,以確保它們被打印出來。爲什麼它會爲空然後 – Boardy

+0

你能告訴我們你如何開始服務? –

回答

2

你正在擴展哪個課程?服務?

方法onStart()僅用於舊的Android版本(< 2.0)。對於較新版本的你應該使用onStartCommand()波紋管:

@Override 
public int onStartCommand(Intent intent, int flags, int startId) 

而且你需要從,如果你希望服務一直執行的代碼運行後,上述方法返回START_STICKY。如果服務不能繼續運行,PowerManager.FULL_WAKE_LOCK將被釋放。也許你也可以讓wakeLock靜態。

啓動服務使用:

Intent i=new Intent(this, PowerDetectionService.class); 
    startService(i); 

--EDITED--

按照話題在這裏:getApplication() vs. getApplicationContext()你得到使用getApplicationContext()上下文時,可能會得到不同的Context對象。嘗試更改以下行:

PowerManager pm = (PowerManager)getApplication().getApplicationContext().getSystemService(Context.POWER_SERVICE); 

由:

PowerManager pm = (PowerManager)this.getSystemService(Context.POWER_SERVICE); 

regrads。

+0

謝謝你試過這個,但是這並沒有得到執行,我已經放了一個Log.d,但沒有輸出只有onCreate被調用。 – Boardy

+0

你正在擴展哪個課程? – Luis

+0

我使用擴展服務' – Boardy

0

剛一說明:

在onStart()方法已被棄用,你應該使用onStartCommand(),而不是(這將只有當你與Context.startService啓動您的服務,稱爲()

然而,的onCreate()應該如果服務不運行(但僅一次)調用。

+0

我試過onStartCommand但這從來沒有得到執行 – Boardy

4

您在不同的進程啓動服務。

<service android:name="PowerDetectionService" 
     android:process=":ScreenStay" 
     android:icon="@drawable/ic_launcher" 
     android:label="Screen Stay"> 

解雜物附着在你的主要過程中。當新的啓動時,它沒有附加調試器,所以它會忽略你的斷點。如果你想調試遠程進程,你可以在DDMS的角度通過Eclipse來完成。在設備下,你可以看到它的過程。然後您可以選擇一個並按下調試選定的過程(綠色錯誤圖標)。當您只想在某個時候開始調試您的應用程序時,這也很有用。

至於調試onCreate(),您必須在進程啓動後,但在調用onCreate()之前附加調試器。例如,您可以在onCreate()的開頭放置一些Thread.sleep()幾秒鐘,以便您可以附加調試器。

相關問題