第一部分 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>