oncreate()方法在应用程序被终止时被调用。为什么?

问题描述:

当我的应用程序启动时,服务启动(它启动一个计时器),然后活动被绑定到该服务。当我杀死应用程序时,服务的onCreate()方法被调用,因此计时器重新启动。为什么要调用onCreate()方法?我不希望我的天文钟重新启动,我希望它从离开的地方继续。oncreate()方法在应用程序被终止时被调用。为什么?

MainActivity.java

public class MainActivity extends AppCompatActivity { 
    BoundService mBoundService; 
    boolean mServiceBound = false; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     final TextView timestampText = (TextView) findViewById(R.id.timestamp_text); 
     Button printTimestampButton = (Button) findViewById(R.id.print_timestamp); 
     Button stopServiceButon = (Button) findViewById(R.id.stop_service); 
     Button startServiceButon = (Button) findViewById(R.id.start_service); 
     printTimestampButton.setOnClickListener(new OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       if (mServiceBound) { 
        timestampText.setText(mBoundService.getTimestamp()); 
       } 
      } 
     }); 

     stopServiceButon.setOnClickListener(new OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       if (mServiceBound) { 
        unbindService(mServiceConnection); 
        mServiceBound = false; 
       } 
       Intent intent = new Intent(MainActivity.this, 
         BoundService.class); 
       stopService(intent); 
      } 
     }); 

     startServiceButon.setOnClickListener(new OnClickListener(){ 
      @Override 
      public void onClick(View v) { 
       if (!mServiceBound) { 
        Intent intent = new Intent(MainActivity.this, BoundService.class); 
        startService(intent); 
        bindService(intent, mServiceConnection, Context.BIND_AUTO_CREATE); 
        mServiceBound = true; 
       } 

      } 
     }); 
    } 

    @Override 
    protected void onStart() { 
     super.onStart(); 
     Log.v("mainactivity", "in onStart"); 
     Intent intent = new Intent(this, BoundService.class); 
     startService(intent); 
     bindService(intent, mServiceConnection, Context.BIND_AUTO_CREATE); 
     mServiceBound = true; 
    } 

    @Override 
    protected void onStop() { 
     super.onStop(); 
     if (mServiceBound) { 
      unbindService(mServiceConnection); 
      mServiceBound = false; 
     } 
    } 

    private ServiceConnection mServiceConnection = new ServiceConnection() { 
     @Override 
     public void onServiceDisconnected(ComponentName name) { 
      mServiceBound = false; 
     } 

     @Override 
     public void onServiceConnected(ComponentName name, IBinder service) { 
      MyBinder myBinder = (MyBinder) service; 
      mBoundService = myBinder.getService(); 
      mServiceBound = true; 
     } 
    }; 
} 

BoundService.java

public class BoundService extends Service { 
    private static String LOG_TAG = "BoundService"; 
    private IBinder mBinder = new MyBinder(); 
    private Chronometer mChronometer; 

    @Override 
    public void onCreate() { 
     super.onCreate(); 
     Log.v(LOG_TAG, "in onCreate"); 
     mChronometer = new Chronometer(this); 
     mChronometer.setBase(SystemClock.elapsedRealtime()); 
     mChronometer.start(); 
    } 

    @Override 
    public IBinder onBind(Intent intent) { 
     Log.v(LOG_TAG, "in onBind"); 
     return mBinder; 
    } 

    @Override 
    public void onRebind(Intent intent) { 
     Log.v(LOG_TAG, "in onRebind"); 
     super.onRebind(intent); 
    } 

    @Override 
    public boolean onUnbind(Intent intent) { 
     Log.v(LOG_TAG, "in onUnbind"); 
     return true; 
    } 

    @Override 
    public void onDestroy() { 
     super.onDestroy(); 
     Log.v(LOG_TAG, "in onDestroy"); 
     mChronometer.stop(); 
    } 

    public String getTimestamp() { 
     long elapsedMillis = SystemClock.elapsedRealtime() 
       - mChronometer.getBase(); 
     int hours = (int) (elapsedMillis/3600000); 
     int minutes = (int) (elapsedMillis - hours * 3600000)/60000; 
     int seconds = (int) (elapsedMillis - hours * 3600000 - minutes * 60000)/1000; 
     int millis = (int) (elapsedMillis - hours * 3600000 - minutes * 60000 - seconds * 1000); 
     return hours + ":" + minutes + ":" + seconds + ":" + millis; 
    } 

    public class MyBinder extends Binder { 
     BoundService getService() { 
      return BoundService.this; 
     } 
    } 
} 

activity_main.xml中

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="#FFFFFF" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    tools:context="oiyioz.com.a13_local_bound_service_4.MainActivity" > 

    <ImageView 
     android:id="@+id/imageView1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:layout_centerHorizontal="true" 
     android:layout_marginBottom="-40dp" 
     android:src="@drawable/truiton_sq" /> 

    <Button 
     android:id="@+id/print_timestamp" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentTop="true" 
     android:layout_centerHorizontal="true" 
     android:layout_marginTop="130dp" 
     android:text="Print Timestamp" /> 

    <TextView 
     android:id="@+id/timestamp_text" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/print_timestamp" 
     android:layout_centerHorizontal="true" 
     android:layout_marginTop="120dp" 
     android:text="" 
     android:textAppearance="?android:attr/textAppearanceLarge" /> 

    <Button 
     android:id="@+id/stop_service" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/print_timestamp" 
     android:layout_centerHorizontal="true" 
     android:text="Stop Service" /> 

    <Button 
     android:id="@+id/start_service" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/stop_service" 
     android:layout_centerHorizontal="true" 
     android:text="Start Service" /> 

</RelativeLayout> 
+0

我认为问题出在你的'mServiceBound'布尔变量上。尝试在开始服务时检查它是否为false。 –

+0

@VivekMishra,它不起作用,在所有场景中调用服务的onCreate()方法:( – oiyio

相反的onCreate的()的使用onActivityCreated()。 有关更多详细信息,请使用Official link

+0

)在回顾过程中,您的答案将被视为链接唯一的答案。最好将该链接中的一些细节添加到你的答案。 –

当您杀死您的应用程序时,您将杀死整个过程,而不仅仅是活动。所以你的服务也没了,当你重新启动你的应用程序时,它不知道以前的状态。要解决这个问题,你应该坚持你感兴趣的状态。