标签:
1.start Service 不会随着activity finish 而关闭,必须调用 stop方法
每次调用都会调用onstart方法
package com.weidingqiang.customnetroid; import android.app.Service; import android.content.Intent; import android.os.Bundle; import android.os.IBinder; import android.util.Log; import java.util.ArrayList; import java.util.List; public class StartService extends Service { private static final String TAG = StartService.class.getSimpleName(); public StartService() { Log.d(TAG,"StartService()"); } @Override public void onCreate() { super.onCreate(); Log.d(TAG, "onCreate()"); } @Override public int onStartCommand(Intent intent, int flags, int startId) { Log.d(TAG,"onStartCommand()"); Bundle bundle = intent.getBundleExtra("bundle"); int flag = bundle.getInt("Flag", 0); switch (flag) { case 0: Log.d(TAG, "onStartCommand() -- 0"); List<SDownVO> list = (List<SDownVO>) bundle.getSerializable("start1"); list.size(); break; case 1: ArrayList<PDownVO> list1 = bundle.getParcelableArrayList("start2"); list1.size(); Log.d(TAG,"onStartCommand() -- 1"); break; } return super.onStartCommand(intent, flags, startId); } @Override public void onDestroy() { Log.d(TAG,"onDestroy()"); super.onDestroy(); } @Override public IBinder onBind(Intent intent) { Log.d(TAG,"onBind()"); // TODO: Return the communication channel to the service. throw new UnsupportedOperationException("Not yet implemented"); } }
package com.weidingqiang.customnetroid; import android.content.Intent; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import java.io.Serializable; import java.util.ArrayList; import java.util.List; public class StartActivity extends AppCompatActivity implements View.OnClickListener{ private Button start,start1,start2,end; private Intent intent; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_test); start = (Button) this.findViewById(R.id.start); start1 = (Button) this.findViewById(R.id.start1); start2 = (Button) this.findViewById(R.id.start2); end = (Button) this.findViewById(R.id.end); start.setOnClickListener(this); start1.setOnClickListener(this); start2.setOnClickListener(this); end.setOnClickListener(this); intent = new Intent(this,StartService.class); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.start: List<SDownVO> list = new ArrayList<SDownVO>(); list.add(new SDownVO("www.baidu.com", "百度", false, 100)); Bundle bundle = new Bundle(); bundle.putInt("Flag", 0); bundle.putSerializable("start1", (Serializable) list); intent.putExtra("bundle",bundle); startService(intent); break; case R.id.start1: ArrayList<PDownVO> list1 = new ArrayList<PDownVO>(); list1.add(new PDownVO("www.baidu.com", "百度", false, 100)); Bundle bundle1 = new Bundle(); bundle1.putInt("Flag", 1); bundle1.putParcelableArrayList("start2", list1); intent.putExtra("bundle",bundle1); startService(intent); break; case R.id.start2: finish(); break; case R.id.end: stopService(intent); break; } } @Override protected void onStop() { super.onStop(); stopService(intent); } }
2Bind 方法
此方式会随着activity关闭 而关闭
package com.weidingqiang.customnetroid; import android.app.ActivityManager; import android.content.ComponentName; import android.content.Context; 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.Button; import java.util.List; public class BindActivity extends AppCompatActivity implements View.OnClickListener{ private Button start,start1,start2,end; private Intent intent; private ServiceConnection serviceConnection; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_test); start = (Button) this.findViewById(R.id.start); start1 = (Button) this.findViewById(R.id.start1); start2 = (Button) this.findViewById(R.id.start2); end = (Button) this.findViewById(R.id.end); start.setOnClickListener(this); start1.setOnClickListener(this); start2.setOnClickListener(this); end.setOnClickListener(this); intent = new Intent(this,BindService.class); serviceConnection = new ServiceConnection() { @Override public void onServiceConnected(ComponentName name, IBinder service) { } @Override public void onServiceDisconnected(ComponentName name) { } }; } @Override public void onClick(View v) { switch (v.getId()) { case R.id.start: bindService(intent, serviceConnection,Context.BIND_AUTO_CREATE); break; case R.id.start1: Intent intent2 = new Intent(this,StartBindActivity.class); startActivity(intent2); finish(); break; case R.id.start2: finish(); break; case R.id.end: break; } } @Override protected void onStop() { super.onStop(); } @Override protected void onDestroy() { if(isServiceRunning(this,"BindService")) { unbindService(serviceConnection); } super.onDestroy(); } /** * 用来判断服务是否运行. * @param mContext * @param className 判断的服务名字 * @return true 在运行 false 不在运行 */ public static boolean isServiceRunning(Context mContext,String className) { boolean isRunning = false; ActivityManager activityManager = (ActivityManager) mContext.getSystemService(Context.ACTIVITY_SERVICE); List<ActivityManager.RunningServiceInfo> serviceList = activityManager.getRunningServices(30); if (!(serviceList.size()>0)) { return false; } for (int i=0; i<serviceList.size(); i++) { if (serviceList.get(i).service.getClassName().equals(className) == true) { isRunning = true; break; } } return isRunning; } }
package com.weidingqiang.customnetroid; import android.app.Service; import android.content.Intent; import android.os.Binder; import android.os.IBinder; import android.util.Log; public class BindService extends Service { private static final String TAG = BindService.class.getSimpleName(); /** * 在 BindService 中我们直接继承 Binder 而不是 IBinder,因为 Binder 实现了 IBinder 接口,这样我们可以少做很多工作。 */ public class SimpleBinder extends Binder{ /** * 获取 Service 实例 * @return */ public BindService getService(){ return BindService.this; } public int add(int a, int b){ return a + b; } } public SimpleBinder sBinder; public BindService() { } @Override public void onCreate() { super.onCreate(); Log.d(TAG,"onCreate()"); // 创建 SimpleBinder sBinder = new SimpleBinder(); } @Override public int onStartCommand(Intent intent, int flags, int startId) { Log.d(TAG,"onStartCommand()"); return super.onStartCommand(intent, flags, startId); } @Override public void onDestroy() { Log.d(TAG,"onDestroy()"); super.onDestroy(); } @Override public IBinder onBind(Intent intent) { // 返回 SimpleBinder 对象 return sBinder; } }
3.start and bind
package com.weidingqiang.customnetroid; import android.content.ComponentName; import android.content.Context; import android.content.Intent; import android.content.ServiceConnection; import android.os.IBinder; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.Button; import java.io.Serializable; import java.util.ArrayList; import java.util.List; public class StartBindActivity extends AppCompatActivity implements View.OnClickListener{ private static final String TAG = StartBindActivity.class.getSimpleName(); private Button start,start1,start2,end; private Intent intent; private ServiceConnection serviceConnection; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_test); start = (Button) this.findViewById(R.id.start); start1 = (Button) this.findViewById(R.id.start1); start2 = (Button) this.findViewById(R.id.start2); end = (Button) this.findViewById(R.id.end); start.setOnClickListener(this); start1.setOnClickListener(this); start2.setOnClickListener(this); end.setOnClickListener(this); intent = new Intent(this,StartBindService.class); serviceConnection = new ServiceConnection() { @Override public void onServiceConnected(ComponentName name, IBinder service) { StartBindService.SimpleBinder sBinder = (StartBindService.SimpleBinder)service; Log.v(TAG, "3 + 5 = " + sBinder.add(3, 5)); Log.v(TAG, sBinder.getService().toString()); } @Override public void onServiceDisconnected(ComponentName name) { } }; } @Override public void onClick(View v) { switch (v.getId()) { case R.id.start: startService(intent); bindService(intent, serviceConnection, Context.BIND_AUTO_CREATE); break; case R.id.start1: startService(intent); break; case R.id.start2: Intent intent2 = new Intent(this,BindActivity.class); startActivity(intent2); finish(); break; case R.id.end: stopService(intent); break; } } @Override protected void onDestroy() { super.onDestroy(); unbindService(serviceConnection); } }
package com.weidingqiang.customnetroid; import android.app.Service; import android.content.Intent; import android.os.Binder; import android.os.Bundle; import android.os.IBinder; import android.util.Log; import java.util.ArrayList; import java.util.List; public class StartBindService extends Service { private static final String TAG = StartBindService.class.getSimpleName(); /** * 在 BindService 中我们直接继承 Binder 而不是 IBinder,因为 Binder 实现了 IBinder 接口,这样我们可以少做很多工作。 */ public class SimpleBinder extends Binder { /** * 获取 Service 实例 * @return */ public StartBindService getService(){ return StartBindService.this; } public int add(int a, int b){ return a + b; } } public SimpleBinder sBinder; public StartBindService() { } @Override public void onCreate() { super.onCreate(); Log.d(TAG, "onCreate()"); // 创建 SimpleBinder sBinder = new SimpleBinder(); } @Override public int onStartCommand(Intent intent, int flags, int startId) { Log.d(TAG,"onStartCommand()"); return super.onStartCommand(intent, flags, startId); } @Override public void onDestroy() { Log.d(TAG,"onDestroy()"); super.onDestroy(); } @Override public IBinder onBind(Intent intent) { // 返回 SimpleBinder 对象 return sBinder; } }
标签:
原文地址:http://www.cnblogs.com/weidingqiang/p/5021416.html