我的Android应用程序正在使用该AndroidAsync库连接到客户端的Node.js服务器Socket.IO.套接字客户端是通过服务建立的.
我目前正在启动Socket.IO服务onResume并在应用程序中停止onPause每个服务Activity.
这看起来效率很低,因为我每次按下主页按钮或切换到应用程序中的其他活动时,基本上都会停止套接字并重新创建一个新套接字.
什么是处理保持套接字打开的上述要求的最佳方法?
public class SocketIOService extends Service {
private Preferences prefs;
private SocketIOClient socket;
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
try {
ConnectCallback callback = new ConnectCallback() {
@Override
public void onConnectCompleted(Exception ex, SocketIOClient client) {
if (ex != null) {
ex.printStackTrace();
return;
}
Log.v("SOCKET IO CONNECTION", "ESTABLISHED");
client.setDisconnectCallback(new DisconnectCallback() {
@Override
public void onDisconnect(Exception e) { …Run Code Online (Sandbox Code Playgroud) I have this really weird bug where some of my code is not being called, but doesn't look there is any reason why it wouldn't be called.
onFinishedRecording gets called and the tag "Finished recording." gets logged out, but the code after that does not get called at all. All the code stops being called when mMediaRecorder.stop(); is called. It also does not go into the catch block either. Why is this happening?
I thought it might have to do …
我有一个Service我试图绑定到我的主要Activity,但我收到了
java.lang.ClassCastException:android.os.BinderProxy无法强制转换为com.walintukai.rubix.ConnectionService $ LocalBinder.
我已在我的清单中声明了该服务.为什么会这样?
宣言宣言
<service android:name=".ConnectionService" />
Run Code Online (Sandbox Code Playgroud)
服务(简化代码)
public class ConnectionService extends Service {
static final String TAG = ConnectionService.class.getName();
private BluetoothAdapter mBtAdapter = null;
public BluetoothGatt mBluetoothGatt = null;
private ConnectionServiceEventListener mIRedBearServiceEventListener;
HashMap<String, BluetoothDevice> mDevices = null;
private BluetoothGattCharacteristic txCharc = null;
private final IBinder mBinder = new LocalBinder();
@Override
public IBinder onBind(Intent intent) {
return mBinder;
}
public class LocalBinder extends Binder {
public ConnectionService getService() {
return ConnectionService.this;
}
} …Run Code Online (Sandbox Code Playgroud) android classcastexception android-service android-service-binding
我试图将信息从a传递DialogFragment到它的托管Fragment,但问题是我使用的是android.support.v4.app.Fragment代替android.app.Fragment我的常规包Fragments.
这段代码给了我一个错误:
messageResponseDialog.setTargetFragment(this, 0);
Run Code Online (Sandbox Code Playgroud)
setTargetFragment期待a Fragment而不是a support.v4.Fragment.
如何使用android.support.v4.app.Fragment实现从DialogFragment到托管片段的回调?
我在我的ViewController中添加了一个UITapGestureRecognizer用于Content View在点击内容视图时关闭键盘的一个.
问题是我有一个UICollectionView内部我的内容视图,并设置UITapGestureRecognizer拦截我的水龙头UICollectionView.
如何让我UICollectionView的水龙头通过,以便该didSelectItemAtIndexPath方法再次点火?
func setupGestureRecognizer() {
let dismissKeyboardTap = UITapGestureRecognizer(target: self, action: "dismissKeyboard")
contentView.addGestureRecognizer(dismissKeyboardTap)
}
func dismissKeyboard() {
contentView.endEditing(true)
}
Run Code Online (Sandbox Code Playgroud) MediaRecorder当我按住按钮时,我想开始录制语音消息.IllegalStateException当我试图开始录制时,我正在尝试onLongClickListener.
我在堆栈跟踪中记录的错误是在第219行: recorder.start()
为什么会这样?
btnSendVoice.setOnLongClickListener(new OnLongClickListener() {
@Override
public boolean onLongClick(View view) {
isVoiceButtonHeld = true;
startRecording();
return false;
}
});
btnSendVoice.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent event) {
view.onTouchEvent(event);
if (event.getAction() == MotionEvent.ACTION_UP) {
if (isVoiceButtonHeld) {
isVoiceButtonHeld = false;
stopRecording();
}
}
return false;
}
});
private void startRecording() {
Toast.makeText(getActivity(), "Recording Message", Toast.LENGTH_SHORT).show();
filename = Environment.getExternalStorageDirectory().getAbsolutePath() + "/audiotest.3gp";
recorder = new MediaRecorder();
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
recorder.setOutputFile(filename);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
try { recorder.prepare(); …Run Code Online (Sandbox Code Playgroud) android illegalstateexception mediarecorder android-mediarecorder
我JodaTime用来弄清楚当前LocalDate是什么,然后得到下周一的日期.
当我使用以下方法并且当前日期是星期一时,而不是获得下一个星期一,它将获得当天.
private LocalDate getNextMonday() {
LocalDate date = LocalDate.now();
date = date.plusWeeks(1);
return date.withDayOfWeek(DateTimeConstants.MONDAY);
}
Run Code Online (Sandbox Code Playgroud)
为什么我的方法在下周一当前是星期一时没有工作?
我只是Robolectric通过这个设置设置测试,当我测试我的应用程序时它给了我一个错误.
我把问题缩小到了ProgressSpinner我的习惯中ActionBar layout.当我禁用自定义ActionBar时,测试运行正常.我正在使用常规操作栏,而不是ActionBarSherlock.
这有解决方法吗?
测试代码
@RunWith(RobolectricTestRunner.class)
public class MainActivityTest {
MainActivity activity;
@Before
public void setup()
{
this.activity = Robolectric.buildActivity(MainActivity.class).create().get();
}
@Test
public void shouldHaveHappySmiles() throws Exception
{
String hello = this.activity.getString(R.string.app_name);
assertThat(hello, equalTo("LFDate"));
}
Run Code Online (Sandbox Code Playgroud)
}
失败追踪
android.view.InflateException: XML file C:\Users\Deric\Documents\GitHub\lfdate\android\res\layout\actionbar.xml line #-1 (sorry, not yet implemented): Error inflating class android.widget.ProgressBar
at android.view.LayoutInflater.createView(LayoutInflater.java:620)
at org.robolectric.shadows.RoboLayoutInflater.onCreateView(RoboLayoutInflater.java:38)
at android.view.LayoutInflater.onCreateView(LayoutInflater.java:669)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:694)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:758)
at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
at com.android.internal.app.ActionBarImpl.setCustomView(ActionBarImpl.java:312)
at …Run Code Online (Sandbox Code Playgroud) 我想知道当用户Activity从中返回时,正确的顺序是什么Home Screen.
将Activity's onResume()首先执行,然后在Fragment's onResume()后执行?它是按顺序执行还是同时执行?
我在Fragment的onResume()中添加了一些代码,但它似乎没有在Activity的onResume()之后触发.
android activity-lifecycle android-fragments android-activity fragment-lifecycle
我正在java.lang.IndexOutOfBoundsException: Inconsistency detected使用RecyclerView,但是只有在我似乎将其扔掉时才使用。当我滚动得足够慢时,它不会崩溃。
我将基本适配器用于许多其他子类,但是只有某些子类会崩溃。知道为什么会这样吗?
编辑
我发现发生这种情况的原因是因为getItemCount()更改取决于页脚加载视图的状态。我相信这会导致更改和滚动时出现不一致。
我通过始终在getItemCount()中的页眉/页脚加载视图返回2个额外的项来进行测试。而不是根据其状态动态计算计数。解决此问题的好方法是什么?
我的适配器
public abstract class BaseRecyclerAdapter<T>
extends RecyclerView.Adapter<RecyclerView.ViewHolder>
implements LoadingAndErrorViewHolder.LoadingViewHolderListener {
public static final int STATUS_HIDDEN = 0;
public static final int STATUS_LOADING = 1;
public static final int STATUS_ERROR = 2;
protected static final int TYPE_HEADER_LOADING = 0;
protected static final int TYPE_FOOTER_LOADING = 1;
protected static final int TYPE_NORMAL = 2;
private static final int POSITION_TYPE_HEADER_LOADING = 0;
private BaseRecyclerAdapterListener mBaseRecyclerAdapterListener;
private final List<T> mBackingList = …Run Code Online (Sandbox Code Playgroud) android indexoutofboundsexception recycler-adapter android-recyclerview