【问题标题】:How to show notification bar for a short time when the window is full screen by default?窗口默认全屏时如何短时间显示通知栏?
【发布时间】:2017-08-24 13:23:08
【问题描述】:
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //setting fullscreen
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setContentView(R.layout.repository_list);

可以看到窗口默认是全屏的。

在异步任务中,要显示通知

@Override
    protected void onPreExecute() {
        Toast.makeText(this_context,"Downloading",Toast.LENGTH_LONG).show();
        mNotifyManager =
                (NotificationManager) this_context.getSystemService(Context.NOTIFICATION_SERVICE);
        mBuilder = new NotificationCompat.Builder(this_context);
        mBuilder.setContentTitle("Repository")
                .setContentText("Download in progress")
                .setSmallIcon(android.R.drawable.stat_sys_download);
        mBuilder.setProgress(0, 0, false);
        // Displays the progress bar for the first time.
        mNotifyManager.notify(1, mBuilder.build());
        //dialog = ProgressDialog.show(this_context, "Wait", "Downloading...", true);
    }

但是窗口是全屏的,看不到通知。 如何在调用 onPostExecute 方法时看到通知然后再次隐藏通知。 感谢您的宝贵时间。

【问题讨论】:

    标签: java android android-layout android-asynctask android-notifications


    【解决方案1】:

    好的,已经找到答案了。

    已编写此代码。

    关于 preExecute 方法

    @Override
        protected void onPreExecute() {
            Toast.makeText(this_context,"Downloading",Toast.LENGTH_LONG).show();
            ((Activity)this_context).getWindow().addFlags((WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN));
    
            mNotifyManager =
                    (NotificationManager) this_context.getSystemService(Context.NOTIFICATION_SERVICE);
            mBuilder = new NotificationCompat.Builder(this_context);
            mBuilder.setContentTitle("Repository")
                    .setContentText("Download in progress")
                    .setSmallIcon(android.R.drawable.stat_sys_download);
            mBuilder.setProgress(0, 0, false);
            // Displays the progress bar for the first time.
            mNotifyManager.notify(1, mBuilder.build());
            //dialog = ProgressDialog.show(this_context, "Wait", "Downloading...", true);
        }
    

    关于 postExecute 方法

    @Override
        protected void onPostExecute(String s) {
            ((Activity)this_context).getWindow().clearFlags((WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN));
            mBuilder.setContentText("Download complete")
                    .setSmallIcon(android.R.drawable.stat_sys_download_done)
                    // Removes the progress bar
                    .setProgress(0,0,false);
            mNotifyManager.notify(1, mBuilder.build());
            mNotifyManager.cancel(1);
    

    长话短说。

    1.当活动启动时,窗口被设置为标记全屏

    2.asyntask启动时,在preexecute方法上,添加了强制非全屏的标志。现在可以看到按计划下载通知了。

    3.当asynctask完成下载时,强制非全屏的窗口标志被清除。窗口再次自动实现默认标志,即在活动开始时设置的标志全屏。

    感谢您的宝贵时间。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-06-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多