I am currently working on an android project and I am trying to start a service and when the service has started running some code to initialise some stuff.
Below is the code I am using for the service.
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();
}
}
The wake lock is always null as that bit of code never gets executed in the oncreate or onstart. I've tried putting it in the bind function but still no joy.
When I go into the android settings I can see that my app has the service running but I need that code to be initialised before anything would work.
Thanks for any help you can provide.
UPDATE I've discovered that the functions are being called thanks to the previous comment. For some reason the debugger doesn't get fired.
Below is the code that shows how to create the server as requested.
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 As requested below is all the code that starts the service and performs the wake lock.
Below is the main activity that starts the service
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);
}
}
Below is the class for the service
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();
}
}
}
And below is the mainfest file.
<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>
Hope this helps.
The Android documentation says the method onCreate is called only when is starting or when is destroyed. Why is this happening? Thanks!! Regars.
@OnCreate is only for initial creation, and thus should only be called once. If you have any processing you wish to complete multiple times you should put it elsewhere, perhaps in the @OnResume method.
onCreate() method gets called when activity created and onStart() method called when activity comes from onPause() or onResume() . onCreate() , will be called in two scenarios , one when activity is first created, and second when activity goes through configuration changes.
As onCreate() of an Activity is called only once, this is the point where most initialization should go: calling setContentView(int) to inflate the activity's UI, using findViewById to programmatically interact with widgets in the UI, calling managedQuery(android.
You are starting service in different process.
<service android:name="PowerDetectionService"
android:process=":ScreenStay"
android:icon="@drawable/ic_launcher"
android:label="Screen Stay">
The debugger is attached to your main process. When the new one starts, it has no debugger attached, so it will ignore your breakpoints. If you want to debug remote process, you can do it via Eclipse in DDMS perspective. Under devices, you can see its processes. Then you can select one and press debug selected process (green bug icon). This is also useful when you only want to start debugging your app at some point.
As for debugging onCreate()
, you have to attach debugger after the process started, but before onCreate()
is called. You can for example put some Thread.sleep()
in the beginning of onCreate()
for few seconds so you can attach debugger.
Which class are you extending? Service?
The method onStart()
is only used for old Android versions (<2.0). For more recent versions you should use onStartCommand()
as bellow:
@Override
public int onStartCommand(Intent intent, int flags, int startId)
And you need to return START_STICKY
from the method above if you want the service to keeps running after executing the code. If service is not kept alive the PowerManager.FULL_WAKE_LOCK
will be released. Probably you can also get away making wakeLock
static.
To start service use:
Intent i=new Intent(this, PowerDetectionService.class);
startService(i);
--EDITED--
As per the topic here: getApplication() vs. getApplicationContext() you may get different Context
object when getting the context using getApplicationContext()
. Try change the following line:
PowerManager pm = (PowerManager)getApplication().getApplicationContext().getSystemService(Context.POWER_SERVICE);
by:
PowerManager pm = (PowerManager)this.getSystemService(Context.POWER_SERVICE);
regrads.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With