2017-02-22 60 views
0

當我的應用程序啓動時,服務啓動(它啓動一個計時器),然後活動被綁定到該服務。當我殺死應用程序時,服務的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

回答

0

相反的onCreate的()的使用onActivityCreated()。 有關更多詳細信息,請使用Official link

+0

)在回顧過程中,您的答案將被視爲鏈接唯一的答案。最好將該鏈接中的一些細節添加到你的答案。 –

0

當您殺死您的應用程序時,您將殺死整個過程,而不僅僅是活動。所以你的服務也沒了,當你重新啓動你的應用程序時,它不知道以前的狀態。要解決這個問題,你應該堅持你感興趣的狀態。

相關問題