应用程序崩溃的服务启动 [英] App crashes on service start

查看:181
本文介绍了应用程序崩溃的服务启动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试从活动启动一个服务,但是当点击按钮调用它的模拟器停止我的应用程序,并告诉我说:应用程序(...)已意外停止,请重试。

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"

在我的表现,我有

<服务机器人:名称=机器人:启用=真正的机器人MainService。标签=主服务>< /服务>

的codeI用于调用服务(从TimerGuardActivity)

意向书我=新的意图(TimerGuardActivity.this,MainService.class);             i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);             TimerGuardActivity.this.startService(我);

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

整个服务本身

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);
}}  

编辑:

请帮我即时困在这里了将近一个月

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/

推荐答案

您stopService(I)方法崩溃的应用程序。

your stopService(i) method crashing the app.

这篇关于应用程序崩溃的服务启动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