我正在嘗試開發Android Nougat應用程序,並且我想在從Android服務例程生成的狀態欄中顯示一些信息/文本。所以我的問題是,我不知道如何在狀態欄中顯示文本。我如何顯示Android系統狀態欄中的文本
我添加了一個示例圖像來顯示我的意思是什麼(紅色圓圈)。 我知道這是可能的,因爲我從Play商店的電池監視器應用程序中看到它。
我已經嘗試過NotificationCombat.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);
}
}
activity_main.xml中
<?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>
結果:
[在狀態欄顯示通知文本 - 安卓]的可能的複製(https://stackoverflow.com/questions/25734370/display-notification-text-in-status-bar-android)希望,這是你在找什麼.. –
我已經閱讀並嘗試過,但這不是我正在尋找的東西。我不需要通知swype,我需要直接在狀態欄中顯示文本。我嘗試使用NotificationCompat.Builder中的setSmallIcon(int圖標),但它需要一個int ressource,所以沒有辦法將文本用於位圖/圖像。 – Matze