标签:
service
开启service:第一次开启时会调用onCreate
参数:intent = new Intent(this, MyService.class);serviceConnection = new ServiceConnection() {}
方法:
startService(intent);stopService(intent);//没stopService就一直存在
bindService(intent, serviceConnection, Context.BIND_AUTO_CREATE);//当创建者的类回收时,会自动调用unbindService
结束service:
如果是startService开启则需要调用stopService(intent);
如果是bindService开启则需要调用unbindService(serviceConnection);
如果startService和bindService开启,则需要先调用unbindService,在调用stopService,如果stopService在unbindService前调用的,则onDestroy会在 unbindService后调用。
MyService类:
public class MyService extends Service { private MyBinder mBinder = new MyBinder(); @Override public void onCreate() { super.onCreate(); LogUtils.d("MyService---------onCreate"); Notification notification = new Notification(R.drawable.ic_launcher, "有通知到来", System.currentTimeMillis()); Intent notificationIntent = new Intent(this, TestService.class); PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0); notification.setLatestEventInfo(this, "这是通知的标题", "这是通知的内容", pendingIntent); startForeground(1, notification);//设置为前台 Service } @Override public int onStartCommand(Intent intent, int flags, int startId) { LogUtils.d("MyService---------onStartCommand"); return super.onStartCommand(intent, flags, startId); } @Override public IBinder onBind(Intent intent) { LogUtils.d("MyService---------onBind"); return mBinder; } @Override public boolean onUnbind(Intent intent) { LogUtils.d("MyService---------onUnbind"); return super.onUnbind(intent); } @Override public boolean stopService(Intent name) { LogUtils.d("MyService---------stopService"); return super.stopService(name); } @Override public void onDestroy() { LogUtils.d("MyService---------onDestroy"); super.onDestroy(); } class MyBinder extends Binder { public void onStartDownload() { LogUtils.d("MyBinder--------onStartDownload"); } } }
调用Service类:
public class TestService extends Activity{ private Intent intent; private ServiceConnection serviceConnection; private MyService.MyBinder myBinder; private boolean bindService; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.test_service); initData(); } private void initData() { intent = new Intent(this, MyService.class); serviceConnection = new ServiceConnection() { @Override public void onServiceDisconnected(ComponentName name) { LogUtils.d("TestService-------onServiceDisconnected"); } @Override public void onServiceConnected(ComponentName name, IBinder service) { LogUtils.d("TestService-------onServiceConnected"); myBinder = (MyService.MyBinder)service; myBinder.onStartDownload(); } }; } public void Start_Service(View view) { startService(intent); } public void stop_Service(View view) { stopService(intent); } public void bind_Service(View view) { bindService = bindService(intent, serviceConnection, Context.BIND_AUTO_CREATE); } public void unbind_Service(View view) { if (bindService) { unbindService(serviceConnection); bindService = false; } } @Override public void onBackPressed() { finish(); super.onBackPressed(); } }
分类:
前台service:Service几乎都是在后台运行的,但Service的系统优先级还是比较低,因此可以设为前台service,在service调用startForeground
远程Service:注册Service的时候将它的android:process属性指定成:remote。相当于在另外一个进程中,避免anr
注意:service是运行在主线程中的。远程Service不能使用传统的bindService,这需要进行ipc。
aidl:共用远程Service。
创建步骤:
1.新建远程Service的aidl文件
标签:
原文地址:http://www.cnblogs.com/cjcblogs/p/4704129.html