// Path: PracticeDemos/app/src/main/java/yifeiyuan/practice/practicedemos/DownloadService.java
// public class DownloadService extends IntentService {
// 
//     public static final String TAG = "DownloadService";
// 
//     public static final String EXTRA_PATH = "yfy.practice.filepath";
// 
//     public DownloadService() {
//         super("DownloadService");
//     }
// 
//     String mUrl;
// 
//     @Override
//     protected void onHandleIntent(Intent intent) {
//         mUrl = intent.getStringExtra(EXTRA_PATH);
//         if (TextUtils.isEmpty(mUrl)) {
//             stopSelf();
//         } else {
//             download();
//         }
//     }
// 
//     private void download() {
// 
//         try {
//             //file:/storage/emulated/0/test.apk
//             File file = new File(Environment.getExternalStorageDirectory(), "test.apk");
//             Log.d(TAG, "download: file:" + file.getPath());
//             URL url = new URL(mUrl);
//             HttpURLConnection connection = (HttpURLConnection) url.openConnection();
//             connection.setRequestMethod("GET");
//             InputStream in = connection.getInputStream();
//             BufferedInputStream bis = new BufferedInputStream(in);
//             if (!file.exists()) {
//                 file.createNewFile();
//             } else {
//                 file.delete();
//                 file.createNewFile();
//             }
//             FileOutputStream fos = new FileOutputStream(file);
// 
//             byte[] buffer = new byte[1024];
//             int size = -1;
//             while ((size = bis.read(buffer)) != -1) {
//                 fos.write(buffer, 0, size);
//             }
// 
//             fos.flush();
//             fos.close();
//             bis.close();
//             connection.disconnect();
//         } catch (MalformedURLException e) {
//             e.printStackTrace();
//         } catch (IOException e) {
//             e.printStackTrace();
//         }
//     }
// 
//     @Override
//     public void setIntentRedelivery(boolean enabled) {
//         super.setIntentRedelivery(enabled);
//         Log.d(TAG, "setIntentRedelivery() called with: " + "enabled = [" + enabled + "]");
//     }
// 
//     @Override
//     public void onCreate() {
//         super.onCreate();
//         Log.d(TAG, "onCreate: ");
//     }
// 
//     @Override
//     public void onStart(Intent intent, int startId) {
//         super.onStart(intent, startId);
//         Log.d(TAG, "onStart() called with: " + "intent = [" + intent + "], startId = [" + startId + "]");
//     }
// 
//     // onStartCommand 里调用了onstart
//     @Override
//     public int onStartCommand(Intent intent, int flags, int startId) {
//         Log.d(TAG, "onStartCommand() called with: " + "flags = [" + flags + "], startId = [" + startId + "]");
//         return super.onStartCommand(intent, flags, startId);
//     }
// 
//     @Override
//     public void onDestroy() {
//         super.onDestroy();
//         Log.d(TAG, "onDestroy() called with: " + "");
//     }
// 
//     @Override
//     public IBinder onBind(Intent intent) {
//         Log.d(TAG, "onBind() called with: " + "intent = [" + intent + "]");
//         return super.onBind(intent);
//     }
// }


import android.app.Service;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;
import yifeiyuan.practice.practicedemos.DownloadService;
import yifeiyuan.practice.practicedemos.R;
package yifeiyuan.practice.practicedemos.service;



public class ServiceActivity extends AppCompatActivity {

    public static final String TAG = "MyService";
    @InjectView(R.id.start_service)
    TextView mStartService;
    @InjectView(R.id.stop_service)
    TextView mStopService;
    @InjectView(R.id.bind_service)
    TextView mBindService;
    @InjectView(R.id.unbind_service)
    TextView mUnbindService;

    @Override
// Path: PracticeDemos/app/src/main/java/yifeiyuan/practice/practicedemos/DownloadService.java
// public class DownloadService extends IntentService {
// 
//     public static final String TAG = "DownloadService";
// 
//     public static final String EXTRA_PATH = "yfy.practice.filepath";
// 
//     public DownloadService() {
//         super("DownloadService");
//     }
// 
//     String mUrl;
// 
//     @Override
//     protected void onHandleIntent(Intent intent) {
//         mUrl = intent.getStringExtra(EXTRA_PATH);
//         if (TextUtils.isEmpty(mUrl)) {
//             stopSelf();
//         } else {
//             download();
//         }
//     }
// 
//     private void download() {
// 
//         try {
//             //file:/storage/emulated/0/test.apk
//             File file = new File(Environment.getExternalStorageDirectory(), "test.apk");
//             Log.d(TAG, "download: file:" + file.getPath());
//             URL url = new URL(mUrl);
//             HttpURLConnection connection = (HttpURLConnection) url.openConnection();
//             connection.setRequestMethod("GET");
//             InputStream in = connection.getInputStream();
//             BufferedInputStream bis = new BufferedInputStream(in);
//             if (!file.exists()) {
//                 file.createNewFile();
//             } else {
//                 file.delete();
//                 file.createNewFile();
//             }
//             FileOutputStream fos = new FileOutputStream(file);
// 
//             byte[] buffer = new byte[1024];
//             int size = -1;
//             while ((size = bis.read(buffer)) != -1) {
//                 fos.write(buffer, 0, size);
//             }
// 
//             fos.flush();
//             fos.close();
//             bis.close();
//             connection.disconnect();
//         } catch (MalformedURLException e) {
//             e.printStackTrace();
//         } catch (IOException e) {
//             e.printStackTrace();
//         }
//     }
// 
//     @Override
//     public void setIntentRedelivery(boolean enabled) {
//         super.setIntentRedelivery(enabled);
//         Log.d(TAG, "setIntentRedelivery() called with: " + "enabled = [" + enabled + "]");
//     }
// 
//     @Override
//     public void onCreate() {
//         super.onCreate();
//         Log.d(TAG, "onCreate: ");
//     }
// 
//     @Override
//     public void onStart(Intent intent, int startId) {
//         super.onStart(intent, startId);
//         Log.d(TAG, "onStart() called with: " + "intent = [" + intent + "], startId = [" + startId + "]");
//     }
// 
//     // onStartCommand 里调用了onstart
//     @Override
//     public int onStartCommand(Intent intent, int flags, int startId) {
//         Log.d(TAG, "onStartCommand() called with: " + "flags = [" + flags + "], startId = [" + startId + "]");
//         return super.onStartCommand(intent, flags, startId);
//     }
// 
//     @Override
//     public void onDestroy() {
//         super.onDestroy();
//         Log.d(TAG, "onDestroy() called with: " + "");
//     }
// 
//     @Override
//     public IBinder onBind(Intent intent) {
//         Log.d(TAG, "onBind() called with: " + "intent = [" + intent + "]");
//         return super.onBind(intent);
//     }
// }


// Path: PracticeDemos/app/src/main/java/yifeiyuan/practice/practicedemos/service/ServiceActivity.java
import android.app.Service;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;
import yifeiyuan.practice.practicedemos.DownloadService;
import yifeiyuan.practice.practicedemos.R;

package yifeiyuan.practice.practicedemos.service;



public class ServiceActivity extends AppCompatActivity {

    public static final String TAG = "MyService";
    @InjectView(R.id.start_service)
    TextView mStartService;
    @InjectView(R.id.stop_service)
    TextView mStopService;
    @InjectView(R.id.bind_service)
    TextView mBindService;
    @InjectView(R.id.unbind_service)
    TextView mUnbindService;

    @Override
    protected void onCreate(Bundle savedInstanceState) {