我试图从活动中启动服务,但是当按钮单击调用它时,模拟器停止我的应用程序并告诉我:“应用程序(...)已意外停止。请再试一次”
在我的清单中,我有:
<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);
}}
编辑:
请帮助我,我在这里困了将近一个月