android api分析21 Service

本文深入探讨Android中Service组件的使用方式与生命周期方法,并通过实例展示了如何启动和停止Service,以及Service的基本生命周期回调方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Service


dump()
getApplication()
onBind() //返回一个IBinder对象 应用程序通过该对象与Service组件通信
onConfigurationChanged()
onCreate() //第一次创建时回调
onDestroy() //关闭之前回调
onLowMemory()
onRebind()
onStart() //客户端调用startService(Intent)启动Service回调该方法
onStartCommand()
onTaskRemoved()
onTrimMemory()
onUnbind() //Service绑定的客户端都断开连接时回调该方法
startForeground()
stopForeground()
stopSelf()
stopSelfResult()


运行Service的两种方式
Context的startService()方法 该方法启用Service 访问者与Service没有关联 访问者退出了 Service依然运行
Context的bindService()方法 该方法启用Service 访问者与Service绑定在一起 访问者退出 Service也就
终止了

实例

public class MainActivity extends Activity
{
	Button start , stop;
	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		// 获取程序界面中的start、stop两个按钮
		start = (Button) findViewById(R.id.start);
		stop = (Button) findViewById(R.id.stop);
		//创建启动Service的Intent
		final Intent intent = new Intent();
		//为Intent设置Action属性
		intent.setAction("org.crazyit.service.FIRST_SERVICE");		
		start.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View arg0)
			{
				//启动指定Serivce
				startService(intent);	
			}
		});
		stop.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View arg0)
			{
				//停止指定Serivce
				stopService(intent);	
			}
		});		
	}
}

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="center_horizontal"
    android:orientation="horizontal" >

    <Button
        android:id="@+id/start"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="start" />

    <Button
        android:id="@+id/stop"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="stop" />

</LinearLayout>

public class FirstService extends Service
{
	// 必须实现的方法
	@Override
	public IBinder onBind(Intent arg0)
	{
		Log.e("FirstService", "onBind");
		return null;
	}
	// Service被创建时回调该方法。
	@Override
	public void onCreate()
	{
		super.onCreate();
		Log.e("FirstService", "onCreate");
	}
	// Service被启动时回调该方法
	@Override
	public int onStartCommand(Intent intent, int flags, int startId)
	{
		Log.e("FirstService", "onStartCommand");
		return START_STICKY;
	}
	// Service被关闭之前回调。
	@Override
	public void onDestroy()
	{
		super.onDestroy();
		Log.e("FirstService", "onDestroy");
	}
}
                <service android:name=".FirstService">
			<intent-filter>
				<!-- 为该Service组件的intent-filter配置action -->
				<action android:name="org.crazyit.service.FIRST_SERVICE" />
			</intent-filter>		
		</service>

运行结果

点击start按钮

onCreate
onStartCommand

再次点击start按钮

onStartCommand

点击stop按钮

onDestroy



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值