Android读书笔记----Service的用法

内容来自于(Anroid开发艺术探索和guolin大神的博客)
这里写链接内容guolin关于servcie的讲解

Servcie用法

Service分为两种,一种是启动状态,在后台执行一些操作,一种是绑定状态,用于其他组件和Service的互动。
Service可以同时具有两种状态。
第一种的用法:

1.定义一个Service
2.在配置文件中注册
3.启动service

//声明一个Servcie
public class MyService extends Service {  

    public static final String TAG = "MyService";  

    @Override  
    public void onCreate() {  
        super.onCreate();  
        Log.d(TAG, "onCreate() executed");  
    }  

    @Override  
    public int onStartCommand(Intent intent, int flags, int startId) {  
        Log.d(TAG, "onStartCommand() executed");  
        return super.onStartCommand(intent, flags, startId);  
    }  

    @Override  
    public void onDestroy() {  
        super.onDestroy();  
        Log.d(TAG, "onDestroy() executed");  
    }  

    @Override  
    public IBinder onBind(Intent intent) {  
        return null;  
    }  
}  

注册一个Servcie

 <service android:name="com.example.servicetest.MyService" > </service> 

启动和取消一个Servcie

Intent startIntent = new Intent(this, MyService.class);  
            startService(startIntent); 
//取消Servcie
Intent stopIntent = new Intent(this, MyService.class);  
stopService(stopIntent); 

注:当启动一个Servcie时,首先会先执行他的onCreate方法和onStartCommond方法。一旦一个Servcie已经启动,再继续调用启动方法时,只会执行onStartCommond方法。

绑定Servcie的用法
1.声明一个Servcie
2.声明一个Bind对象
3.创建ServiceConnection 对象,在回调方法中执行指定操作。
4.启动和注销

声明:

public class MyService extends Service {  

    public static final String TAG = "MyService";  

    private MyBinder mBinder = new MyBinder();  

    @Override  
    public void onCreate() {  
        super.onCreate();  
        Log.d(TAG, "onCreate() executed");  
    }  

    @Override  
    public int onStartCommand(Intent intent, int flags, int startId) {  
        Log.d(TAG, "onStartCommand() executed");  
        return super.onStartCommand(intent, flags, startId);  
    }  

    @Override  
    public void onDestroy() {  
        super.onDestroy();  
        Log.d(TAG, "onDestroy() executed");  
    }  

    @Override  
    public IBinder onBind(Intent intent) {  
        return mBinder;  
    }  

    class MyBinder extends Binder {  
        public void startDownload() {  
            Log.d("TAG", "startDownload() executed");  
            // 执行具体的下载任务  
        }  
    }  
}  

声明一个ServcieConnection类

 private ServiceConnection connection = new ServiceConnection() {  

        @Override  
 public void onServiceDisconnected(ComponentName name) {  
        }  

        @Override  
 public void onServiceConnected(ComponentName  name, IBinder service) {  
            myBinder = (MyService.MyBinder) service;  
            //执行定义的方法
            myBinder.startDownload();  
        }  
    };  

启动和注销

 Intent bindIntent = new Intent(this, MyService.class);  
 bindService(bindIntent, connection, BIND_AUTO_CREATE);  

注意:这里第三个参数,当Service和Activity关联之后,自动创建Service,Servcie的onCreate方法会执行,但onStratCommond方法不会执行。

注意:一个Service如果同时具有两种状态,并且都启动了,必须同时注销两种状态,既要通过stopService停止Service又要通过unbindService 接触Service 和Activity的关联

三丶前台Service
Servcie不能完成一些特别耗时的操作,否则会报ANR错误。Servcie会在系统内存不足的情况下被回收,如果不想你的Servcie被回收,可以通过startForeground()方法让Service变成一个前台Service。

public class MyService extends Service {  

    public static final String TAG = "MyService";  

    private MyBinder mBinder = new MyBinder();  

    @Override  
    public void onCreate() {  
        super.onCreate();  
        Notification notification = new Notification(R.drawable.ic_launcher,  
                "有通知到来", System.currentTimeMillis());  
        Intent notificationIntent = new Intent(this, MainActivity.class);  
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,  
                notificationIntent, 0);  
        notification.setLatestEventInfo(this, "这是通知的标题", "这是通知的内容",  
                pendingIntent);  
        startForeground(1, notification);  
        Log.d(TAG, "onCreate() executed");  
    }  

四:远程Servcie
Servcie 是在主线程中运行的,所以执行耗时的操作时会报ANR错误,如果想要Servcie在另一个线程中完成操作,可以将你的Servcie变成远程Servcie
注意:不要轻易使用远程Servcie,那样会使bindService无法生效,因为绑定的方式是为了将Service和Activity关联,一旦将两个东西分开,会造成崩溃,可以通过AIDL(跨进程间通信解决)
使用方法:
<service android:name="com.example.servicetest.MyService"
android:process=":remote" >
</service>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值