英文:
Problem of being unable to use other apps when calling intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
问题
在GpsData应用程序中,GPS数据是从服务器接收的。GPS数据每秒由一个线程接收,并且InfoService类将接收到的GPS数据数量发送到MainActivity。在MainActivity中检查日志时,会调用onPause() -> onNewIntent() -> onResume()。
然而,如果我按下主屏幕按钮以使用其他应用程序,GpsData应用程序仍然保持打开状态,无法使用其他应用程序。
是否有办法让GpsData应用程序在按下主屏幕按钮后继续接收GPS数据,同时允许用户使用其他应用程序?
请注意,上述内容已经翻译,不包含代码部分。
英文:
In the GpsData app, GPS data is received from the server. The GPS data is received by a thread every second, and the InfoService class sends the number of received GPS data to MainActivity. When checking the logs in MainActivity, onPause() -> onNewIntent() -> onResume() is called.
However, if I press the home button to use other apps, the GpsData app remains open and cannot use other apps.
Is there any way for the GpsData app to continue receiving GPS data while allowing the user to use other apps by pressing the home button?
Code>>
public class Client
{
public class SockHandler extends Handler
{
@Override
public void handleMessage(Message msg)
{
switch (msg.what)
{
case TCPClient.SOCKET_WRITTEN:
break;
case TCPClient.SOCKET_READ:
if (msg.arg1 <= 0)
break;
byte[] buf = (byte[])msg.obj;
String recv = new String(buf, 0, msg.arg1);
mRecvCount += msg.arg1;
mListener.setCount(String.format("%d", mRecvCount));
break;
case TCPClient.SOCKET_KEEP_ALIVE:
if (!isConnected()) break;`
...
break;
}
}
}
public interface Message
{
void setCount(String count);
}
}
public class InfoService extends Service implement Client.message {
........
@Override
public void setCount(String count) {
Intent intent = new Intent(getApplicationContext(),MainActivity.class);
intent.putExtra("Count", count);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
}
public class MainActivity extends AppCompatActivity
{
@Override
protected void onResume() {
Log.i("MainActivity", "onResume CALL")
super.onResume();
}
@Override
protected void onRestart() {
super.onRestart();
}
@Override
protected void onPause() {
Log.i("MainActivity", "onPause CALL")
super.onPause();
}
public void setCount(String strCount) {
mRelayCountView.setText(strCount);
}
@Override
protected void onNewIntent(Intent intent) {
Log.i("MainActivity", "onNewIntent CALL")
........
if(intent.getStringExtra("Count")!=null) {
setCount(intent.getStringExtra("Count"));
}
.......
super.onNewIntent(intent);
}
}
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:launchMode="singleInstance"
android:screenOrientation="portrait"
android:theme="@style/AppTheme.NoActionBar"
android:windowSoftInputMode="stateHidden|adjustPan"></activity>
I couldn't find a solution. Thank you for reading this question.
答案1
得分: 1
Sure, here's the translated content:
> GpsData应用是否有办法在按下主屏按钮后,继续接收GPS数据并允许用户使用其他应用程序?
好吧,你每秒钟从一个服务中启动这个活动(这似乎是操作系统不应该允许的事情),所以不要这样做。
> GPS数据每秒钟由一个线程接收,并且InfoService类将接收到的GPS数据数量发送到MainActivity。
移除启动活动的代码,而是将该值存储在共享首选项或数据库中,并稍后从活动中读取它,当用户打开它时,从活动中绑定到服务以接收更新:
https://developer.android.com/guide/components/bound-services
英文:
>Is there any way for the GpsData app to continue receiving GPS data while allowing the user to use other apps by pressing the home button?
Well, you're launching the activity every second from a Service (which seems like something the OS shouldn't even allow) so don't do that.
> The GPS data is received by a thread every second, and the InfoService class sends the number of received GPS data to MainActivity.
Remove the code that launches the activity, store the value instead in shared preferences or a database and read it later from the activity, and bind to the service from the Activity when the user opens it to receive updates while its running:
https://developer.android.com/guide/components/bound-services
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论