Android启动式服务

Github: https://github.com/MADMAX110/Joke
服务是与活动类似的应用组件,只不过服务没有用户界面。
使用服务可以一直在后台做某些事情,比如下载一个大文件,播放一段音乐或者监听来自服务器的一个消息。
有三种类型的服务:
1、启动式服务
启动式服务可以在后台无限期的运行,即使启动这个服务的活动已经撤销,也不会影响服务的运行。
2、绑定式服务
绑定式服务会绑定到另一个应用组件,如一个活动。
这个活动可以与绑定式服务交互,发送请求并得到结果。只要与之绑定的组件还在运行,绑定式服务就会一直运行下去。这个组件不再与之绑定时服务将会被撤销。
3、调度式服务
调度式服务是安排在某个特定的事件运行的服务。

创建一个启动式服务

要创建一个新工程,其中包含一个名为MainActivity的活动,以及一个名为DelayedMessageService的服务。只要MainActivity调用DelayedMessageService,它会等待10s,然后显示一段文本。
如图创建一个名为Joke的新工程。
在这里插入图片描述

使用IntentService类创建一个基本启动式服务

要创建启动式服务,最简单的方法式扩展IntentService类,因为它提供了你需要的大多数功能。
首先利用一个意图启动这个服务,它会在一个单独的线程中运行指定的代码。
在com.hafd.joke包中创建一个名为DelayedMessageService的Java类。

package com.hafd.joke;

import android.app.IntentService;
import android.content.Intent;
import androidx.annotation.Nullable;

public class DelayedMessageService extends IntentService {
    public DelayedMessageService() {
        super("DelayedMessageService");
    }

    @Override
    protected void onHandleIntent(@Nullable Intent intent) {

    }
}

记录消息日志

在日志中增加消息非常有用,可以利用日志检查你的代码是否能像预期那样工作。
在Java代码中告诉Android要记录什么,应用运行时可以检查Android日志中的输出。
可以使用Android.util.Log类的以下方法记录消息日志:

		Log.v(TAG, "Verbose message"); // 记录详细信息
        Log.d(TAG, "Debug message"); // 记录调试信息
        Log.i(TAG, "Info message"); // 记录普通信息
        Log.w(TAG, "Warning message"); // 记录警告信息
        Log.e(TAG, "Error message"); // 记录错误信息

完整的DelayedMessageSeervice代码

package com.hafd.joke;

import android.app.IntentService;
import android.content.Intent;
import android.util.Log;

import androidx.annotation.Nullable;

public class DelayedMessageService extends IntentService {

    public static final String EXTRA_MESSAGE = "message";

    public DelayedMessageService() {
        super("DelayedMessageService");
    }

