第一部分 Service部分 關聯文章http://blog.csdn.net/pi9nc/article/details/18764415?locationNum=3&fps=1
public class MyService extends Service {
public String TAG = "MY_SERVICE";
public MyBinder mBinder = new MyBinder();
@Override
public void onCreate() {
super.onCreate();
Log.e(TAG, "CREATE");
}
@Override
public void onDestroy() {
super.onDestroy();
Log.e(TAG, "DESTROY");
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.e(TAG, "START_COMMAND");
return super.onStartCommand(intent, flags, startId);
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
Log.e(TAG, "BIND");
return null;
}
class MyBinder extends Binder {
public void startDownLoad() {
//執行下載的具體任務
Log.e(TAG, "執行下載任務");
}
}
}
第二部分 Activity部分
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
public Button mBtnStart, mBtnStop, mBtnBind, mBtnUnBind;
//任何一個Service在整個應用程序範圍內都是通用的,即MyService不僅可以和MainActivity建立關聯
//還可以和任何一個Activity建立關聯,而且在建立關聯時它們都可以獲取到相同的MyBinder實例。
public MyService.MyBinder myBinder;
private ServiceConnection connection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
myBinder = (MyService.MyBinder) service;
myBinder.startDownLoad();
}
@Override
public void onServiceDisconnected(ComponentName name) {
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mBtnStart = (Button) findViewById(R.id.start);
mBtnStart.setOnClickListener(this);
mBtnStop = (Button) findViewById(R.id.stop);
mBtnStop.setOnClickListener(this);
mBtnBind = (Button) findViewById(R.id.bind);
mBtnBind.setOnClickListener(this);
mBtnUnBind = (Button) findViewById(R.id.unbind);
mBtnUnBind.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.start:
Intent intent = new Intent(this, MyService.class);
startService(intent);
break;
case R.id.stop:
Intent intent1 = new Intent(this, MyService.class);
stopService(intent1);
break;
case R.id.bind:
Intent intent2 = new Intent(this,MyService.class);
//BIND_AUTO_CREATE表示在Activity和Service建立關聯後自動創建Service
//這會使得MyService中的onCreate()方法得到執行,但onStartCommand()方法不會執行。
bindService(intent2,connection,BIND_AUTO_CREATE);
break;
case R.id.unbind:
unbindService(connection);
break;
}
}
}
第三部分 佈局部分
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.liying.servicedemo.MainActivity">
<Button
android:id="@+id/start"
android:layout_width="match_parent"
android:layout_height="45dp"
android:text="開啓服務" />
<Button
android:id="@+id/stop"
android:layout_width="match_parent"
android:layout_height="45dp"
android:text="關閉服務" />
<Button
android:id="@+id/bind"
android:layout_width="match_parent"
android:layout_height="45dp"
android:text="綁定服務" />
<Button
android:id="@+id/unbind"
android:layout_width="match_parent"
android:layout_height="45dp"
android:text="解綁服務" />
</LinearLayout>
第四部分 清單文件部分
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.liying.servicedemo">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name=".MyService" />
</application>
</manifest>