gpt4 book ai didi

java - 应用程序在服务启动时崩溃

转载 作者:行者123 更新时间:2023-12-01 05:04:21 25 4
gpt4 key购买 nike

我尝试从 Activity 启动服务,但是当单击按钮调用它时,模拟器会停止我的应用程序并告诉我:“应用程序(...)已意外停止。请重试”

在我的 list 中我有:

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

我用来调用服务的代码(来自 TimerGuardActivity):

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

编辑:

请帮助我,我在这里被困了将近一个月

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

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

最佳答案

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

关于java - 应用程序在服务启动时崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12935646/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com