在onCreate之前,活动无法使用Android系统服务

时间:2014-08-12 17:39:44

标签: java android

所以我在服务中有一个计时器,每次运行时我都会收到此错误System services not available to Activities before onCreate() __________________________________________________________________________________________


服务代码

import android.app.IntentService;
import android.app.Service;
import android.content.Intent;
import android.os.Handler;
import android.os.IBinder;
import android.os.Looper;

import java.util.Timer;
import java.util.TimerTask;

public class MyService extends Service {
    Locator locator;
    Timer myTimer;
    @Override
    public void onCreate() {
        locator = new Locator();
        myTimer = new Timer();
    }
    private class MyTimerTask extends TimerTask
    {

        @Override
        public void run() {
            Handler handler = new Handler(Looper.getMainLooper());

            handler.postDelayed(new Runnable() {
                @Override
                public void run() {

                    locator.TemperatureCatch();
                }
            }, 1000);
        }
    }

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {

        MyTimerTask myTimerTask = new MyTimerTask();
            myTimer.scheduleAtFixedRate(myTimerTask, 0, 15000);
        return START_STICKY;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        myTimer.cancel();
    }
}

切换按钮代码(如果它有助于此代码高于onCreate()方法):

 @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
    {

        if (isChecked)
        {
 startService(new Intent(this, MyService.class));
            Toast.makeText(this,"Check-In will be done every 15 seconds",Toast.LENGTH_SHORT).show();
        }
        else
        {
stopService(new Intent(this, MyService.class));
            Toast.makeText(this,"Manual Check-In enabled",Toast.LENGTH_SHORT).show();
        }
}

我正在运行的方法:

 public String TemperatureCatch()
    {
       /*--> this line throws the error*/ Vibrator vibrate = (Vibrator) this.getSystemService(Context.VIBRATOR_SERVICE);
        Spinner reeferchoice = (Spinner) findViewById(R.id.optionselecti);
        String reeferChoicei = reeferchoice.getSelectedItem().toString();
        if(reeferChoicei.equals("Yes"))
        {
            final ToneGenerator tg = new ToneGenerator(AudioManager.STREAM_NOTIFICATION, 500);
            tg.startTone(ToneGenerator.TONE_CDMA_ABBR_ALERT);
            vibrate.vibrate(300);
            AlertDialog.Builder alert = new AlertDialog.Builder(this);
            alert.setTitle("Temperature");
            alert.setMessage("Input Temperature in F° (-20 to 65) ");
            final EditText input = new EditText(this);
            input.setInputType(InputType.TYPE_CLASS_PHONE);
            alert.setView(input);
            alert.setPositiveButton("Check-In", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {
                    temperaturei = input.getText().toString();
                    Toast.makeText(getBaseContext(),"Updated",Toast.LENGTH_SHORT).show();
                    Updater(temperaturei);
                }
            });
            alert.show();
        }
        else if (reeferChoicei.equals("No"))
        {
            temperaturei = "DRY";
            Updater(temperaturei);
        }
        return temperaturei;
    }

1 个答案:

答案 0 :(得分:4)

错误消息解释了您的问题 - 在调用服务的onCreate()之前,不要尝试创建对象(特别是那些需要系统API的对象,但最好不要做任何设计实践)。

public class MyService extends Service {
    Locator locator = new Locator();
    Timer myTimer = new Timer();
    ...

应该是

public class MyService extends Service {
    Locator locator;
    Timer myTimer;
    @Override
    public void onCreate() {
        locator = new Locator();
        myTimer = new Timer();
    }
    ...

或者可能是其他一种服务生命周期方法更适合您实际执行的操作。