我目前正在一個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>
希望這會有所幫助。
你確定你的方法onCreate()和onStart沒有被調用嗎?由於其他原因,您的喚醒鎖可能爲空。 –
我已經在它們上面放置了斷點,它們從未被解僱。但我只是把Log.d放在代碼中,以確保它們被打印出來。爲什麼它會爲空然後 – Boardy
你能告訴我們你如何開始服務? –