アプリケーションが起動すると、サービスが開始され(クロノメーターが開始されます)、アクティビティはこのサービスにバインドされます。アプリケーションを終了すると、サービスの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>
問題はあなたの 'mServiceBound'ブール変数にあると思います。サービスを開始するときにfalseをチェックしてみてください。 –
@VivekMishra、それは動作しません、サービスのonCreate()メソッドは、すべてのシナリオで呼び出されます:( – oiyio