gpt4 book ai didi

android - 绑定(bind)服务正在运行,但如果我关闭应用程序,计时器将无法工作

转载 作者:行者123 更新时间:2023-11-30 03:18:25 24 4
gpt4 key购买 nike

我正在开发我的第一个 Android 应用程序,但遇到服务问题。我有一项 Activity 和一项服务,在 Activity 中有一个按钮调用服务中的方法,在服务中我有一个计时器,它在 20 秒后在 Logcat 中显示一些内容。在 Activity 中,我使用 startService(intet) 启动服务,然后绑定(bind)到它,以便即使在关闭 Activity 后也能保持工作,正如许多主题中所建议的那样。

如果我点击后退按钮或主页按钮应用程序正常工作,但如果我按住主页按钮然后关闭应用程序我看不到日志,但如果我转到运行选项卡下的应用程序管理器我可以看到我的应用程序和服务正在运行!

我不想使用警报管理器。我在这里使用计时器的原因是我想确保我的服务真的有效!我只想在服务中做一些事情,并确保即使应用程序关闭它也能正常工作。

public class BoundService extends Service {

private final IBinder myBinder = new MyLocalBinder();


@Override
public IBinder onBind(Intent intent) {

return myBinder;
}


public int onStartCommand(Intent intent, int flags, int startId)
{
return START_STICKY;
}

@Override
public void onDestroy()
{
super.onDestroy();
Toast.makeText(this, "Service Stopped", Toast.LENGTH_LONG).show();

}

public void testNotification()
{
int interval = 20000; // 20 Second
Handler handler = new Handler();
Runnable runnable = new Runnable(){
public void run() {
Log.d("BoundService", "Timer");
}
};

handler.postAtTime(runnable, System.currentTimeMillis()+interval);
handler.postDelayed(runnable, interval);

}

public class MyLocalBinder extends Binder {
BoundService getService() {
return BoundService.this;
}
}
}

Activity :

   public class MainActivity extends Activity {

BoundService myService;
boolean isBound = false;





public void test(View view)
{
myService.testNotification();
}

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

Intent intent = new Intent(this, BoundService.class);

startService(intent);
bindService(intent, myConnection, Context.BIND_AUTO_CREATE);

}


private ServiceConnection myConnection = new ServiceConnection() {

public void onServiceConnected(ComponentName className,
IBinder service) {
MyLocalBinder binder = (MyLocalBinder) service;
myService = binder.getService();
isBound = true;
}

public void onServiceDisconnected(ComponentName arg0) {
isBound = false;
}

};



@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

}

谢谢

最佳答案

手册说:“当您创建一个新的处理程序时,它会绑定(bind)到创建它的线程的线程/消息队列”。这就是为什么您的处理程序在 Activity 线程中工作并且在 Activity 被终止后无法继续处理事件的原因。在您的服务中尝试这样的事情:

Thread worker = null;
Handler handler = null;

@Override
public void onCreate() {
worker = new Thread(new Runnable() {
@Override
public void run() {
Looper.prepare();
handler = new Handler();
Looper.loop();
}
});
worker.start();
}

@Override
public void onDestroy() {
handler.getLooper().quit();
worker = null;
handler = null;
}

编辑:

您还必须在您的服务中调用 startForeground(使用一些通知),因为如果没有前台服务在运行并且服务崩溃, Activity 管理器会在您关闭应用程序时终止应用程序进程,并且稍后会因为 START_STICKY 而重新启动。这是一种奇怪的行为,但我也在真实设备上看到了这一点。

关于android - 绑定(bind)服务正在运行,但如果我关闭应用程序,计时器将无法工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19623188/

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