Service服务<二>

本文深入探讨了Android服务的权限管理及生命周期,包括如何通过manifest文件声明服务,服务进程的优先级,以及如何处理服务在内存压力下的重启。同时提供了一个本地服务的示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

上篇:service服务<一>

权限

  当服务在manifest的<service>标签里声明时,那么这个服务就可以被全局访问执行。通过这样做,其他的应用程序可以在自己的manifest文件中通过声明相应的<use-permission>来开启,停止或绑定到这个服务。

  自android2.3起,当使用Context.startService(intent)时,你可以通过设置Intent(意图).FLAG_GRANT_READ_URI_PERMISSION 和/或者 Intent.FLAG_GRANT_WRITE_URI_PERMISSION这将会授予服务临时的权限到指定Uri的意图。这个权限将会一直保持,直到服务在开启命令或稍后调用了stopSelf(),或者服务已经完全停止了,权限才会解除。这个工作用于授予访问其他应用程序的权限,并不需要请求一个权限来保护服务,即使这个服务没有暴露个其他应用程序。

  此外,服务可以保护个人进程间通信的调用权限,通过在执行调用实现前调用checkCallingPermission(String)方法。

  关于更多有关权限和安全的信息可以查看Security and Permissions

进程生命周期:

  只要服务一旦开启或者有客户(client)绑定到它,那么安卓系统将会尝试着使进程保持持有这个服务。当应用运行在低内存或者需要杀死现有的进程时,那么持有服务进程的优先级将在下面几种情况下升高:

  • 如果服务当前已经在它的onCreate(),onStartCommand(),onDestroy方法里执行了代码,那么持有这个服务的进程将会成为前台进程,以确保代码能够执行而没有被杀死;
  • 如果服务已经开启,那么持有这个服务的进程将比那些在屏幕上对于用户可见所在的进程要次要,但是比不可见的进程要重要。因为仅有少数进程对于用户是可见的,这意味着服务只有在低内存的情况下才会被杀死。然而,用户并不能直接意识到后台的服务,在这种状态下,服务将被考虑成为一个有效的候选者杀死。特别是,长期运行的服务将增加被杀死的可能性以及如果他们保持足够长的时间,将确保他们能够杀死(或适当的重启)
  • 如果一个客户绑定到服务,那么持有服务的进程与最重要的客户相比并不是次要的。也就是,如果其中的一个客户是可见的,那么这个服务也将被认为是可见的。客户(client)的重要性可以通过BIND_ABOVE_CLIENT,BIND_ALLOW_OOM_MANAGEMENT,BIND_WAIVE_PRIORITY,BIND_IMPORTANT,BIND_ADJUST_WITH_ACTIVITY.来调节影响服务的重要性
  • 一个开启的服务可以通过使用startForeground(int,Notification)API使服务处于前台状态,系统将会认为这是用户意识到的一些事情并且不会将服务作为在低内存下杀死的候选者。(但是理论上服务在极低内存的压力下仍会从当前前台应用程序被杀死,但在实际中并不需要考虑它)

 注意,这意味着你的服务大多数时间是运行的,当在极大内存压力的情况下,系统将会考虑杀死服务。如果这发生的话,系统稍后会尝试着重启服务。这个结果表明,如果你实现onStartCommand()方法异步的执行工作或者在其他的线程中执行,你可以使用START_FLAG_REDELIVERY让系统重新给你发送一个意图,这样如果你的服务在处理某些事情时被杀死将不会丢失。运行在同一进程的其他应用程序将作为服务(如Activity),当然,增加所有进程的重要性,而不是仅仅服务本身。

本地服务示例:

一个使用服务的例子。首先是服务本身,定义一个类:

public class LocalService extends Service {
    private NotificationManager mNM;

    // Unique Identification Number for the Notification.
    // We use it on Notification start, and to cancel it.
    private int NOTIFICATION = R.string.local_service_started;

    /**
     * Class for clients to access.  Because we know this service always
     * runs in the same process as its clients, we don't need to deal with
     * IPC.
     */
    public class LocalBinder extends Binder {
        LocalService getService() {
            return LocalService.this;
        }
    }

    @Override
    public void onCreate() {
        mNM = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);

        // Display a notification about us starting.  We put an icon in the status bar.
        showNotification();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.i("LocalService", "Received start id " + startId + ": " + intent);
        // We want this service to continue running until it is explicitly
        // stopped, so return sticky.
        return START_STICKY;
    }

    @Override
    public void onDestroy() {
        // Cancel the persistent notification.
        mNM.cancel(NOTIFICATION);

        // Tell the user we stopped.
        Toast.makeText(this, R.string.local_service_stopped, Toast.LENGTH_SHORT).show();
    }

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

    // This is the object that receives interactions from clients.  See
    // RemoteService for a more complete example.
    private final IBinder mBinder = new LocalBinder();

    /**
     * Show a notification while this service is running.
     */
    private void showNotification() {
        // In this sample, we'll use the same text for the ticker and the expanded notification
        CharSequence text = getText(R.string.local_service_started);

        // Set the icon, scrolling text and timestamp
        Notification notification = new Notification(R.drawable.stat_sample, text,
                System.currentTimeMillis());

        // The PendingIntent to launch our activity if the user selects this notification
        PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
                new Intent(this, LocalServiceActivities.Controller.class), 0);

        // Set the info for the views that show in the notification panel.
        notification.setLatestEventInfo(this, getText(R.string.local_service_label),
                       text, contentIntent);

        // Send the notification.
        mNM.notify(NOTIFICATION, notification);
    }
}
做完上一步,就可以直接在客户端里编写代码来访问正在运行的服务,如下:

