如何在android系统状态栏中显示文本

2024-05-01

我正在尝试为 Android Nougat 开发一个应用程序,我想在从 android 服务例程生成的状态栏中显示一些信息/文本。所以我的问题是,我不知道如何在状态栏中显示文本。

我添加了一个示例图像来显示我的确切含义(红色圆圈)。 我知道这是可能的,因为我在 Play 商店的电池监视器应用程序中看到了它。

我已经尝试过NotificationCompat.Builder,但我认为这不是正确的方法。也许是覆盖,但搜索后我没有找到任何东西。

有人可以告诉我如何做或给我提示吗?

编辑:这是我的NotificationCompat.Builder的测试代码

MainActivity.java

import android.app.Notification;
import android.app.NotificationManager;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;

public class MainActivity extends AppCompatActivity
{
    private final int NOTIFICATION_ID = 10;

    @Override
    protected void onCreate (Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this);
        mBuilder.setContentTitle("Value");
        mBuilder.setContentText("123");
        mBuilder.setSmallIcon(R.mipmap.ic_launcher);
        mBuilder.setOngoing(true);
        mBuilder.setAutoCancel(false);

        //Intent resultIntent = new Intent(this, MainActivity.class);
        //PendingIntent resultPendingIntent = PendingIntent.getActivity(this, 0, resultIntent, PendingIntent.FLAG_UPDATE_CURRENT);
        //mBuilder.setContentIntent(resultPendingIntent);

        Notification notification = mBuilder.build();
        notification.flags |= Notification.FLAG_NO_CLEAR | Notification.FLAG_ONGOING_EVENT;

        NotificationManager mNotifyMgr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        mNotifyMgr.notify(NOTIFICATION_ID, notification);
    }
}

活动主文件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context=".MainActivity">

    <android.support.design.widget.CoordinatorLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <android.support.design.widget.AppBarLayout android:layout_height="wrap_content"
                                                    android:layout_width="match_parent"
                                                    android:theme="@style/AppTheme.AppBarOverlay">

            <android.support.v7.widget.Toolbar android:id="@+id/toolbar"
                                               android:layout_width="match_parent"
                                               android:layout_height="wrap_content"
                                               android:background="#000000"
                                               app:popupTheme="@style/AppTheme.PopupOverlay" />

        </android.support.design.widget.AppBarLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:orientation="vertical"
            app:layout_behavior="@string/appbar_scrolling_view_behavior"
            android:weightSum="100" >

            <TextView
                android:id="@+id/tv_value"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Hello World!"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintRight_toRightOf="parent"
                app:layout_constraintTop_toTopOf="parent"/>

        </LinearLayout>

    </android.support.design.widget.CoordinatorLayout>

</RelativeLayout>

Result:


我确实找到了解决方案,关键字是overlay with a 浮动窗口.

int statusBarHeight = 0;
int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
if (resourceId > 0) statusBarHeight = getResources().getDimensionPixelSize(resourceId);

final WindowManager.LayoutParams parameters = new WindowManager.LayoutParams(
        WindowManager.LayoutParams.WRAP_CONTENT,
        statusBarHeight,
        WindowManager.LayoutParams.TYPE_SYSTEM_ERROR,   // Allows the view to be on top of the StatusBar
        WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN,    // Keeps the button presses from going to the background window and Draws over status bar
        PixelFormat.TRANSLUCENT);
parameters.gravity = Gravity.TOP | Gravity.CENTER;

LinearLayout ll = new LinearLayout(this);
ll.setBackgroundColor(Color.TRANSPARENT);
LinearLayout.LayoutParams layoutParameteres = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.MATCH_PARENT);
ll.setLayoutParams(layoutParameteres);

TextView tv = new TextView(this);
ViewGroup.LayoutParams tvParameters = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT);
tv.setLayoutParams(tvParameters);
tv.setTextColor(Color.WHITE);
tv.setGravity(Gravity.CENTER);
tv.setText("123");
ll.addView(tv);

