美文网首页组件
Android开发高级进阶——Service与Notificat

Android开发高级进阶——Service与Notificat

作者: trampcr | 来源:发表于2016-09-13 11:04 被阅读772次

    众所周知,Service是Android中实现程序后台运行的解决方案,非常适合于执行不需要和用户交互而且要求长期运行的任务。Notification是显示在手机状态栏的通知,通过Notification和startForeground可以将Service后台服务设置为前台服务。

    一. Service的分类


    按种类分为LocalService和RemoteService

    • LocalService:当前进程的Service,依附在主进程上,节约资源,通信不需要进程间通信,但主进程被杀掉时LocalService也会被停止。一般可以做播放操作。
    • RemoteService:远程进程的Service,是一个独立的进程,当主进程被杀掉时,其服务依然会运行,比较灵活,但使用复杂。可以做一些系统常驻进程或守护进程(当主进程被杀掉时,可以用守护进程唤醒主进程)。

    按类型分为前台Service和后台Service

    • 前台Service:在通知栏一直显示的服务,最大程度保证服务不被杀掉。
    • 后台Service:默认的服务为后台服务,看不见。

    前台Service的优先级高于后台Service。

    启动方式有startService和bindService

    • startService:onCreate只会在Service第一次创建的时候被调用,start一次之后,下次不会再onCreate了,每次会执行onStartCommand。
    • bindService:Service中的onBind是与外界进行关联的。onBind返回的IBinder都会在与其他组件连接时返回,即IBinder是一个负责沟通的桥梁。

    二. Service、Thread、Process


    Service是处理一些后台任务,在主线程中,并不能执行耗时操作。而Thread是开启一个子线程,在子线程中执行耗时操作,这样不会阻塞主线程。

    那么Service与Thread、Process有什么关系呢?

    其实它们之间并无太大关联,只是Service可以放在其他进程中,Service是进程中的,Thread也是进程中的,Service中耗时操作又可以在通过Thread执行。Service中的IBinder可以帮助我们与其他进程进行通信。

    获取当前进程:Thread.currentThread().getId();

    在Service中创建一个子线程与Activity中创建一个子线程有什么区别?

    当Activity被销毁,就无法获取在被销毁的Activity中创建的子线程实例了。例如,Activity创建了一个子线程在后台运行,执行完之后,返回数据时,创建它的Activity已被销毁,这是线程不安全的。再例如,Activity1创建了一个子线程,Activity2是无法对Activity1创建的子线程进行操作。
    而Service是一个服务,所有的Activity都可以与该Service进行关联,即使Activity被销毁,但依然可以获取Service中的IBinder的实例。
    所以,用Service处理后台任务,Activity就可以放心的被finish掉了,完全不用担心对后台任务无法进行控制。

    三. AndroidManifest.xml中Service的一些标签


    • name:服务的类名。
    • label:服务名,如果不设置,默认为类名。
    • icon:服务的图标。
    • permission:服务的权限。
    • process:服务的进程名。
    • enabled:boolean类型,true表示该服务默认被系统启动。
    • exported:表示该服务是否能够被其他应用程序控制或连接,是否向外通过服务。

    四. Notification


    1. 创建通知
      可以在NotificationCompat.Builder对象中为通知指定 UI 信息和操作。要创建通知,请调用NotificationCompat.Builder.build(),它将返回包含具体规范的Notification对象。要发出通知,请通过调用NotificationManager.notify()将 [Notification对象传递给系统。

    2. 必需的通知内容
      Notification对象必须包含以下内容:

    • 小图标,由setSmallIcon()设置。
    • 标题,由setContentTitle()设置。
    • 详细文本,由setContentText()设置。
    1. 通知操作
      尽管通知操作都是可选的,但是至少应向通知添加一个操作。 操作允许用户直接从通知转到应用中的Activity,用户可在其中查看一个或多个事件或执行进一步的操作。
      一个通知可以提供多个操作。应该始终定义一个当用户点击通知时会触发的操作;通常,此操作会在应用中打开Activity。也可以向通知添加按钮来执行其他操作,例如,暂停闹铃或立即答复短信。
      在Notification内部,操作本身由PendingIntent定义,后者包含在应用中启动Activity的Intent。要将PendingIntent与手势相关联,请调用NotificationCompat.Builde的适当方法。例如,如果要在用户点击抽屉式通知栏中的通知文本时启动Activity,则可通过调用setContentIntent()来添加PendingIntent。

    创建简单通知

    以下代码段说明了一个指定某项 Activity 在用户点击通知时打开的简单通知。 请注意,该代码将创建TaskStackBuilder对象并使用它来为操作创建PendingIntent。

    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
            .setSmallIcon(R.drawable.notification_icon)
            .setContentTitle("My notification")
            .setContentText("Hello World!");
    // Creates an explicit intent for an Activity in your app
    Intent resultIntent = new Intent(this, ResultActivity.class);
    
    // The stack builder object will contain an artificial back stack for the
    // started Activity.
    // This ensures that navigating backward from the Activity leads out of
    // your application to the Home screen.
    TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
    // Adds the back stack for the Intent (but not the Intent itself)
    stackBuilder.addParentStack(ResultActivity.class);
    // Adds the Intent that starts the Activity to the top of the stack
    stackBuilder.addNextIntent(resultIntent);
    PendingIntent resultPendingIntent =
            stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
    mBuilder.setContentIntent(resultPendingIntent);
    NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    // mId allows you to update the notification later on.
    mNotificationManager.notify(mId, mBuilder.build());
    

    五. 补充

    1. 旋转屏幕时,Activity会被重新创建,bindService的连接会被断开。

    相关文章

      网友评论

        本文标题:Android开发高级进阶——Service与Notificat

        本文链接:https://www.haomeiwen.com/subject/rglmettx.html