美文网首页
autojs自定义通知栏界面

autojs自定义通知栏界面

作者: 牙叔教程 | 来源:发表于2021-09-06 18:29 被阅读0次

牙叔教程 简单易懂

效果展示

效果.gif

环境

Autojs版本: 9.0.5

Android版本: 10.0.0

MIUI版本: 12.5.1

MT管理器版本: 2.9.9

Android Studio 4.1.2

思路

要自定义通知栏界面, 那就需要实例化RemoteViews,

RemoteViews构造函数如下:

public RemoteViews(String packageName, int layoutId) {
  throw new RuntimeException("Stub!");
}

第一个参数是包名, 第二个是layout的id,

包名好说, 一个字符串而已,

重点是layoutId如何获取, 这就是MT管理器的作用了

获取RemoteViews步骤

  1. 用Android Studio设计好layout, 通知栏的界面一般都很简单, 你没见过特别复杂的通知栏吧
  2. 将调试好的app打包成apk
  3. 用autojs打包一个用于调试的apk
  4. 用mt管理器把安卓app里面的layout.xml文件添加到autojs打包的app中
  5. 添加layout中引用的资源到autojs打包的app中
  6. 直接引用资源即可com.yashu.yashu.R$layout.layout_float_window

自定义的通知栏界面

<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android">
    <TextView />
    <TextView />
    <ImageView android:background="@drawable/yashu" />
    <TextView />
    <TextView  />
</LinearLayout>

该界面只用了一个资源, 就是yashu.png, 只要添加到drawable中即可

代码讲解

1. 导入类
importClass(android.app.NotificationManager);
importClass(android.app.Notification);
importClass(android.graphics.BitmapFactory);
importClass(android.content.Context);
importClass(android.app.NotificationChannel);
importClass(android.net.Uri);
importClass(android.widget.RemoteViews);
importClass(java.io.FileInputStream);
importClass(android.graphics.drawable.Icon);
2. 获取icon
function getIcon(icon_path, largeBitmap) {
  largeBitmap = BitmapFactory.decodeStream(new FileInputStream(icon_path));
  return {
    icon: Icon.createWithBitmap(largeBitmap),
    largeBitmap: largeBitmap,
  };
}
3. build
let id = "channel_1";
let description = "143";
let importance = NotificationManager.IMPORTANCE_LOW;
let channel = new NotificationChannel(id, description, importance);
manager.createNotificationChannel(channel);
let remoteViews = new RemoteViews(context.packageName, com.yashu.yashu.R$layout.layout_float_window); //通知栏布局
let notification = new Notification.Builder(context, id)
  .setCategory(Notification.CATEGORY_MESSAGE)
  .setSmallIcon(getIcon(icon_path).icon)
  .setCustomContentView(remoteViews)
  .setAutoCancel(true)
  .build();
4. 设置横幅通知
notification.defaults = Notification.DEFAULT_SOUND;
notification.headsUpContentView = remoteViews;
5. 显示通知
manager.notify(1, notification);

名人名言

思路是最重要的, 其他的百度, bing, stackoverflow, 安卓文档, autojs文档, 最后才是群里问问
--- 牙叔教程

声明

部分内容来自网络
本教程仅用于学习, 禁止用于其他用途

相关文章

  • autojs自定义通知栏界面

    牙叔教程 简单易懂 效果展示 环境 Autojs版本: 9.0.5 Android版本: 10.0.0 MIUI版...

  • RemoteView

    常驻通知栏 Notification RemoteView 自定义通知栏中,需要自定义通知栏的视图时,需要使用Re...

  • Android学习笔记之Notification

    一、需求 自定义通知栏 适配 Android 8.0 按钮点击事件 二、代码 2.1 通知栏的代码 2.2 自定义...

  • 自定义通知栏显示不全

    通过builder.setContent(remoteViews);给通知添加自定义界面,结果由于自定义界面布局过...

  • 2019-08-29 安卓系统音频api学习

    手机音频后台播放可能会被迫中断。多个音源互斥,如何实现。自定义一个通知栏播放通知(可上下一曲,暂停等)锁屏界面显示...

  • ionic3 本地通知

    检测是否有权限 自定义通知栏 点击通知栏回调 github:https://github.com/katzer/c...

  • iOS 导航栏管理器(FJFNavigationBarManag

    一. 前言 我们产品需求中很经常会碰到某个界面是需要隐藏导航栏或者自定义导航栏,但是跳转到下个界面又需要显示导航栏...

  • flutter-仿京东商城04

    一、商品列表页搭建 自定义筛选切换image.png 添加抽屉界面和自定义导航栏 自定义导航栏左侧和右侧按钮和Dr...

  • Swift JPush极光推送通知和自定义消息

    在开始之前,首先了解下自定义消息和通知的几点区别。 自定义消息和通知的区别 收到推送自定义消息时推送通知栏不显示 ...

  • 关于安卓自定义通知栏适配总结

    前言 Android通知栏是一个比较常用的功能,系统自带的通知栏构建起来比较简单,这里主要总结一下自定义通知栏的构...

网友评论

      本文标题:autojs自定义通知栏界面

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