我发现这个问题非常有趣,有些可能没有。让我们希望得到一个可靠的答案。
我有一个片段,它绑定onResume()
中的服务,以便在用户出现时每60秒自动刷新一次数据。我的bind()
方法在这里......
public void bind() {
if(connected || MainActivity.sRefresherBinding) return;
MainActivity.sRefresherBinding = true;
Log.v(LOG_TAG, "binding...");
mProgressDialog.setTitle("Service Connected!");
mProgressDialog.setMessage("We're almost there!");
Intent bindingIntent = new Intent(getActivity(), Refresh.class);
getActivity().bindService(bindingIntent, mConnection, Context.BIND_AUTO_CREATE);
getActivity().startService(bindingIntent);
}
unbind()
...
onStop()
方法
public void unbind() {
Log.v(LOG_TAG, "unbinding");
if (connected) {
mRefreshService.terminate();
getActivity().unbindService(mConnection);
getActivity().stopService(new Intent(getActivity(), Refresh.class));
connected = false;
}
}
onServiceConnected()
这里......
public void onServiceConnected(ComponentName name, IBinder service) {
if(binder != null) binder = null;
binder = (Refresh.LocalBinder) service;
if(mRefreshService != null) mRefreshService.terminate();
mRefreshService = binder.getService();
connected = true;
mProgressDialog.setTitle("Refreshing your data...");
mProgressDialog.setMessage("We are almost there!");
Log.v(LOG_TAG, "Service Connected");
}
我的onStartCommand()
关于刷新扩展服务
public int onStartCommand(Intent intent, int flags, int startId) {
start(new UpdateListener() {
@Override
public void onUpdate(ArrayList<Quote.SingleQuote> newData) {
Intent broadcastIntent = new Intent();
broadcastIntent.setAction(Utility.BROADCAST);
broadcastIntent.putExtra(Utility.QUOTE_INTENT, newData);
sendBroadcast(broadcastIntent);
}
});
if (mHandler == null) {
mHandler = new Handler();
mHandler.post(mRunnable);
}
return Service.START_STICKY;
}
可运行的......
private Runnable mRunnable = new Runnable() {
@Override
public void run() {
refreshMain();
if (mHandler != null)
mHandler.postDelayed(this, MIN_UPDATE_TIME);
}
};
refreshMain太大了,我只是发布将结果发送回侦听器的片段...
call.enqueue(new Callback<Quote>() {
@Override
public void onResponse(Response<Quote> response) {
try {
List<Quote.SingleQuote> asList = response.body().query.results.getQuote();
mQuotes = new ArrayList<>(asList);
mListener.onUpdate(mQuotes);
Log.v(LOG_TAG, "Refreshed");
}
最后我的广播接收器......
public static class UpdateReceiver extends BroadcastReceiver{
@Override
public void onReceive(final Context context, final Intent intent) {
if(MainFragmentService.getInstance().mProgressDialog != null) MainFragmentService.getInstance().mProgressDialog.dismiss();
if(MainFragmentService.getInstance() != null) {
MainFragmentService.getInstance().mQuotes = intent.getParcelableArrayListExtra(Utility.QUOTE_INTENT);
MainFragmentService.getInstance().mAdapter.swapList(MainFragmentService.getInstance().mQuotes);
Log.v(LOG_TAG, "Package received");
MainActivity.sRefresherBinding = false;
}
}
}
这是棘手的部分。 所有这些代码都在我的nexus 5上运行,但是当我在Nexus 7上运行它时,进入另一个Activity并返回,服务挂起。数据被提取,广播,拾取,但对话框没有被解雇,数据不会显示在回收站视图中。
看看我的Nexus 5上的logcat:
V/MainFragmentService: onPause
V/MainFragmentService: unbinding
V/MainFragmentService: onStop
V/MainFragmentService: onResume
V/MainFragmentService: binding...
V/Refresh: RefreshMain called
V/MainFragmentService: Service Connected
V/Refresh: Refreshed
V/MainFragmentService: Package received
现在看看我的Nexus 7上的logcat:
V/MainFragmentService: onPause
V/MainFragmentService: unbinding
V/MainFragmentService: onStop
V/MainFragmentService: onResume
V/MainFragmentService: binding...
V/MainFragmentService: onResume
V/Refresh: RefreshMain called
V/MainFragmentService: Service Connected
V/Refresh: Refreshed
V/MainFragmentService: Package received
V/Refresh: Waiting
V/Refresh: Waiting
V/Refresh: RefreshMain called
V/Refresh: Refreshed
V/MainFragmentService: Package received
如果您对此代码的工作有疑问,应用程序就在这里,转到nuts。如果你读完了,谢谢你的时间!
答案 0 :(得分:1)
问题跟踪到开发者选项&gt;应用&gt;不要在Nexus 7上启用活动,在Nexus 5(和其他人)上禁用。
意味着真正的问题出现在Activity本身上,当它从导航中被破坏时。导航回来时,在现有的片段之上创建了一个新片段,因为我没有检查savedInstanceState
。上面评论中提到的混乱现在也已修复,无需再通过Manifest阻止对配置更改的活动破坏。
getFragmentManager().beginTransaction().add(R.id.content_main_list_container,
new MainFragmentService(), FRAGMENT_TAG_MAIN_LIST).commit();
现在是......
MainFragmentService mainFragment;
if(savedInstanceState == null) {
mainFragment = new MainFragmentService();
getFragmentManager()
.beginTransaction()
.add(R.id.content_main_list_container,
mainFragment, FRAGMENT_TAG_MAIN_LIST)
.commit();
}