gpt4 book ai didi

android - 当音频插孔连接或忙碌时在 fragment 内切换图像

转载 作者:行者123 更新时间:2023-12-02 22:38:32 24 4
gpt4 key购买 nike

我正在尝试检测耳机是否插入了音频插孔,并基于此尝试更改带有Fragment的布局中的图像。

我试图通过youtube上的幻灯视频来了解Fragment的生命周期,说实话,我听不懂他想解释的一个词。据我了解,当Fragment第一次加载UI元素时,onCreateView方法仅运行一次。

在这里,我一直在基于Audio插孔的状态更改UI元素,这是一种实时的布局动态更新。

现在,我可以切换图像,但是问题是我需要关闭应用程序或进行其他 Activity ,然后返回Fragment查看更改。我知道为什么发生这种情况,onCreateView只运行一次。

有人可以帮我吗?

第二:我在其他片段中也遇到了类似的问题,即使在向数据库添加新条目之后,直到我退出 Activity 并再次回来,人员列表也不会更新。

MeasureFragment.classs

import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.media.AudioManager;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.kevin.sugar.R;

import java.util.logging.Handler;
import java.util.logging.LogRecord;

/**
* A simple {@link Fragment} subclass.
*/
public class MeasureFragment extends Fragment{

Handler handler;

public MeasureFragment() {
// Required empty public constructor
}


public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.measure_layout, container, false);
return view;
}

public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
ImageView img = (ImageView) getActivity().findViewById(R.id.phone);
IntentFilter iFilter = new IntentFilter(Intent.ACTION_HEADSET_PLUG);
Intent iStatus = getActivity().registerReceiver(null, iFilter);
boolean isConnected = iStatus.getIntExtra("state",0) == 1;
if(isConnected){
img.setImageResource(R.drawable.phone_connected);
TextView button = (TextView)getActivity().findViewById(R.id.start_button);
button.setVisibility(View.VISIBLE);

}else{
img.setImageResource(R.drawable.phone_disconnected);
TextView button = (TextView)getActivity().findViewById(R.id.start_button);
button.setVisibility(View.GONE);
}
}

public void onStart(){
super.onStart();
}

}

最佳答案

这是我所做的,并且效果很好。如果您有任何更好的解决方案,请在此处发布。

据我所知,它工作正常。

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.media.AudioManager;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.kuro.bloodpressure.R;

import android.os.Handler;

/**
* A simple {@link Fragment} subclass.
*/
public class MeasureFragment extends Fragment {

ImageView img;
IntentFilter intentFilter = new IntentFilter(Intent.ACTION_HEADSET_PLUG);

public MeasureFragment() {
// Required empty public constructor
}


public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.measure_layout, container, false);
img = (ImageView) view.findViewById(R.id.phone);
return view;
}

public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);

}

public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);

}


public void onStart() {
super.onStart();
}

public void onResume() {
getActivity().registerReceiver(bcReceived, intentFilter);
super.onResume();
}

//
private BroadcastReceiver bcReceived = new BroadcastReceiver() {
public void onReceive(Context ctx, Intent intent) {
// AudioManager audioManager = (AudioManager) ctx.getSystemService(Context.AUDIO_SERVICE);
if (intent.getAction().equals(Intent.ACTION_HEADSET_PLUG)) {
int state = intent.getIntExtra("state", -1);
switch (state) {
case 0:
Log.d("TAG", "NOT CONNECTED");
img.setImageResource(R.drawable.phone_disconnected);
TextView button2 = (TextView) getActivity().findViewById(R.id.start_button);
button2.setVisibility(View.GONE);
break;
case 1:
Log.d("TAG", "CONNECTED");
img.setImageResource(R.drawable.phone_connected);
TextView button1 = (TextView) getActivity().findViewById(R.id.start_button);
button1.setVisibility(View.VISIBLE);
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.d("TAG", "CLICKED");
}
});
break;
default:
Log.d("TAG", "I have no idea what the headset state is");
}
}
}
};

@Override
public void onPause() {
getActivity().unregisterReceiver(bcReceived);
super.onPause();
}


}

关于android - 当音频插孔连接或忙碌时在 fragment 内切换图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36922125/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com