本站首页    管理页面    写新日志    退出


«December 2025»
123456
78910111213
14151617181920
21222324252627
28293031


公告
本博客在此声明所有文章均为转摘,只做资料收集使用。并无其他商业用途。

我的分类(专题)

日志更新

最新评论

留言板

链接

Blog信息
blog名称:
日志总数:210
评论数量:205
留言数量:-19
访问次数:928794
建立时间:2007年5月10日




[Android]Android学习笔记(7)-关于Service和Notification的体验
文章收藏,  网上资源,  软件技术,  电脑与网络

李小白 发表于 2008/10/31 15:18:32

颜承ID:sharetop http://blog.csdn.net/sharetop/archive/2008/01/04/2025961.aspx 大略地看了一下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: 500)this.width=500'>        <activity class=".HelloTwoD" android:label="hello_two_d">500)this.width=500'>        </activity>500)this.width=500'>        <service class=".HelloTwoDService" /> HelloTwoD.java的代码比较简单,如下: 500)this.width=500'>public class HelloTwoD extends Activity implements OnClickListener500)this.width=500'>500)this.width=500'>...{ 500)this.width=500'> public HelloTwoD()500)this.width=500'>500)this.width=500'>    ...{500)this.width=500'>  super();500)this.width=500'>    }500)this.width=500'>500)this.width=500'> public void onCreate(Bundle icicle) ...{500)this.width=500'>        super.onCreate(icicle);500)this.width=500'>        setTheme(android.R.style.Theme_Dark);500)this.width=500'>        setContentView(R.layout.maind);500)this.width=500'>        500)this.width=500'>        Button btn = (Button)findViewById(R.id.btnTest);500)this.width=500'>        btn.setOnClickListener(this);500)this.width=500'>    }500)this.width=500'> 500)this.width=500'> @Override500)this.width=500'>500)this.width=500'> public void onClick(View arg0) ...{500)this.width=500'>  // 用一个显式的Intent来启动服务 500)this.width=500'>  Intent i = new Intent();500)this.width=500'>  i.setClass(this, HelloTwoDService.class);500)this.width=500'>  //带上我的名字500)this.width=500'>  Bundle b= new Bundle();500)this.width=500'>  b.putString("name", "sharetop");500)this.width=500'>  this.startService(i,b);500)this.width=500'> }500)this.width=500'> 500)this.width=500'>}500)this.width=500'>500)this.width=500'> 当然要启动这个HelloTwoD,也需要在我最初的那个HelloTwo中加一点代码(我就不罗嗦了)。再看看那个HelloTwoDService是如何实现的: 500)this.width=500'>500)this.width=500'>public class HelloTwoDService extends Service ...{500)this.width=500'> public Timer timer;500)this.width=500'> public final String TAG="HelloTwoDService_TAG";500)this.width=500'>500)this.width=500'> public void onCreate() ...{500)this.width=500'>        super.onCreate();500)this.width=500'>        500)this.width=500'>        Log.d(TAG,"onCreate");500)this.width=500'>        500)this.width=500'>        timer = new Timer(true);500)this.width=500'>500)this.width=500'>    }500)this.width=500'> @Override500)this.width=500'>500)this.width=500'> public IBinder getBinder() ...{500)this.width=500'>  // TODO Auto-generated method stub500)this.width=500'>  return null;500)this.width=500'> }500)this.width=500'> public void onStart(int startId, Bundle arg)500)this.width=500'>500)this.width=500'> ...{ 500)this.width=500'>  //看看startId是什么内容 500)this.width=500'>  if(arg!=null)500)this.width=500'>   Log.d(TAG,"onStart "+Integer.valueOf(startId).toString()+" from "+arg.getString("name"));500)this.width=500'>  else500)this.width=500'>   Log.d(TAG,"onStart with null Bundle");500)this.width=500'>  500)this.width=500'>500)this.width=500'>        timer.schedule(new TimerTask()...{500)this.width=500'>500)this.width=500'>         public void run()...{500)this.width=500'>          //表示一下我的存在500)this.width=500'>          Log.d(TAG,"say from a timer.");500)this.width=500'>          //停掉自己这个服务500)this.width=500'>          HelloTwoDService.this.stopSelf();500)this.width=500'>         }         500)this.width=500'>        },5000);500)this.width=500'> }500)this.width=500'> public void onDestroy()500)this.width=500'>500)this.width=500'> ...{500)this.width=500'>  Log.d(TAG,"onDestroy");500)this.width=500'> }500)this.width=500'>}500)this.width=500'>500)this.width=500'> 这里我用一个定时器timer来延时5秒钟显示消息,否则立即就显示出来觉得不象一个后台服务了。用日志输出那个onStart中的startId看看,原来只是一个标识而已。 下面来个简单的NotificationManager吧,看了看API文档,觉得最简单地恐怕就是那个NotificationManager.notifyWithText()了,修改上面的run方法如下: 500)this.width=500'>500)this.width=500'>        timer.schedule(new TimerTask()...{500)this.width=500'>500)this.width=500'>         public void run()...{500)this.width=500'>          500)this.width=500'>          NotificationManager manager=(NotificationManager)getSystemService(NOTIFICATION_SERVICE);500)this.width=500'>          manager.notifyWithText(1001, "わたしはSHARETOPです。", NotificationManager.LENGTH_LONG, null);500)this.width=500'>          500)this.width=500'>          HelloTwoDService.this.stopSelf();500)this.width=500'>         }         500)this.width=500'>        },5000); 再试试看效果。太简单了,Notification主要是用于后台服务用来通知前台,所以,Android提供了三类不同的通知方式,notifyWithText可以简单地显示一个字串,而notifyWithView稍复杂点,可以有一个view来构造这个显示信息框,而最灵活的就是那个notify(int id, Notification notification)了,参数notification是类Notification的实例。 修改一下刚才的那个run方法,如下: 500)this.width=500'>500)this.width=500'>        timer.schedule(new TimerTask()...{500)this.width=500'>500)this.width=500'>         public void run()...{500)this.width=500'>          500)this.width=500'>          NotificationManager manager=(NotificationManager)getSystemService(NOTIFICATION_SERVICE);          500)this.width=500'>          Notification nf = new Notification(R.drawable.icon,"这是信息的详细描述",null,"信息的标题",null);500)this.width=500'>          manager.notify(0,nf);          500)this.width=500'>          500)this.width=500'>          HelloTwoDService.this.stopSelf();500)this.width=500'>         }         500)this.width=500'>        },5000); 这里创建一个Notification的实例nf,构造函数的第一个参数是那个显示在状态栏(也就是Android手机上面的那一条显示信号强度、电池电量等信息的位置)的图标。后面可以有 一个标题和点击以后的详细信息,这是字串形式,还可以有一个Intent用来表示点击后可以发生一个跳转行为。 OK,今天到此为止。下次该体验各种View了。


阅读全文(2095) | 回复(0) | 编辑 | 精华
 



发表评论:
昵称:
密码:
主页:
标题:
验证码:  (不区分大小写,请仔细填写,输错需重写评论内容!)



站点首页 | 联系我们 | 博客注册 | 博客登陆

Sponsored By W3CHINA
W3CHINA Blog 0.8 Processed in 0.516 second(s), page refreshed 144800748 times.
《全国人大常委会关于维护互联网安全的决定》  《计算机信息网络国际联网安全保护管理办法》
苏ICP备05006046号