private LocalService mBoundService;

private ServiceConnection mConnection = new ServiceConnection() {
    public void onServiceConnected(ComponentName className, IBinder service) {
        // This is called when the connection with the service has been
        // established, giving us the service object we can use to
        // interact with the service.  Because we have bound to a explicit
        // service that we know is running in our own process, we can
        // cast its IBinder to a concrete class and directly access it.
        mBoundService = ((LocalService.LocalBinder)service).getService();

        // Tell the user about this for our demo.
        Toast.makeText(Binding.this, R.string.local_service_connected,
                Toast.LENGTH_SHORT).show();
    }

    public void onServiceDisconnected(ComponentName className) {
        // This is called when the connection with the service has been
        // unexpectedly disconnected -- that is, its process crashed.
        // Because it is running in our same process, we should never
        // see this happen.
        mBoundService = null;
        Toast.makeText(Binding.this, R.string.local_service_disconnected,
                Toast.LENGTH_SHORT).show();
    }
};

void doBindService() {
    // Establish a connection with the service.  We use an explicit
    // class name because we want a specific service implementation that
    // we know will be running in our own process (and thus won't be
    // supporting component replacement by other applications).
    bindService(new Intent(Binding.this, 
            LocalService.class), mConnection, Context.BIND_AUTO_CREATE);
    mIsBound = true;
}

void doUnbindService() {
    if (mIsBound) {
        // Detach our existing connection.
        unbindService(mConnection);
        mIsBound = false;
    }
}

@Override
protected void onDestroy() {
    super.onDestroy();
    doUnbindService();
}



实验十五 SpringCloud的使用 一、实验目的 1、了解SpringCloud的作用 2、掌握SpringCloud的配置和使用 3、了解SpringCloud的组件使用方法 、实验过程 1、编写程序测试Springcloud服务发现过程。首先创建项目springcloud,然后分别创建3个子模块discovery、producer和comsumer。(以下配置或代码供参考,注意相关依赖版本和JDK版本) a)Springcloud工程的依赖配置 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.9.RELEASE</version> </parent> <groupId>org.example</groupId> <artifactId>SpringCloud</artifactId> <packaging>pom</packaging> <version>1.0-SNAPSHOT</version> <name>SpringCloud</name> <!-- FIXME change it to the project's website --> <url>http://www.example.com</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.7</maven.compiler.source> <maven.compiler.target>1.7</maven.compiler.target> </properties> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Edgware.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <version>2.0.3.RELEASE</version> <scope>test</scope> </dependency> </dependencies> </project> b)配置discovery服务发现中心 1)相关依赖 <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka-server</artifactId> <version>1.3.5.RELEASE</version> </dependency> 2)服务中心启动代码 import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.builder.SpringApplicationBuilder; import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; @EnableEurekaServer @SpringBootApplication public class Application { public static void main(String[] args) { new SpringApplicationBuilder(Application.class).web(true).run(args); } } 3)application.yml server.port=8260 eureka.instance.hostname=localhost eureka.client.register-with-eureka=false eureka.client.fetch-registry=false eureka.client.service-url.defaultZone=http://${eureka.instance.hostname}:${server.port}/eureka c)配置producer服务生产者 1)相关依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency> 2)关键代码 主程序 App.java @EnableDiscoveryClient @SpringBootApplication public class App { public static void main( String[] args ) { SpringApplication.run(App.class, args); } } 服务提供程序 PController.java @RestController public class PController{ protected Logger logger = LoggerFactory.getLogger(HelloEndpoint.class); @Autowired private EurekaInstanceConfig eurekaInstanceConfig; @Value("${server.port}") private int serverPort = 0; @RequestMapping(value = "/hello", method = RequestMethod.GET) public String hello() { return "Hello, Spring Cloud! I am from producer 1. My port is " + String.valueOf(serverPort); } } 3)配置application.yml server.port=2100 spring.application.name=SERVICE-HELLO eureka.client.service-url.defaultZone=http://localhost:8260/eureka d)配置consumer服务消费者 1)相关依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-ribbon</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-feign</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> 2)主要代码 主程序 App.java @EnableDiscoveryClient @SpringBootApplication public class ConsumerApp { @Bean @LoadBalanced RestTemplate restTemplate() { return new RestTemplate(); } public static void main(String[] args) { SpringApplication.run(ConsumerApp.class, args); } } 服务消费程序 CController.java @RestController public class HelloController { @Autowired private RestTemplate restTemplate; @RequestMapping(value = "/chello", method = RequestMethod.GET) public String hello() { return restTemplate.getForEntity("http://SERVICE-HELLO/hello", String.class).getBody(); } } 3)配置文件application.properties server.port=8080 spring.application.name=consumer-hello eureka.client.service-url.defaultZone=http://localhost:8260/eureka 2、参考以上内容进行代码编写和项目配置,分别运行服务发现中心,生产者和消费者程序,最后测试消费者是否能够成功消费生产者提供的服务,并将相关过程的结果截图于此。 3、修改消费者模块,用Feign进行改造,使其能够简化服务调用的请求。 4、提交代码文档至超星,命名为“班级+学号+姓名+实验十五.rar”。
最新发布
06-26
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值