2017-07-28 128 views
3

我正在嘗試開發Android Nougat應用程序,並且我想在從Android服務例程生成的狀態欄中顯示一些信息/文本。所以我的問題是,我不知道如何在狀態欄中顯示文本。我如何顯示Android系統狀態欄中的文本

我添加了一個示例圖像來顯示我的意思是什麼(紅色圓圈)。 我知道這是可能的,因爲我從Play商店的電池監視器應用程序中看到它。

Sample

我已經嘗試過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> 

結果:

Result 1

Result 2

+0

[在狀態欄顯示通知文本 - 安卓]的可能的複製(https://stackoverflow.com/questions/25734370/display-notification-text-in-status-bar-android)希望,這是你在找什麼.. –

+0

我已經閱讀並嘗試過,但這不是我正在尋找的東西。我不需要通知swype,我需要直接在狀態欄中顯示文本。我嘗試使用NotificationCompat.Builder中的setSmallIcon(int圖標),但它需要一個int ressource,所以沒有辦法將文本用於位圖/圖像。 – Matze

回答

2

我找到了一個解決方案,關鍵詞是覆蓋浮動窗口

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); 
0

您可以找到文檔中你的答案,在這裏:https://developer.android.com/reference/android/support/v4/app/NotificationCompat.html

編輯:: 那麼,答案是在doc。然而,在進行了一些研究和挖掘之後,似乎社區的共識是,這對於任何應用都是不可能的。只有特定的圖標可以放置在狀態欄的右側(即時鐘,天氣,系統信息等)。

對不起沒有更令人興奮的答案,但至少你可以停止強調爲什麼你不能弄明白。

編輯2 :: 顯然,預棒棒糖設備可以訪問私人apis,允許您使用系統圖標(再次考慮報警圖標)。之後,apis被移除。這stackoverflow post相當廣泛地覆蓋了整個情況。

編輯3 :: 如果你可以把你上,你可以將文本轉換狀態欄的左側圖標爲位圖這樣的生活:

TextView textView = new TextView(activity.getContext()); 
textView.setText("Hello World"); 
textView.setDrawingCacheEnabled(true); 
textView.destroyDrawingCache(); 
textView.buildDrawingCache(); 
Bitmap bitmap = getTransparentBitmapCopy(textView.getDrawingCache()); 
private Bitmap getTransparentBitmapCopy(Bitmap source) { 
    int width = source.getWidth(); 
    int height = source.getHeight(); 
    Bitmap copy = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); 
    int[] pixels = new int[width * height]; 
    source.getPixels(pixels, 0, width, 0, 0, width, height); 
    copy.setPixels(pixels, 0, width, 0, 0, width, height); 
    return copy; 
} 
+0

你可以請具體一點。我試圖使用NotificationCompat.Builder,但它沒有按需要工作(上面的示例圖片)。 – Matze

+0

請隨時發佈您的代碼和結果圖片。 – anomeric

+0

我編輯了我的帖子,所以你可以看到我的代碼。請告訴我我錯過的文檔的哪一部分。 – Matze