Android-Service&Notification

来源:互联网 发布:cookie的js代码调用 编辑:程序博客网 时间:2024/06/07 20:20

Notification

当某个应用不在前台运行,想发出一些提示信息时,就可以借助Notification来实现。
创建Notification首先需要创建NotificationManager实例来对其进行管理。

NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

然后通过support-v4库中的NotificationCompat类的构造器来创建Notification。

Notification notification = new NotificationCompat.Builder(context).build();

最后调用NOtificationManager的notify()方法让其显示出来,第一个参数是Notificaton的id。

manager.notify(1, notification);

但此时的Notification只是一个空的对象,可以通过在build()之前增加一系列set方法增添其功能。

  • setContentTitle(String);
  • setContentText(String);

分别指定Notification的标题内容和正文内容。

  • setWhen(System,currentTimeMills());

指定通知被创建的时间。

  • setSmallIcon();
  • setLargeIcon();

分别指定通知的小图标和大图标。

  • setSound(URI);

指定通知发出时播放一段音频。

  • setVibrate(new Long[] {0, 1000, 1000, 1000});

指定通知发出时的振动频率,长整型的数组,下标为奇数的表示手机静止时长,下标为偶数的表示手机振动时长,单位为毫秒。

  • setDefaults(NotificationCompat.DEFAULT_ALL);

指定通知发出时的效果由手机环境来决定。

  • setContentIntent(PendingIntent);

PendingIntent相当于一个延时执行的Intent,它提供了getActivity() getBroadcast() getService()方法来获得实例。

Intent intent = new Intent(this, anotherActivity.class);PendingIntent pi = PendingIntent.getActivity(this, 0, intent, 0);
  • setAutoCancel(true);

指定通知被点击后,会自动取消。

Service

Service是一种计算型组件,用于在后台执行一系列计算任务。Service组件有两种运行模式:

  • 运行状态
    Service内部可以做一些后台计算,并且不需要和外界有直接的交互。
  • 绑定状态
    Service内部也可以做一些后台计算,但外界可以方便地和Service组件进行通信。

创建Service

创建Service的方法和静态注册BroadcastReceiver的方法类似
首先创建一个自定义Service类继承Service,并重写onBinder()方法。
另外,Service中处理事情的方法是onStartCommand()

class MyService extends Service{    public MyService{    }    @Override    public IBinder onBind(Intent intent){        return null;    }    @Override    public int onStartCommand(Intent intent, int flags, int startId){        return super.onStartCommand(Intent intent, int flags, int startId);    }}

然后在AndroidManifest.xml中进行注册

  • android:enabled表示是否启用这个服务
  • android:exported表示是否允许本程序之外的程序访问这个服务
...<service    android:name=".MyServicer"    android:enabled="true"    android:exported="true"></service>...

启动和停止Service

启动Service的方法与启动Activity的方法类似,是由Intent触发的。然后调用Context的startService()方法。
停止Service则是调用Context的stopService()方法,或者在MyService内部调用stopSelf()方法。

Intent intent = new Intent(this, MyService.class);startService(intent);stopService(intent);

Activity与Service通信

Activity与Service通信,是通过Activity绑定Service实现的。Context的bindService()方法和unbindService()分别用于绑定服务和解绑服务。

Intent intent = new Intent(this, MyService.class);//BIND_AUTO_CREATE表示Activity与Service绑定后自动创建服务bindService(intent, connection, BIND_AUTO_CREATE);

此方法的第二个参数是一个ServiceConnection类型的,ServiceConnection中有两个方法onServiceConnected()和onServiceDisconnected(),分别会在Activity与Service绑定和解绑时调用。

class MainActivity extends AppCompatActivity{    private MyService.MyBinder myBinder;    private ServiceConnection connection = new ServiceConnection(){        @Override        public void onServiceDisconnected(ComponentName name){        }        @Override        public void onServiceConnected(ComponentName name, IBinder service){            myBinder = (MyBinder) service;            myBinder.method();        }    };}

在onServiceConnected()方法中IBinder向下转型得到一个MyBinder类型的实例,MyBinder是继承Binder在M有Service中实现内部类,并且可以在其中自定义一些方法,然后通过onServiceConnected()中调用,实现了Activity管理Service的目的。最后重写MyService类中的onBinder()方法,使得其他Activity绑定时,获取相同的MyBinder实例。

class MyService extends Service{    private MyBinder myBinder;    class MyBinder extends Binder{        public void method(){            //do something        }    }    @Override    public IBinder onBind(Intent intent){        return myBinder;    }}

Service的生命周期

  • 调用startService()和stopService()
    onCreate()→onStartCommand()→onDestroy()
  • 调用bindService()和unbindService()
    onCreate()→onBind()→onStartCommand()→onDestroy()

前台服务

创建前台服务和创建通知很类似,只是在构建出Notification后,并没有使用NotificationManager的notify()将通知显示出来,而是调用了startForeground()方法让Service变成一个前台服务,并在系统状态栏显示出来。

Intent intent = new Intent(this, MainActivity.class);PendingIntent pi = PendingIntent.getActivity(this, 0, intent, 0);Notification notification = new NotificatonCompat.Builder(this).build();startForeground(1, notification);
0 0
原创粉丝点击