Chinaunix首页 | 论坛 | 博客
  • 博客访问: 9152510
  • 博文数量: 1727
  • 博客积分: 12961
  • 博客等级: 上将
  • 技术积分: 19860
  • 用 户 组: 普通用户
  • 注册时间: 2009-01-09 11:25
个人简介

偷得浮生半桶水(半日闲), 好记性不如抄下来(烂笔头). 信息爆炸的时代, 学习是一项持续的工作.

文章分类

全部博文(1727)

文章存档

2024年(3)

2023年(26)

2022年(112)

2021年(217)

2020年(157)

2019年(192)

2018年(81)

2017年(78)

2016年(70)

2015年(52)

2014年(40)

2013年(51)

2012年(85)

2011年(45)

2010年(231)

2009年(287)

分类: LINUX

2009-04-30 11:23:08

大略地看了一下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有关的还有一个安全的问题,可以在Manifest.xml中用标签来声明一个Service的访问权限,关于的安全问题也留待以后再解决吧。

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

下面来做个例子。

修改Manifest.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用来表示点击后可以发生一个跳转行为。

URL
阅读(2530) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~