WindowManager windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
windowManager.addView(ll, parameters);
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何在android系统状态栏中显示文本 的相关文章

  • Android 应用程序因 Firebase 电话身份验证而崩溃

    我正在使用 firebase 进行电话号码身份验证 当我使用我的电话号码时 它会自动验证它 但是当我使用另一个电话号码时 我得到类转换异常 它说 getGoogleApiForMethod 返回 Gms stackoverflow上也有类似
  • Android:如何在多语言资源中重用字符串

    自从我的第一个 Android 项目以来 这个问题一直困扰着我 考虑以 en 作为默认值的多语言字符串资源 res values strings xml lt The default language en res values de st
  • Android Wear:在手持设备上启动服务

    我正在构建一个 Wear 应用程序 它将与手持设备上的 WearableListenerService 进行通信 但是 我想确保当应用程序在手表上启动时服务已启动并运行 我最初的想法是发送意图或广播消息来启动服务 但是 我一直无法弄清楚如何
  • Robolectric 1.2:“警告:无法找到 Android SDK 的路径”

    I used Robolectric 1 1 jar 与依赖项 在我的项目中并成功使其工作 但是当我将罐子更改为 1 2 SNAPSHOT jar 与依赖项 我收到以下警告 警告 无法找到 Android SDK 的路径 两个jar包都下载
  • Android EditText默认数字键盘和允许文本[重复]

    这个问题在这里已经有答案了 可能的重复 EditText 默认带有数字键盘 但允许字母字符 https stackoverflow com questions 3544214 edittext with number keypad by d
  • 如何实现可运行队列

    我正在尝试实现一个可运行队列 在异步任务期间依次执行 意味着队列中的下一个将在另一个完成后执行 我编写了一个管理器来管理这些可运行对象和本身就是可运行对象的任务 然后 我获取异步任务中的第一个任务并运行它 希望它能够在队列中运行 但是它最终
  • 在 Android 的 Recycler View 中的文本视图背景上生成并设置随机颜色

    I am Trying to Generate Random Colors and set the Random color as background of Text View Just Like in GMail app The Tex
  • Android 缩放和密度问题

    Update 一些研究表明 华硕 Transformer TF700T 高端 的像素密度应约为 224 因此 android 报告的 159 值要么是错误的 要么已被某种方式修改 system 中的 lcd 密度键 build prop 由
  • GpsStatusListener:尽管状态为 GpsStatus.GPS_EVENT_FIRST_FIX,但修复中未使用卫星

    我向我的位置管理器添加了一个 GPS 状态侦听器 以便查看何时获得第一个修复 当我收到 GPS EVENT FIRST FIX 时 我会循环遍历所有卫星 但为什么修复中没有使用它们 usedInFix 我的日志对所有卫星都显示 错误 fin
  • Android 原生 AAssetManager 的文件层次结构

    Issue 我想知道如何从本机代码创建 Android 中资产文件夹的文件层次结构 我在用着AAssetManager openDir but AAssetDir getNextFileName不返回任何目录名称 因此基本上我无法深入了解层
  • 即时应用程序上的文本转语音崩溃

    我正在实现一个即时应用程序 该应用程序利用 Android 设备上提供的文本转语音功能 我已经设法得到了TextToSpeech按照 Android 开发博客文章中详细说明的说明 实例已初始化并可在非即时应用程序中正常工作Android 中
  • 设置 ViewGroup 的最大宽度

    如何设置 ViewGroup 的最大宽度 我正在使用一个Theme Dialog然而 当调整大小到更大的屏幕时 它看起来不太好 它也有点轻量级 我不希望它占据整个屏幕 I tried 这个建议 https stackoverflow com
  • 以编程方式卸载 Android 应用程序

    我希望能够允许我的用户从我的应用程序中卸载应用程序 就像 Google Play 商店允许其用户一样 请下图 主要问题是如何定义一个按钮 按下它我们可以通过提供包名称或其他一些信息来卸载应用程序 就像图像上的卸载按钮一样 try Inten
  • Android 中 Activity 的服务回调

    我有我的 GPSTracker 的摘要 它返回用户的位置 其作品 public class GPSTracker extends Service implements LocationListener public GPSTracker C
  • 为什么我的字体大小在 android webview 对象中看起来比在 android 浏览器中查看时大得多?

    我正在尝试制作一个小型 Android 应用程序 它除了在 webview 对象而不是浏览中显示网站之外什么也不做 到目前为止它加载了目标网页 但文本和图像大小都比查看页面时大得多在实际设备浏览器中 在浏览器中 页面看起来正确 但在我的应用
  • 尝试在后台使用 AsyncTask 解析 JSON 时强制关闭

    我是 Android 开发新手 正在研究 json 数据 我设法让解析工作 我想显示一个 ProgressDialog 我读到我需要使用 AsyncTask 但由于某种原因 一旦我将相同的工作代码放入 doInBackground 中 即使
  • 不幸的是应用程序已在 Android 模拟器中停止

    我是 Android 新手 正在尝试一些小应用程序 例如 Compass 当我在模拟器中运行应用程序时 它会给出消息Unfortunately Compass has Stopped 我没有编译时错误 我该如何解决这个问题 是什么原因造成的
  • 我收到“循环依赖”Android Dagger Hilt 错误

    我从头开始检查了所有内容 但找不到错误 我找不到错误 可能是什么 我收到以下编译错误 HomeViewModel java 6 error ComponentProcessor MiscError dagger internal codeg
  • Visual Studio代码无法检测到模拟器设备或连接的电话

    I was running my app with vscode using Android emulator or my phone however all of a sudden vscode could not identify an
  • PinnedHeaderListView 滚动和标题问题

    背景 我正在尝试模仿 Lollipop 的联系人应用程序显示联系人首字母的固定标题的方式 正如我所写的here https stackoverflow com q 27621425 878126 问题 由于原始代码 发现here http

随机推荐