您的位置:首页 > 移动开发 > Android开发

Android学习--13-服务

2016-10-20 00:00 344 查看
摘要: Service , 4大组件之一。

首先写个简单的例子

创建

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() 是干什么用的呢?

活动和服务

服务启动以后,活动就管不住着它了。 怎么办?

活动想指挥服务,就得借助 onBind()

比如MyService 提供了一个下载功能,让活动何时去下载,随时看下载进度。

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;
}
}
}

绑定和解绑

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); // 解绑服务

比如说 又想像墨迹天气一样,让服务停留在前台。

其实还可以防止内存不足被系统回收。

前台服务

在Service 中onCreate 方法中写

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);

上面的代码其实已经过时了,这是新的。

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);

IntentService

服务都是默认在主线程中进行的,服务如果处理耗时的操作,容易引起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);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Android