后台应用服务弹自定义Toast框

2024-09-05 07:58

本文主要是介绍后台应用服务弹自定义Toast框,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

     之前看到自己的手机应用突然会弹出一个提示框感到有点神奇,不知道是怎么做到的,后面才了解到是后台服务弹的。后台服务弹Toast框其实跟Activity界面差不多,下面先来看看项目SimpleJarService结构:

├── AndroidManifest.xml
├── Android.mk
├── libs
│  ├── android-support-v4.jar
│  ├── fw.jar
│  └── simple.jar
├── res
│  ├── drawable
│  │  └── common_toast_bg.xml
│  ├── drawable-hdpi
│  │  └── ic_launcher.png
│  ├── drawable-ldpi
│  ├── drawable-mdpi
│  │  └── ic_launcher.png
│  ├── drawable-xhdpi
│  │  └── ic_launcher.png
│  ├── layout
│  │  └── common_toast_custom.xml
│  ├── values
│  │  ├── colors.xml
│  │  ├── strings.xml
│  │  └── styles.xml
│  ├── values-v11
│  │  └── styles.xml
│  └── values-v14
│      └── styles.xml
└── src
    └── com
        └── china
            └── service
                ├── BootReceiverBroadcast.java
                ├── Logger.java
                ├── SimpleControl.java
                └── SimpleService.java

       SimpleJarService项目在《在Android系统中实现AIDL 自定义对象传递》文章中有介绍,现在主要是在SimpleControl.java类实现实现弹Toast框的方法,而SimpleControl.java类是在SimpleService.java中实例化的。如下:

package com.china.service;import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;import com.china.jar.VoiceChangedListener;
import com.chinatsp.service.R;public class SimpleControl {private static VoiceChangedListener changedListener;private static Context mContext;public SimpleControl(VoiceChangedListener voiceChangedListener, Context context){changedListener = voiceChangedListener;mContext = context;}//获取初始化后的回调实例public static VoiceChangedListener getVoiceCallBack(){return changedListener;}static long lastshowToastTime = 0;public static void showToast(String s) {if(Math.abs(System.currentTimeMillis() - lastshowToastTime)<3000){return; //如果两次要求弹Toast框的时间间隔小于3秒,就不执行第二次弹框}Logger.d("show Toast");View view = LayoutInflater.from(mContext.getApplicationContext()).inflate(R.layout.common_toast_custom,null);WindowManager wm = (WindowManager) mContext.getApplicationContext().getSystemService(Context.WINDOW_SERVICE);int width = wm.getDefaultDisplay().getWidth();LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(width / 2, ViewGroup.LayoutParams.WRAP_CONTENT);TextView txt = (TextView) view.findViewById(R.id.textViewToastMsg);txt.setText(s);txt.setLayoutParams(layoutParams);Toast toast = new Toast(mContext.getApplicationContext());toast.setView(view);toast.setGravity(Gravity.CENTER, 0, 0);toast.setDuration(3000);toast.show();lastshowToastTime = System.currentTimeMillis();}
}
package com.china.service;import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.os.RemoteCallbackList;
import android.os.RemoteException;
import android.os.ServiceManager;import com.china.jar.IVoiceCallBackInterface;
import com.china.jar.IVoiceClientInterface;
import com.china.jar.StudentInfo;
import com.china.jar.VoiceChangedListener;
import com.china.jar.VoiceManager;public class SimpleService extends Service{private static VoiceClientInterfaceImpl mBinder;@Overridepublic IBinder onBind(Intent intent) {Logger.d();return mBinder;//跟客户端绑定}@Overridepublic void onCreate() {super.onCreate();Logger.d();if (null == mBinder){initService();}}@Overridepublic int onStartCommand(Intent intent, int flags, int startId) {Logger.d();if (null == mBinder){initService();}return START_STICKY;}//实现AIDL的接口private class VoiceClientInterfaceImpl extends IVoiceClientInterface.Stub{protected RemoteCallbackList<IVoiceCallBackInterface> mRemoteCallbackList = new RemoteCallbackList<IVoiceCallBackInterface>();private SimpleControl control;public VoiceClientInterfaceImpl(){control = new SimpleControl(voiceChangedListener, SimpleService.this);//实例化SimpleControl对象}@Overridepublic void face() throws RemoteException {Logger.d("face----excute!");//客户端调用face方法时这里会执行,会打印face----excute!}//注册回调@Overridepublic void registerCallBack(IVoiceCallBackInterface arg0)throws RemoteException {Logger.d();mRemoteCallbackList.register(arg0);}//注销回调@Overridepublic void unRegisterCallBack(IVoiceCallBackInterface arg0)throws RemoteException {Logger.d();mRemoteCallbackList.unregister(arg0);}//调用回调方法private VoiceChangedListener voiceChangedListener = new VoiceChangedListener() {@Overridepublic void openAppByVoice(String arg0) {Logger.d("arg0 = " + arg0);int len = mRemoteCallbackList.beginBroadcast();for (int i = 0; i <len; i++) {try {mRemoteCallbackList.getBroadcastItem(i).openAppByVoice(arg0);} catch (RemoteException e) {e.printStackTrace();}}mRemoteCallbackList.finishBroadcast();}};@Overridepublic void registerUser(StudentInfo studentInfo) throws RemoteException {Logger.d("name = " + studentInfo.getName() + " ,age = " + studentInfo.getAge());}}//初始化服务,主要是向系统注册服务private void initService(){Logger.d();if (null == mBinder){synchronized (SimpleService.class) {if (null == mBinder){try {mBinder = new VoiceClientInterfaceImpl();ServiceManager.addService(VoiceManager.NAME, mBinder);} catch (Exception e) {e.printStackTrace();}}}}}}

 可以看到SimpleControl实例化放到了SimpleService类中的内部类VoiceClientInterfaceImpl中实例化,其实也可以放到onCreate()方法中实例化,由于该项目之前是用于aidl的功能实现,所以会放到VoiceClientInterfaceImpl类中实例化。

common_toast_custom.xml是自定义一个布局,用于规范Toast的大小及风格,具体如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:id="@+id/dialogAera"android:layout_width="wrap_content"android:layout_height="wrap_content"android:background="@drawable/common_toast_bg"android:clickable="true"android:gravity="center"android:minHeight="285.0px"android:minWidth="871.0px"android:orientation="horizontal"><TextViewandroid:id="@+id/textViewToastMsg"style="@style/shadowStyle"android:layout_width="wrap_content"android:layout_height="wrap_content"android:gravity="center"android:singleLine="true"android:textColor="@color/common_text_dark"android:textSize="28sp" /></LinearLayout>

      通过发送接收广播的方式来测试showToast(String s)方法的功能。具体实现如下:

package com.china.service;import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;public class BootReceiverBroadcast extends BroadcastReceiver{@Overridepublic void onReceive(Context context, Intent intent) {Logger.d();Intent service = new Intent(context, SimpleService.class);//开机启动会拉起服务SimpleServicecontext.startService(service);if(intent.getAction().equals("android.intent.gunder.SimpleJar")){//android.intent.gunder.SimpleJar属于自定义actionLogger.d(intent.getAction());SimpleControl.getVoiceCallBack().openAppByVoice("nihao");//这里模拟调用penAppByVoice方法SimpleControl.showToast("你好,我是Service里面的Toast!");//这里模拟调用showToast方法}}
}

测试结果如下:

 

 

代码参考:https://github.com/gunder1129/android-tool/tree/master/AIDLdemo/SimpleJarService

 

      

 

 

 

 

这篇关于后台应用服务弹自定义Toast框的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1138380

相关文章

如何自定义Nginx JSON日志格式配置

《如何自定义NginxJSON日志格式配置》Nginx作为最流行的Web服务器之一,其灵活的日志配置能力允许我们根据需求定制日志格式,本文将详细介绍如何配置Nginx以JSON格式记录访问日志,这种... 目录前言为什么选择jsON格式日志?配置步骤详解1. 安装Nginx服务2. 自定义JSON日志格式各

Android自定义Scrollbar的两种实现方式

《Android自定义Scrollbar的两种实现方式》本文介绍两种实现自定义滚动条的方法,分别通过ItemDecoration方案和独立View方案实现滚动条定制化,文章通过代码示例讲解的非常详细,... 目录方案一:ItemDecoration实现(推荐用于RecyclerView)实现原理完整代码实现

基于Spring实现自定义错误信息返回详解

《基于Spring实现自定义错误信息返回详解》这篇文章主要为大家详细介绍了如何基于Spring实现自定义错误信息返回效果,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录背景目标实现产出背景Spring 提供了 @RestConChina编程trollerAdvice 用来实现 HTT

SpringSecurity 认证、注销、权限控制功能(注销、记住密码、自定义登入页)

《SpringSecurity认证、注销、权限控制功能(注销、记住密码、自定义登入页)》SpringSecurity是一个强大的Java框架,用于保护应用程序的安全性,它提供了一套全面的安全解决方案... 目录简介认识Spring Security“认证”(Authentication)“授权” (Auth

SpringBoot自定义注解如何解决公共字段填充问题

《SpringBoot自定义注解如何解决公共字段填充问题》本文介绍了在系统开发中,如何使用AOP切面编程实现公共字段自动填充的功能,从而简化代码,通过自定义注解和切面类,可以统一处理创建时间和修改时间... 目录1.1 问题分析1.2 实现思路1.3 代码开发1.3.1 步骤一1.3.2 步骤二1.3.3

dubbo3 filter(过滤器)如何自定义过滤器

《dubbo3filter(过滤器)如何自定义过滤器》dubbo3filter(过滤器)类似于javaweb中的filter和springmvc中的intercaptor,用于在请求发送前或到达前进... 目录dubbo3 filter(过滤器)简介dubbo 过滤器运行时机自定义 filter第一种 @A

CSS自定义浏览器滚动条样式完整代码

《CSS自定义浏览器滚动条样式完整代码》:本文主要介绍了如何使用CSS自定义浏览器滚动条的样式,包括隐藏滚动条的角落、设置滚动条的基本样式、轨道样式和滑块样式,并提供了完整的CSS代码示例,通过这些技巧,你可以为你的网站添加个性化的滚动条样式,从而提升用户体验,详细内容请阅读本文,希望能对你有所帮助...

Linux使用nohup命令在后台运行脚本

《Linux使用nohup命令在后台运行脚本》在Linux或类Unix系统中,后台运行脚本是一项非常实用的技能,尤其适用于需要长时间运行的任务或服务,本文我们来看看如何使用nohup命令在后台... 目录nohup 命令简介基本用法输出重定向& 符号的作用后台进程的特点注意事项实际应用场景长时间运行的任务服

SpringBoot 自定义消息转换器使用详解

《SpringBoot自定义消息转换器使用详解》本文详细介绍了SpringBoot消息转换器的知识,并通过案例操作演示了如何进行自定义消息转换器的定制开发和使用,感兴趣的朋友一起看看吧... 目录一、前言二、SpringBoot 内容协商介绍2.1 什么是内容协商2.2 内容协商机制深入理解2.2.1 内容

【前端学习】AntV G6-08 深入图形与图形分组、自定义节点、节点动画(下)

【课程链接】 AntV G6:深入图形与图形分组、自定义节点、节点动画(下)_哔哩哔哩_bilibili 本章十吾老师讲解了一个复杂的自定义节点中,应该怎样去计算和绘制图形,如何给一个图形制作不间断的动画,以及在鼠标事件之后产生动画。(有点难,需要好好理解) <!DOCTYPE html><html><head><meta charset="UTF-8"><title>06