• android创建无界面的服务运行在后台


    android-创建无界面的服务运行在后台

    RT, 目标是新建一个服务运行在后台,但不能有界面

    具体步骤如下:

    1. 新建一个无界面的Activity

    新建一个无界面的Activity, 但是不给它 setContentView(),也就是不给他设置布局文件,注意新建的Activity必须继承自:Activity, 而不是AppCompatActivity, 就是android.app包下的那个类, 并在onCreate时自动启动服务:

    package com.realsil.test.server;
    
    import android.app.Activity;
    import android.content.Intent;
    import android.os.Bundle;
    import android.util.Log;
    
    public class MainActivity extends Activity {
    
        public static final String TAG = "[server]";
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
    
            Log.e(TAG, "准备启动服务...");
            startService(new Intent(getApplicationContext(), MyService.class));
        }
    }
    

    2. 修改当前应用的主题

    修改清单文件,修改当前应用的主题:

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools">
    
        <application
            android:allowBackup="true"
            android:icon="@mipmap/ic_launcher"
            android:label="@string/app_name"
            android:roundIcon="@mipmap/ic_launcher_round"
            android:supportsRtl="true"
            android:theme="@android:style/Theme.NoDisplay"
            tools:targetApi="31">
    
            <activity
                android:name=".MainActivity"
                android:exported="true">
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
    
                <meta-data
                    android:name="android.app.lib_name"
                    android:value="" />
            </activity>
    
            <service
                android:name=".MyService"
                android:enabled="true"
                android:exported="true"
                android:label="远程计算服务"
                android:process="com.yongdaimi.android.remote.service">
    
                <intent-filter>
                    <action android:name="com.yongdaimi.android.test.service" />
                </intent-filter>
    
            </service>
    
        </application>
    
    </manifest>
    

    主要就是android:theme="@android:style/Theme.NoDisplay" 这一行,这样会让Activity达到无界面的效果。

    再次运行程序,就会发现服务已启动了,但是没有界面。

    3. 扩展

    如果想去除后台任务显示

    就是说不想在phone的多任务管理界面看到这个程序,这样在点击近期任务功能键后就看不到它啦!用户也无法用过任务菜单中的删除任务来删除应用。如果要实现这样的效果只需要在清单文件中设置上述Activity的一个属性:android:excludeFromRecents="true"就可以了。

    不想在手机上有启动图标

    不想有启动图标的话只需要在清单文件中把Activity的意图过滤器中的<category android:name="android.intent.category.LAUNCHER" />注释的掉就好了。但是如果没有启动图标的话我们只能通过注册系统的静态广播来启动,这个要特别注意。

    参考链接

    1. 制作无界面安卓应用
    2. Android 创建单独的服务运行在后台(无界面
  • 相关阅读:
    7.19 repeater的用法:
    7.18 内置对象
    7.17 三级联动 控件及JS简单使用
    7.15 简单控件 以及 复合控件
    7.14 Repeater
    7.14 ASP.NET介绍 基础
    phpcms图文总结(转载)
    phpcms图文总结(转)
    商业php软件的应用
    php前段时间复习
  • 原文地址:https://www.cnblogs.com/yongdaimi/p/16865898.html
Copyright © 2020-2023  润新知