[英]App crashes on service start
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) : 我用来调用服务的代码(来自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/89/error1poh.png/
http://imageshack.us/photo/my-images/405/errorlog.png/ http://imageshack.us/photo/my-images/405/errorlog.png/
您的stopService(i)方法使应用程序崩溃了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.