【问题标题】:Show status bar temporarily暂时显示状态栏
【发布时间】:2015-04-21 04:39:56
【问题描述】:

我的应用是全屏的,所以我在 style.xml 中使用它:

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="android:windowNoTitle">true</item>
    <item name="android:windowFullscreen">true</item>
</style>

我实现了上传通知。现在如何暂时显示状态栏,就像向下滑动应用时显示它的透明版本一样。

-编辑-

public class UploadService extends IntentService {
private NotificationManager mNotificationManager;

public UploadService() {
    super("UploadService");
}

@Override
public void onDestroy() {
    super.onDestroy();
    mNotificationManager.cancel(0);
}

@Override
protected void onHandleIntent(Intent intent) {
    Resources resources = getResources();
    final Notification uploadNotification = new NotificationCompat.Builder(this)
            .setTicker(resources.getString(R.string.upload))
            .setSmallIcon(android.R.drawable.ic_menu_upload)
            .setContentTitle(resources.getString(R.string.app_name))
            .setContentText(resources.getString(R.string.upload_progress))
            .setProgress(0, 0, true)
            .build();


    final Notification failedNotification = new NotificationCompat.Builder(this)
            .setSmallIcon(android.R.drawable.ic_menu_report_image)
            .setTicker(resources.getString(R.string.upload_failed))
            .setContentTitle(resources.getString(R.string.upload_failed))
            .setAutoCancel(true)
            .build();

    mNotificationManager = (NotificationManager)
            getSystemService(NOTIFICATION_SERVICE);

    mNotificationManager.notify(0, uploadNotification);

    ParseFile parseFile = null;
    boolean success = true;

    Bundle bundle = intent.getBundleExtra("bundleImage");

    String userID = bundle.getString("userID");
    ParseUser parseUser = ParseUser.createWithoutData(ParseUser.class, userID);

    String image = bundle.getString("imagePath");
    Uri imageUri = Uri.parse(image);

    String filename = bundle.getString("imageName");

    ByteArrayOutputStream baos;
    InputStream is;
    try {
        baos = new ByteArrayOutputStream();
        is = getContentResolver().openInputStream(imageUri);
        Bitmap bitmap = BitmapFactory.decodeStream(is);
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);

        parseFile = new ParseFile(filename, baos.toByteArray());
        baos.close();
        is.close();
    } catch (Exception e) {
        success = false;

        mNotificationManager.notify(0, failedNotification);
    }

    if (success) {
        Frames frames = new Frames();
        frames.setUploader(parseUser);
        frames.setFrameFile(parseFile);
        try {
            frames.save();
        } catch (ParseException e) {
            mNotificationManager.notify(0, failedNotification);
        }
    }
}

【问题讨论】:

  • 为了保持 App 的完整性,您可以改用带有重力 Top 的自定义 Toast 来做必要的事情。

标签: android


【解决方案1】:

您可以覆盖 onTouchEvent 并添加/清除 FLAG_FULLSCREEN 标志。

public boolean mIsVisible = false;
...
...
@Override
public boolean onTouchEvent(MotionEvent event)
{
  if ( event.getAction() == MotionEvent.ACTION_DOWN )
  {
    if ( mIsVisible)
      getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
    else
      getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);

    mIsVisible = !mIsVisible;
  }

  return super.onTouchEvent(event);
}

【讨论】:

  • 如果我在我的服务中使用它会怎样?
  • 您的服务将没有指向您的活动的指针。但是您可以在显示/隐藏状态栏时从您的服务向您的活动发送消息。在您的 Activity 类中,您可以在收到来自服务的消息时实现状态栏的隐藏/显示。
  • 如果失败。我只是暂时显示状态栏
  • 你可以这样做。 stackoverflow.com/questions/12997463/… 将消息从您的服务发送到您的活动。
猜你喜欢
  • 1970-01-01
  • 2016-03-29
  • 1970-01-01
  • 1970-01-01
  • 2013-09-02
  • 1970-01-01
  • 1970-01-01
  • 2012-03-30
  • 1970-01-01
相关资源
最近更新 更多