使用服务时如何隐藏工具栏?

时间:2018-12-28 08:18:52

标签: android android-videoview

嘿,我使用VideoView作为Service。但是当我将视频视图全屏显示时,toolbar并没有隐藏。如何在服务中隐藏toolbar

谢谢

2 个答案:

答案 0 :(得分:1)

在您的活动中......

public class MainActivity extends AppCompatActivity{

static MainActivity instance;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    instance=this;
}

 public static MainActivity getInstance(){return instance;}

然后隐藏操作栏的方法.....

public void hideActionBar()
{
   getSupportActionBar.hide();
}

}

现在,在您的“服务”中按如下所示进行呼叫。....

MainActivity main=MainActivity.getInstance();
main.hideActionBar 

我认为这会为您提供帮助,但是请在您服务的正确位置致电给我们。...

答案 1 :(得分:0)

正在关注Vipul Chauhan线程(因为我尚无法评论)

无法在服务内部调用getSupportActionBar。

您可以使用的另一种方法是使用LocalBroadcastManager

您需要从服务中发布,然后在活动中添加需要隐藏工具栏的广播接收器

您的活动:

@Override
public void onCreate(Bundle savedInstanceState) {

  ...

  // Register to receive messages.
  // We are registering an observer (mMessageReceiver) to receive Intents
  // with actions named "custom-event-name".
  LocalBroadcastManager.getInstance(this).registerReceiver(mMessageReceiver,
      new IntentFilter("custom-event-name"));
}

// Our handler for received Intents. This will be called whenever an Intent
// with an action named "custom-event-name" is broadcasted.
private BroadcastReceiver mMessageReceiver = new BroadcastReceiver() {
  @Override
  public void onReceive(Context context, Intent intent) {
    // Get extra data included in the Intent
    String message = intent.getStringExtra("message");
    //hide your toolbar here
  }
};

@Override
protected void onDestroy() {
  // Unregister since the activity is about to be closed.
  LocalBroadcastManager.getInstance(this).unregisterReceiver(mMessageReceiver);
  super.onDestroy();
}

您的服务将需要发送广播

private void sendMessage() {
  Log.d("sender", "Broadcasting message");
  Intent intent = new Intent("custom-event-name");
  // You can also include some extra data.
  intent.putExtra("message", "This is my message!");
  LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
}

此示例摘自此处How to use LocalBroadcastManager?

更多参考信息:https://developer.android.com/reference/android/support/v4/content/LocalBroadcastManager