谷歌官方文档已经说了,Android TV要给用户展现的是沉浸式的体验,不希望过多被干扰,所以,普通的Notification是无效的。
思路:一个透明的Activity+自定义Dialog实现类似弹出通知的效果。
先上效果图:

styles.xml

<resources><!--透明主题--><style name="translucent" parent="Theme.AppCompat.Light.NoActionBar"><item name="android:windowBackground">@android:color/transparent</item><item name="android:windowIsTranslucent">true</item><item name="android:windowAnimationStyle">@android:style/Animation.Dialog</item><item name="android:windowNoTitle">true</item><item name="android:windowActionBar">false</item></style>
</resources>

MainActivity.java:

public class MainActivity extends AppCompatActivity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);startService(new Intent(this, NotificationService.class));}
}

NotificationService.java

public class NotificationService extends Service {@Nullable@Overridepublic IBinder onBind(Intent intent) {return null;}@Overridepublic int onStartCommand(Intent intent, int flags, int startId) {// 模拟15秒之后通知到来new Handler().postDelayed(new Runnable() {@Overridepublic void run() {startActivity();}}, 15000);return super.onStartCommand(intent, flags, startId);}/*** 弹出通知*/private void startActivity() {Intent intent = new Intent(this, NotificationActivity.class);intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);startActivity(intent);}
}

NotificationActivity.java

public class NotificationActivity extends Activity {@Overrideprotected void onCreate(@Nullable Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_notification);showNotification();new Handler().postDelayed(new Runnable() {@Overridepublic void run() {if (!NotificationActivity.this.isDestroyed()) {// 延迟10秒关闭弹窗NotificationActivity.this.finish();}}}, 10000);}private void showNotification() {AlertDialog dialog = new AlertDialog.Builder(this).create();Window dialogWindow = dialog.getWindow();if (dialogWindow != null) {dialogWindow.setBackgroundDrawable(new ColorDrawable());}dialog.show();dialog.setContentView(R.layout.layout_dialog);// 设置window typedialog.setCanceledOnTouchOutside(true);if (dialogWindow != null) {WindowManager.LayoutParams lp = dialogWindow.getAttributes();dialogWindow.setGravity(Gravity.CENTER | Gravity.TOP);DisplayMetrics displayMetrics = new DisplayMetrics();getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);lp.width = (int) (displayMetrics.widthPixels * 0.8); // 宽度lp.height = (displayMetrics.heightPixels / 3); // 高度//lp.x = (int) (100 * 0.15); // 新位置X坐标lp.y = (int) (displayMetrics.heightPixels * 0.05); // 新位置Y坐标//lp.alpha = 0.7f; // 透明度dialogWindow.setAttributes(lp);}}
}

shape_dialog.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"android:shape="rectangle"><corners android:radius="3dp"/><solid android:color="#33688698"/>
</shape>

activity_notification.xml:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"/>

layout_dialog.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="horizontal"android:gravity="center_vertical"android:padding="10dp"android:background="@drawable/shape_dialog"><ImageViewandroid:layout_width="@android:dimen/notification_large_icon_width"android:layout_height="@android:dimen/notification_large_icon_height"android:src="@mipmap/ic_launcher"/><TextViewandroid:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginLeft="8dp"android:textColor="@android:color/white"android:text="我是通知的详细内容我是通知的详细内容我是通知的详细内容我是通知的详细内容我是通知的详细内容"/>
</LinearLayout>

清单文件:

<activity android:name=".MainActivity"><intent-filter><action android:name="android.intent.action.MAIN" /><category android:name="android.intent.category.LAUNCHER" /></intent-filter></activity><activity android:name=".NotificationActivity"android:theme="@style/translucent"/><service android:name=".NotificationService"/>

android tv 实现弹出通知相关推荐

  1. android tv二级菜单,android TV开发:弹出菜单实现

    1.在有时候我们需要选择排序方式,在电视开发中往往使用一个弹出菜单实现,Demo效果: 2.核心代码: public class MainActivity extends Activity { pri ...

  2. Android EditText不弹出输入法焦点问题的总结

    同样的代码,碰到有EditText控件的界面时有的机子会弹出输入法,有的机子不会弹出.不好意思,这问题我也一头雾水,谁知道可以告诉我,否则我就把这个问题留下来,以后研究android 源码时再搞个清楚 ...

  3. Android 软键盘弹出时布局内指定内容上移实现及问题解决

    Android 软键盘弹出时布局内指定内容上移实现及问题解决 参考文章: (1)Android 软键盘弹出时布局内指定内容上移实现及问题解决 (2)https://www.cnblogs.com/as ...

  4. Android 软键盘弹出时把布局顶上去,控件乱套解决方法

    Android 软键盘弹出时把布局顶上去,控件乱套解决方法 参考文章: (1)Android 软键盘弹出时把布局顶上去,控件乱套解决方法 (2)https://www.cnblogs.com/zhuj ...

  5. c语言获取安卓弹窗,Android实现信息弹出框

    本文实例为大家分享了Android实现信息弹出框的具体代码,供大家参考,具体内容如下 layout下的dialog_common_layout.xml android:layout_width=&qu ...

  6. Android软键盘弹出引起的各种不适终极解决方案

    参考博客:Android软键盘弹出引起的各种不适终极解决方案 http://blog.csdn.net/itachi85/article/details/6596284 在这里做个笔记 如果不想让软键 ...

  7. Android通知栏Notification弹出横幅显示的解决方法

    Android通知栏Notification弹出横幅显示的解决方法 参考文章: (1)Android通知栏Notification弹出横幅显示的解决方法 (2)https://www.cnblogs. ...

  8. Android 软键盘弹出时把原来布局顶上去的解决方法

    Android 软键盘弹出时把原来布局顶上去的解决方法 参考文章: (1)Android 软键盘弹出时把原来布局顶上去的解决方法 (2)https://www.cnblogs.com/Joanna-Y ...

  9. android的底部弹出框炫酷的样式,Android自定义底部弹出框ButtomDialog

    本文实例为大家分享了Android自定义底部弹出框的具体代码,供大家参考,具体内容如下 先看看效果和你要的是否一样 一 .先来配置自定义控件需要的资源 1.在res文件夹下创建一个anim文件夹并创建 ...

最新文章

  1. R语言数据结构之向量
  2. 阿里云E-HPC赋能制造业仿真云弹性
  3. SQLite 3 一些基本的使用
  4. Navicat 安装+连接
  5. SQL Server 2008使用扩展事件进行高级故障排除
  6. LuceneLucene简介
  7. 矩阵指数 matlab,矩阵指数 - MATLAB Simulink Example - MathWorks 中国
  8. 我等这个含蓄的技术男当上了CEO
  9. JAVA-1007. 素数对猜想 (20)
  10. pip安装tensorflow_Tensorflow源代码编译踩坑若干
  11. 绘图软件Origin新手使用教程
  12. 1000以内完数c语言程序_c语言完数(c语言输出1到1000所有完数)
  13. 大版本号跨越,AIDA64更新6.0版本:更新测试,支持Zen 2架构
  14. android excel在线制作教程,Excel手机表格制作软件
  15. 【Pillow库】图片操作
  16. UE4蓝图API翻译【节点】--- Get All Actors with Tag
  17. win7战网服务器修改,正在更新战网【设置办法】
  18. PHP 零基础入门笔记(1):PHP 基础
  19. 【洛谷P2123】皇后游戏
  20. 点击唤起电话功能和企业微信聊天窗口事件(H5)

热门文章

  1. EXCEL中数字显示为E+18且尾数为0的解决方法
  2. matplotlib的简介
  3. android自动划屏实现,OSC首发:android中的左右滑屏实现By ViewPager
  4. mvp关联activity生命周期_极简SaaS创业手册一文读懂SaaS全生命周期阶段
  5. Centos8 部署Promethus(普罗米修斯)+grafana画图
  6. 自学编程的30岁男人,能按应届生那样找工作吗?
  7. SAP中国客户名单[转载]
  8. UE4 昵称修改后客户端的同步
  9. 汉化版PHP代码审计工具rips
  10. 加权平均资本成本(Weighted Average Cost of Capital,WACC)