    @Override
    protected void onHandleIntent(@Nullable Intent intent) {
        synchronized (this) {
            try {
                wait(10000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        String text = intent.getStringExtra(EXTRA_MESSAGE);
        showText(text);
    }

    private void showText(final String text){
        Log.v("DelayedMessageService", "The message is: " + text);
    }
}

在AndroidManifest.xml中声明服务

其中exported表示是否可以在其他应用中使用。

<?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:dataExtractionRules="@xml/data_extraction_rules"
        android:fullBackupContent="@xml/backup_rules"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.Joke"
        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>
        </activity>
        <service android:name=".DelayedMessageService"
            android:exported="false">
        </service>
    </application>

</manifest>

为activity_main.xml增加一个按钮

增加两个字符串值

    <string name="question">What is the secret of comedy?</string>
    <string name="response">Timing!</string>

接下来更新activity_main.xml,让其显示一个按钮:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="16dp"
    tools:context=".MainActivity">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:text="@string/question"
        android:id="@+id/button"
        android:onClick="onCLick"
        />

</LinearLayout>

MainActivity使用startService启动服务

只要用户单击按钮,就要使用MainActivity的onClick方法启动DelayedeMessageService。从活动启动另一个服务与启动一个活动很类似。先要创建一个显示意图,指定你想要启动的服务。然后使用活动中的startService方法启动这个服务。

package com.hafd.joke;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
    
    public void onClick(View view){
        Intent intent = new Intent(this, DelayedMessageService.class);
        intent.putExtra(DelayedMessageService.EXTRA_MESSAGE, getResources().getString(R.string.response));
        startService(intent);
    }
}

试一试应用

在这里插入图片描述
按下按钮等待10s后切换回AS的logcat页面,会发现如下打印:

2023-10-09 09:42:46.602  8237-8306  DelayedMessageService   com.hafd.joke                        V  The message is: Timing!

启动式服务生命周期

在Android中,启动式服务的生命周期可以包括以下四个步骤:
onCreate(): 当服务第一次被创建时执行。这个方法可以在服务创建时进行一些初始化操作。
onStartCommand(): 当服务被启动的时候执行。使用startService()这个方法开启服务,但注意,如果服务已经启动过,那么onCreate()方法不会再被调用。此时服务开始执行,可以长期在后台运行。
onHandleIntent():要把希望服务在后台运行的所有代码增加到onHandleIntent()。
onDestroy(): 当服务不再被使用或被销毁时执行。
一般来说,启动式服务的生命周期就是从onCreate()到onDestroy()。不过需要注意的是,如果启动式服务没有被明确停止,那么它可能会在后台持续运行,即使用户退出了应用或设备重新启动,除非服务被系统销毁或明确停止。

Android内置通知服务

我们要修改Joke应用,在一个通知中显示消息。通知时在应用用户界面之外显示消息。发出一个通知时,它会在状态栏的通知区中显示一个图标。可以在通知抽屉中看到这个通知的详细信息,从屏幕上向下滑动就可以看到通知抽屉:

创建一个通知生成器

首先要做的是创建一个通知生成器。它允许你构建有特定内容和特性的通知。
你创建的每个通知都应当包括一个小图标、一个标题和最简版的一些文本。下面是相应的代码:

NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
                .setSmallIcon(android.R.drawable.sym_def_app_icon) //设置显示小图标
                .setContentTitle(getString(R.string.question)) //设置标题
                .setContentText(text) //设置文本
                .setPriority(NotificationCompat.PRIORITY_HIGH) //设置高优先级
                .setVibrate(new long[] {0, 1000}) //等待0ms,并震动1000ms
                .setAutoCancel(true);//用户单击通知时通知会消失

增加一个动作告诉通知单击时要启动哪个活动

        //启动MainActivity的普通意图
        Intent actionIintent = new Intent(this, MainActivity.class);
        
        PendingIntent actionPendingIntent = PendingIntent.getActivity(
                this, //上下文,这里是当前服务
                0,//不需要访问挂起意图时,可以将其设置为0
                actionIintent,//这是上面创建的意图
                PendingIntent.FLAG_UPDATE_CURRENT);//如果有一个匹配的挂起意图,它会更新
        //这会把这个挂起意图增加到通知
        builder.setContentIntent(actionPendingIntent);

使用内置通知服务发出通知

        //允许你访问Android的通知服务
        NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        //使用通知服务来显示我们创建的通知
        notificationManager.notify(NOTIFICATION_ID, builder.build());

完整的DelayedMessageService.java

package com.hafd.joke;

import android.app.IntentService;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.util.Log;

import androidx.annotation.Nullable;
import androidx.core.app.NotificationCompat;

public class DelayedMessageService extends IntentService {

    public static final String EXTRA_MESSAGE = "message";
    public static final int NOTIFICATION_ID = 5453;
    public DelayedMessageService() {
        super("DelayedMessageService");
    }

    @Override
    protected void onHandleIntent(@Nullable Intent intent) {
        synchronized (this) {
            try {
                wait(10000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        String text = intent.getStringExtra(EXTRA_MESSAGE);
        showText(text);
    }

    private void showText(final String text){
        NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
                .setSmallIcon(android.R.drawable.sym_def_app_icon) //设置显示小图标
                .setContentTitle(getString(R.string.question)) //设置标题
                .setContentText(text) //设置文本
                .setPriority(NotificationCompat.PRIORITY_HIGH) //设置高优先级
                .setVibrate(new long[] {0, 1000}) //等待0ms,并震动1000ms
                .setAutoCancel(true);//用户单击通知时通知会消失

        //启动MainActivity的普通意图
        Intent actionIintent = new Intent(this, MainActivity.class);
        PendingIntent actionPendingIntent = PendingIntent.getActivity(
                this, //上下文,这里是当前服务
                0,//不需要访问挂起意图时,可以将其设置为0
                actionIintent,//这是上面创建的意图
                PendingIntent.FLAG_UPDATE_CURRENT);//如果有一个匹配的挂起意图,它会更新
        //这会把这个挂起意图增加到通知
        builder.setContentIntent(actionPendingIntent);

        //允许你访问Android的通知服务
        NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        //使用通知服务来显示我们创建的通知
        notificationManager.notify(NOTIFICATION_ID, builder.build());
    }
}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值