public class MyService extends Service { @Override public IBinder onBind(Intent intent) { return null; } @Override public void onCreate() { super.onCreate(); } // 服务启动后调用 @Override public int onStartCommand(Intent intent, int flags, int startId) { return super.onStartCommand(intent, flags, startId); } @Override public void onDestroy() { super.onDestroy(); } }
// 注册 <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > …… <service android:name=".MyService" ></service> </application>
Intent startIntent = new Intent(this, MyService.class); startService(startIntent); // 启动服务 Intent stopIntent = new Intent(this, MyService.class); stopService(stopIntent); // 中止服务
onBind() 是干什么用的呢?java
服务启动之后,活动就管不住着它了。 怎么办?android
活动想指挥服务,就得借助 onBind()app
好比MyService 提供了一个下载功能,让活动什么时候去下载,随时看下载进度。ide
public class MyService extends Service { private DownloadBinder mBinder = new DownloadBinder(); @Override public IBinder onBind(Intent intent) { return mBinder; } class DownloadBinder extends Binder { public void startDownload() { Log.d("MyService", "startDownload executed"); } public int getProgress() { Log.d("MyService", "getProgress executed"); return 0; } } }
绑定和解绑ui
private ServiceConnection connection = new ServiceConnection() { @Override public void onServiceDisconnected(ComponentName name) { } @Override public void onServiceConnected(ComponentName name, IBinder service) { downloadBinder = (MyService.DownloadBinder) service; downloadBinder.startDownload(); downloadBinder.getProgress(); } }; Intent bindIntent = new Intent(this, MyService.class); bindService(bindIntent, connection, BIND_AUTO_CREATE); // 绑定服务 unbindService(connection); // 解绑服务
好比说 又想像墨迹天气同样,让服务停留在前台。this
其实还能够防止内存不足被系统回收。线程
在Service 中onCreate 方法中写code
Notification notification = new Notification(R.drawable.ic_launcher,"Notification comes", System. currentTimeMillis()); Intent notificationIntent = new Intent(this, MainActivity.class); PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,notificationIntent, 0); notification.setLatestEventInfo(this, "This is title", "This iscontent", pendingIntent); startForeground(1, notification);
上面的代码其实已通过时了,这是新的。ip
Intent intent = new Intent(this, MainActivity.class); PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0); Notification.Builder builder = new Notification.Builder(this); builder.setSmallIcon(R.mipmap.ic_launcher); builder.setTicker("Notification comes"); builder.setContentTitle("第一个通知"); builder.setContentText("天天进步一点点"); builder.setWhen(System.currentTimeMillis()); builder.setContentIntent(pendingIntent); NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); // 1是标识符,随便写。之后有新的消息会覆盖上去,固然移除的时候也能够用到。 nm.notify(1, notification);
服务都是默认在主线程中进行的,服务若是处理耗时的操做,容易引发ANR。内存
怎么办? 弄个子线程呗。
若是想在服务启动后,自动结束。调用此 stopSelf();
IntentService 就是为了防止忘记开个线程去执行服务,就提供了这个。
public class MyIntentService extends IntentService { public MyIntentService() { super("MyIntentService"); } @Override protected void onHandleIntent(Intent intent) { Log.d("MyIntentService", "Thread id is " + Thread.currentThread().getId()); } @Override public void onDestroy() { super.onDestroy(); Log.d("MyIntentService", "onDestroy executed"); } } // 启动 Intent intentService = new Intent(this, MyIntentService.class); startService(intentService);