Open judaco opened 7 years ago
package com.example.lesson4_services;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;
public class MainActivity extends Activity {
private MyService myService;
boolean bound = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if(!bound){
//this created the service but doesn't start it.
Intent intent = new Intent(this, MyService.class);
bindService(intent, connection, BIND_AUTO_CREATE);
}
}
private ServiceConnection connection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
// This is called when the connection with the service has been
// established, giving us the service object we can use to
// interact with the service. Because we have bound to a explicit
// service that we know is running in our own process, we can
// cast its IBinder to a concrete class and directly access it.
MyService.MyBinder binder = (MyService.MyBinder)service;
myService = binder.getService();
bound = true;
}
@Override
public void onServiceDisconnected(ComponentName name) {
// This is called when the connection with the service has been
// unexpectedly disconnected -- that is, its process crashed.
// Because it is running in our same process, we should never
// see this happen.
Toast.makeText(MainActivity.this, "disconnected", Toast.LENGTH_SHORT).show();
bound = false;
}
};
public void btnStopService(View view) {
if(bound){
bound = false;
unbindService(connection);
}
if(myService != null)
myService.stopSelf();
stopService(new Intent(this, MyService.class));
}
public void btnStartService(View view) {
Intent intent = new Intent(this, MyService.class);
startService(intent);
if(!bound){
bindService(intent, connection, BIND_AUTO_CREATE);
}
}
public void btnStam(View view) {
}
@Override
protected void onDestroy() {
super.onDestroy();
if(bound){
bound = false;
unbindService(connection);// Detach our existing connection.
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Start Service"
android:onClick="btnStartService"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Stop Service"
android:onClick="btnStopService"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Stam"
android:onClick="btnStam"/>
</LinearLayout>