How do I show text in android system status bar

北战南征 提交于 2019-11-28 12:17:30

I do found a solution, the keyword is overlay with a floating window.

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

You can find your answer in the doc, here: https://developer.android.com/reference/android/support/v4/app/NotificationCompat.html

Edit:: Well, the answer is in the doc. However, after a good bit of research and digging, it seems as though the consensus amongst the community is that this is not possible for just any application. Only specific icons can be placed on the right side of the status bar (i.e. Clock, Weather, System info, etc...).

I'm sorry there isn't a more exciting answer, but at least you can stop stressing out about why you can't figure it out.

Edit 2:: Apparently, pre-lollipop devices had access to private apis that allowed you to work with system icons (again, think about the alarm icon). Afterward, the apis were removed. This stackoverflow post goes over the whole situation pretty extensively.

Edit 3:: If you can live with placing you icon on the left side of the status bar you can convert text to bitmap like this:

 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;
 }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!