1

im trying to start a service from an activity, but when the button click calls it the simulator stops my application and tells me: "the application (...) has stopped unexpectedly. please try again"

In my manifest i have:

<service android:name=".MainService" android:enabled="true" android:label="Main Service"></service>

The code i use to call the service (from TimerGuardActivity):

Intent i = new Intent(TimerGuardActivity.this, MainService.class); i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); TimerGuardActivity.this.startService(i);

The whole service itself:

package com.AirplaneModeGuard;

import java.util.Calendar;

import android.app.Service;

import android.content.Intent;

import android.content.SharedPreferences;

import android.os.IBinder;

import android.preference.PreferenceManager;

import android.provider.Settings;

public class MainService extends Service {

@Override
public IBinder onBind(Intent intent) {
    // TODO Auto-generated method stub
    return null;    

}

@Override
public void onCreate() {
    // TODO Auto-generated method stub
    super.onCreate();
}

@Override
public void onDestroy() {
    // TODO Auto-generated method stub
    super.onDestroy();
}

@Override
public void onStart(Intent intent, int startId) {
    // TODO Auto-generated method stub
    super.onStart(intent, startId);

    SharedPreferences preferences =     PreferenceManager.getDefaultSharedPreferences(getApplicationContext());     
    boolean guardE = preferences.getBoolean("guardenabled", false);
    boolean nightE = preferences.getBoolean("nightsleepenabled", false);

    if(guardE&&!nightE)
    {
        guard(false,-1);            
    }
    if(guardE&&nightE)
    {
        guard(true,checkTimeForGuard());
    }
    if(nightE&&!guardE)
    {
        nightSleep(false);
    }
    if(!guardE&&!nightE)
    {
        Intent i=new Intent(this,MainService.class);
        stopService(i);   
    }



}

private void guard(final boolean nightsleeptoggled, final int timeToRun)
{
    if(nightsleeptoggled&&timeToRun==-1){
        nightSleep(true);
        return;
    }
    new Thread(new Runnable(){
           int minutesCount=0;
           SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
           int minNum = preferences.getInt("checkeveryM", 10) ;//shared preferences

            public void run() {

                if(nightsleeptoggled)
                {
            while(true)
            {
               try {
                Thread.sleep(60000); // one minute (1000= 1sec)
                minutesCount++;

                if(minutesCount>= timeToRun)
                    break;
                if(minutesCount % minNum==0)
                {

                ToggleAirplaneMode(false);
                    Thread.sleep(60000);
                ToggleAirplaneMode(true);

                }  
                   } 
               catch (InterruptedException e) 
               {
                e.printStackTrace();
               } 
            }
            nightSleep(true);
                }

                else
                {
                    minNum*=6000;
                    while(true)
                    {
                       try 
                       {
                        Thread.sleep(minNum); // one minute
                        ToggleAirplaneMode(false);
                            Thread.sleep(60000);
                        ToggleAirplaneMode(true);                          
                       } 
                       catch (InterruptedException e) 
                        {
                        e.printStackTrace();
                        } 
                  }                     
                }

            }
            }).start();
    return;

}

private void nightSleep(boolean guardE)
{

    int TimeToSleep = checkTimeToSleep();
    if(TimeToSleep<0)
    {
        guard(true,checkTimeForGuard());
        return;
    }
    try{
        ToggleAirplaneMode(false);
        Thread.sleep(TimeToSleep*1000);
        ToggleAirplaneMode(true);

        if(guardE)
        {
            guard(true, checkTimeForGuard());
        }
        return;
    }
    catch (InterruptedException e) 
    {
    e.printStackTrace();
    } 

}

private int checkTimeToSleep()//take values of sleeptime and wakeuptime from shared preferences and calculate the total amount of minutes to sleep 
    //if the current hour is after the sleeptime use it as the sleeptime.
{
    SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
    int sleepM = preferences.getInt("SleepM", 0);
    int sleepH = preferences.getInt("SleepH", 0);
    int wakeupM = preferences.getInt("WakeUpM", 0);
    int wakeupH = preferences.getInt("WakeUpH", 0);
    int finalTime=0;
    Calendar c = Calendar.getInstance(); 
    int minutes = c.get(Calendar.MINUTE);
    int hours = c.get(Calendar.HOUR_OF_DAY);

    if(sleepH==wakeupH)
    {
        if(sleepM>wakeupM)
            finalTime =wakeupM-sleepM;
        else
            finalTime=1440-(wakeupM-sleepM);
    }

    if(sleepH>wakeupH)
    {
        if(hours<wakeupH || hours>sleepH)
        {
            if(hours<24 && hours>sleepH){
                sleepH=hours;
                sleepM=minutes;
            }
            else{
                sleepH= hours+24;
                sleepM=minutes;
                }
        }
        if(hours==sleepH && minutes>sleepM)
            sleepM=minutes;
        if(hours==wakeupH && minutes<wakeupM)
        {
            wakeupM=minutes;
            sleepH=hours+24;
        }
        finalTime = 60*(24-sleepH+wakeupH);
        finalTime+=(wakeupM-sleepM);
    }

    else if(sleepH<wakeupH)
    {
        if(hours>=wakeupH && hours<=sleepH)
        {
            sleepH=hours;
            sleepM=minutes;
        }
        finalTime = 60*(sleepH-wakeupH);
        finalTime+=(wakeupM-sleepM);
    }

    return finalTime;
}

private int checkTimeForGuard() //checks the amount of minutes guard should run in case nightsleep is enabled
{
    SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
    Calendar c = Calendar.getInstance(); 
    int minutes = c.get(Calendar.MINUTE);
    int hours = c.get(Calendar.HOUR_OF_DAY);
    int sleepM = preferences.getInt("SleepM", 0);
    int sleepH = preferences.getInt("SleepH", 0);
    int wakeupM = preferences.getInt("WakeUpM", 0);
    int wakeupH = preferences.getInt("WakeUpH", 0);

    int value=0;



    if(sleepH>wakeupH)
    {
        if(hours<wakeupH || hours>sleepH)  
            return -1;
        if(hours==sleepH && minutes>sleepM)
            return -1;
        if(hours==wakeupH && minutes<wakeupM)
            return -1;
        value = 1440-checkTimeToSleep();
        value-=((60*(hours-wakeupH))+(minutes-wakeupM));
    }

    else if(sleepH<wakeupH)
    {
        if(hours>wakeupH && hours<sleepH)
            return -1;
        if(hours==sleepH && minutes>sleepM)
            return -1;
        if(hours==wakeupH && minutes<wakeupM)
            return -1;

        if(hours>=sleepH)
        {
            value=60*(sleepH-hours);
            value+=(sleepM-minutes);
        }
        if(hours<=wakeupH)
        {
            value=60*(24+sleepH-hours);
            value+=(sleepM-minutes);
        }
    }

    return value;
}

private void ToggleAirplaneMode(boolean isEnabled)//turn off airplanemode (radiation on)=true, on=false
{
    Settings.System.putInt(
            getApplicationContext().getContentResolver(),
              Settings.System.AIRPLANE_MODE_ON, isEnabled ? 0 : 1);
}}  

EDIT:

Please help me im stuck here for almost a month

http://imageshack.us/photo/my-images/89/error1poh.png/

http://imageshack.us/photo/my-images/405/errorlog.png/

2
  • what is at line 39 of TimerGuardActivity ? (or even better post the whole code). This is where the null pointer comes from apparently.
    – Teovald
    Oct 30, 2012 at 18:04
  • can you please show your TimerGuardActivity and error on your logcat it will help us where the problem is. Oct 30, 2012 at 18:08

1 Answer 1

0

your stopService(i) method crashing the app.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.