我目前正在开发一个 android 项目,我正在尝试启动一个服务,当该服务开始运行一些代码来初始化一些东西时。
以下是我用于该服务的代码。
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();
}
}
}
下面是清单文件。
<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>
希望这可以帮助。