Start Service at Bootup according to User Preference

Put your problem here if it does not fit any of the other categories.

Start Service at Bootup according to User Preference

Postby sem_thesis » Fri Mar 25, 2011 12:19 pm

Hello there!

As the thread subjects says, I want a certain service to run as soon as phone boots up, if the user has enabled the certain service.
Now, this is what I've done and this is where I tripped:


1) My service is implemented in the UpdaterService.class
2) The user gets to choose (by setting the cPhone checkbox to 1 or 0) if he wants the service enabled in UserPrivacyOptions.class

Code: Select all
package com.semesterproject.socialmine;


import android.content.Intent;
import android.os.Bundle;
import android.preference.CheckBoxPreference;
import android.preference.Preference;
import android.preference.Preference.OnPreferenceClickListener;
import android.preference.PreferenceActivity;
import android.widget.Toast;


public class UserPrivacyOptions extends PreferenceActivity implements OnPreferenceClickListener {
   

   
       /* Assign a variable name to every feature i.e. preference */
        public CheckBoxPreference cPhone;
   
   
       /* The onCreate Method loads all Privacy Options and sets a Listener on the each of them */
       
        protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);

                /* Load graphical layout from res/layout/userprivacyoptions.xml directory */
                addPreferencesFromResource(R.layout.userprivacyoptions); 


                /* Assign variables to the corresponding checkbox given in 'android:key=".."' of userprivacyoptions.xml */

                cPhone = (CheckBoxPreference) findPreference("checkboxPhone");
   

                /* Make all preferences 'listenable', i.e. to check their state and act accordingly */

                cPhone.setOnPreferenceClickListener(this);
               
                /* Initialize all Services */

                if (cPhone.isChecked())
               {
                startService(new Intent(this, UpdaterService.class));
              } else {         
                stopService(new Intent(this, UpdaterService.class));
              }       
   
        }


       
       /* The onPreferenceClick Method reacts to a User Click by either activating or deactivating services */
       
      public boolean onPreferenceClick(Preference preference) {

            if (cPhone.isChecked())
           {
            startService(new Intent(this, UpdaterService.class));
              Toast.makeText(getBaseContext(), "Activating Phone Mining now!",Toast.LENGTH_LONG).show();                        
          } else {         
            stopService(new Intent(this, UpdaterService.class));
              Toast.makeText(getBaseContext(), "Deactivating Phone Mining now!",Toast.LENGTH_LONG).show();                        
          }       

         
         return true;
      }
}



3) Now, I've added a Receiver class called StartAtBootServiceReceiver.class which reacts to phone bootup and starts service. I followed the following Tutorial to create this.

So this works fine as well.

4) Now comes the new part: StartAtBookServiceReceiver.class should only react if cPhone is checked, i.e. if cPhone.isChecked() returns the value 1.

Code: Select all
package com.semesterproject.socialmine;

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



/* This class collects privacy settings from UserPrivacyOptions.class
* and starts the respective Service at bootup is enabled */


public class StartAtBootServiceReceiver extends BroadcastReceiver {
   
   
   @Override
   public void onReceive(Context context, Intent intent)
   {
      
      /* This only enables the Service if cPhone is true
       * Somehow the app crashes when I use this code, so I commented it for now
       * Can you find any mistakes or another way to do this?
      
      UserPrivacyOptions u = new UserPrivacyOptions();
      CheckBoxPreference cp = u.cPhone;
      
      if (cp.isChecked())   {
         
            if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)) {
         
            Intent i = new Intent();
            i.setAction("com.semesterproject.socialmine.UpdaterService");
            context.startService(i);
          }      
         
      }
   
   }
   
   
}



And this results in app-crash right after bootup - GREAT!



Anyone has any idea how to tweak this??
All comments welcome!


PS: Sourcecode attached as zip-File for testing.
Attachments
SocialMine.zip
App-sourcecode
(81.19 KiB) Downloaded 23 times
sem_thesis
Freshman
Freshman
 
Posts: 9
Joined: Sun Mar 06, 2011 12:29 pm

Top

Re: Start Service at Bootup according to User Preference

Postby gqgk » Fri Mar 25, 2011 8:58 pm

Might be a n00b question, but is that the correct way to check the settings... I didnt know it saved a checkbox that way... And i think that is the incorrect way to check a preference since it wont return a value the first time you run an app...
gqgk
Freshman
Freshman
 
Posts: 8
Joined: Fri Mar 25, 2011 6:46 am

Re: Start Service at Bootup according to User Preference

Postby gqgk » Fri Mar 25, 2011 9:02 pm

Code: Select all
/* This only enables the Service if cPhone is true
       * Somehow the app crashes when I use this code, so I commented it for now
       * Can you find any mistakes or another way to do this?


is that part a direct copy/paste? if so you forgot to close off your commenting with a */ after it... My syntax knowledge isnt too great with java so i'm not sure if you'd have a problem with that or not. I would think you'd get an error since java is so particular about all that.
gqgk
Freshman
Freshman
 
Posts: 8
Joined: Fri Mar 25, 2011 6:46 am

Re: Start Service at Bootup according to User Preference

Postby sem_thesis » Sat Mar 26, 2011 3:18 pm

Hey 'gqgk'!


No - the comments were not the problem.
I used the SharedPreferences Class to solve this issue.
In case you're interested, I've attached the source code in the zip file.

Thanks for your effort!
Attachments
SocialMine.zip
(81.81 KiB) Downloaded 36 times
sem_thesis
Freshman
Freshman
 
Posts: 9
Joined: Sun Mar 06, 2011 12:29 pm

Re: Start Service at Bootup according to User Preference

Postby gqgk » Tue Mar 29, 2011 2:29 am

ah thanks :) i'll look into it
gqgk
Freshman
Freshman
 
Posts: 8
Joined: Fri Mar 25, 2011 6:46 am

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: No registered users and 13 guests