Android 使用意图启动未绑定服务
示例
服务是在后台(在UI线程上)运行而无需与用户直接交互的组件。一个未绑定的服务刚刚启动,并且未绑定到任何Activity的生命周期。
要启动服务,您可以按照以下示例所示进行操作:
//该意图将用于启动服务
Intent i= new Intent(context, ServiceName.class);
//可能将数据添加到意图附加中
i.putExtra("KEY1", "Value to be used by the service");
context.startService(i);您可以通过onStartCommand()覆盖使用意图中的任何其他功能:
public class MyService extends Service {
public MyService() {
}
@Override
public int onStartCommand(Intent intent, int flags, int startId)
{
if (intent != null) {
Bundle extras = intent.getExtras();
String key1 = extras.getString("KEY1", "");
if (key1.equals("Value to be used by the service")) {
//做点什么
}
}
return START_STICKY;
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
}
热门推荐
1 甄嬛传祝福语简短
3 初八的祝福语简短
10 下雪路滑祝福语简短
11 老师简短祝福语有哪些
12 对同事简短生日祝福语
13 同事们生日祝福语简短
14 老公生日祝福语简短好看
15 车位销售祝福语简短精辟
16 婚礼仪式祝福语简短
17 考试祝福语简短给对象
18 作文结尾新年祝福语简短