2017-07-28 12 views
3

Android Nougat用のアプリを開発しようとしています。アンドロイドサービスルーチンから生成されるステータスバーにいくつかの情報/テキストを表示したいと思います。私の問題は、ステータスバーにテキストを表示する方法がわかりません。アンドロイドシステムステータスバーにテキストを表示するには

正確に何を意味するかを示すサンプル画像を追加しました(赤丸)。 プレイストアからバッテリーモニターアプリで見たので、可能です。

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

あなたはここで、docにあなたの答えを見つけることができます:https://developer.android.com/reference/android/support/v4/app/NotificationCompat.html

編集:: まあ、答えはドキュメントです。しかし、研究と掘り出しの良いビットの後、コミュニティの間のコンセンサスは、これがどんなアプリケーションでも不可能であるようです。ステータスバーの右側(時計、天気、システム情報など)に特定のアイコンのみを配置できます。

もっとエキサイティングな答えはありませんが、少なくともあなたはそれを理解できない理由を強調するのを止めることができます。

編集2 :: 明らかに、ロリポップ前のデバイスはプライベートAPIにアクセスでき、システムアイコンで作業することができました(やはり、アラームアイコンについて考える)。その後、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

関連する問題