Android:(7)Service和Notification的体验

本文对比分析了Android中Service与Activity的区别,详细介绍了Service的生命周期、启动方式及绑定过程,并通过示例展示了如何使用Service进行后台任务处理及通知管理。

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

大略地看了一下android.app下的Service类,觉得它与Activity非常相似,只是要注意几个地方:

1.生命周期,Service的从onCreate()->onStart(int,Bundle)->onDestroy()显得更为简单。但是它的onStart是带参数的,第一个ID可用来标识这个service,第二个参数显示是用来传递数据的了。比较Activity,传递数据的Bundle是在onCreate就带进入的。

2.Service的启动由Context.startService开始,其实Activity或者Service都是Context的派生类。结束于Context.stopService()或者它自己的stopSelf()。

3.Service还有一个与Activity不一样的是它可以由另一个Context去绑定一个已存在的Service。就是这个方法Context.bindService(),被绑定的Service要求是已经onCreate了但可以没有onStart。在Service类中有个抽象方法getBinder()可以得到这个IBinder对象。关于这方面的细节,以后再看,这里只做个记录罢。

4.与Service有关的还有一个安全的问题,可以在AndroidManifest.xml中用<uses-permission>标签来声明一个Service的访问权限,关于Android的安全问题也留待以后再解决吧。

我一直相信一种水到渠成的学习方法,先从最简单的东西入手,就不会觉得学习很枯燥了。

下面来做个例子。

修改AndroidManifest.xml文件,增加一个Activity和一个Service:

<activity class=".HelloTwoD" android:label="hello_two_d"> </activity> <service class=".HelloTwoDService" /> HelloTwoD.java的代码比较简单,如下: public class HelloTwoD extends Activity implements OnClickListener ...{ public HelloTwoD() ...{ super(); } public void onCreate(Bundle icicle) ...{ super.onCreate(icicle); setTheme(android.R.style.Theme_Dark); setContentView(R.layout.maind); Button btn = (Button)findViewById(R.id.btnTest); btn.setOnClickListener(this); } @Override public void onClick(View arg0) ...{ // 用一个显式的Intent来启动服务 Intent i = new Intent(); i.setClass(this, HelloTwoDService.class); //带上我的名字 Bundle b= new Bundle(); b.putString("name", "sharetop"); this.startService(i,b); } }

当然要启动这个HelloTwoD,也需要在我最初的那个HelloTwo中加一点代码(我就不罗嗦了)。再看看那个HelloTwoDService是如何实现的:

public class HelloTwoDService extends Service ...{ public Timer timer; public final String TAG="HelloTwoDService_TAG"; public void onCreate() ...{ super.onCreate(); Log.d(TAG,"onCreate"); timer = new Timer(true); } @Override public IBinder getBinder() ...{ // TODO Auto-generated method stub return null; } public void onStart(int startId, Bundle arg) ...{ //看看startId是什么内容 if(arg!=null) Log.d(TAG,"onStart "+Integer.valueOf(startId).toString()+" from "+arg.getString("name")); else Log.d(TAG,"onStart with null Bundle"); timer.schedule(new TimerTask()...{ public void run()...{ //表示一下我的存在 Log.d(TAG,"say from a timer."); //停掉自己这个服务 HelloTwoDService.this.stopSelf(); } },5000); } public void onDestroy() ...{ Log.d(TAG,"onDestroy"); } }

这里我用一个定时器timer来延时5秒钟显示消息,否则立即就显示出来觉得不象一个后台服务了。用日志输出那个onStart中的startId看看,原来只是一个标识而已。

下面来个简单的NotificationManager吧,看了看API文档,觉得最简单地恐怕就是那个NotificationManager.notifyWithText()了,修改上面的run方法如下:

timer.schedule(new TimerTask()...{ public void run()...{ NotificationManager manager=(NotificationManager)getSystemService(NOTIFICATION_SERVICE); manager.notifyWithText(1001, "わたしはSHARETOPです。", NotificationManager.LENGTH_LONG, null); HelloTwoDService.this.stopSelf(); } },5000);


再试试看效果。太简单了,Notification主要是用于后台服务用来通知前台,所以,Android提供了三类不同的通知方式,notifyWithText可以简单地显示一个字串,而notifyWithView稍复杂点,可以有一个view来构造这个显示信息框,而最灵活的就是那个notify(int id, Notification notification)了,参数notification是类Notification的实例。

修改一下刚才的那个run方法,如下:

timer.schedule(new TimerTask()...{ public void run()...{ NotificationManager manager=(NotificationManager)getSystemService(NOTIFICATION_SERVICE); Notification nf = new Notification(R.drawable.icon,"这是信息的详细描述",null,"信息的标题",null); manager.notify(0,nf); HelloTwoDService.this.stopSelf(); } },5000);

这里创建一个Notification的实例nf,构造函数的第一个参数是那个显示在状态栏(也就是Android手机上面的那一条显示信号强度、电池电量等信息的位置)的图标。后面可以有

一个标题和点击以后的详细信息,这是字串形式,还可以有一个Intent用来表示点击后可以发生一个跳转行为。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值