gpt4 book ai didi

android - 为什么我的服务停止了?

转载 作者:行者123 更新时间:2023-11-30 04:21:36 25 4
gpt4 key购买 nike

我正在创建一个与 BroadcastReceiverService 相关的应用程序。

我的 Intent 是我的服务从开机启动。

为此,我正在使用广播接收器并通过它启动我的服务。

它会很好地工作,我的服务等级看起来像休假。

public class Myservice extends Service 
{
public void onCreate()
{
super.onCreate();
}

@Override
public void onStart(Intent intent, int startId)
{
super.onStart(intent, startId);

mScrollback = new Vector<String>();
Thread thr = new Thread(worker);
thr.start();
}

Runnable worker = new Runnable()
{
public void run()
{
runLog();
return;
}
};
}

这是我的服务类,它工作正常。

在我的应用程序开始之前,我正在获取信息关于通过此应用程序提供的服务。

当我的应用程序运行时,服务没有运行。

如果有人有解决方案,请告诉我。

最佳答案

这不是您在服务中进行多线程处理的方式。查看“扩展服务类”步骤:http://developer.android.com/guide/topics/fundamentals/services.html

服务类中的多线程是通过处理程序线程完成的。这是文档中的代码(来自上面的链接)

public class HelloService extends Service {
private Looper mServiceLooper;
private ServiceHandler mServiceHandler;

// Handler that receives messages from the thread
private final class ServiceHandler extends Handler {
public ServiceHandler(Looper looper) {
super(looper);
}
@Override
public void handleMessage(Message msg) {
// Normally we would do some work here, like download a file.
// For our sample, we just sleep for 5 seconds.
long endTime = System.currentTimeMillis() + 5*1000;
while (System.currentTimeMillis() < endTime) {
synchronized (this) {
try {
wait(endTime - System.currentTimeMillis());
} catch (Exception e) {
}
}
}
// Stop the service using the startId, so that we don't stop
// the service in the middle of handling another job
stopSelf(msg.arg1);
}
}

@Override
public void onCreate() {
// Start up the thread running the service. Note that we create a
// separate thread because the service normally runs in the process's
// main thread, which we don't want to block. We also make it
// background priority so CPU-intensive work will not disrupt our UI.
HandlerThread thread = new HandlerThread("ServiceStartArguments",
Process.THREAD_PRIORITY_BACKGROUND);
thread.start();

// Get the HandlerThread's Looper and use it for our Handler
mServiceLooper = thread.getLooper();
mServiceHandler = new ServiceHandler(mServiceLooper);
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Toast.makeText(this, "service starting", Toast.LENGTH_SHORT).show();

// For each start request, send a message to start a job and deliver the
// start ID so we know which request we're stopping when we finish the job
Message msg = mServiceHandler.obtainMessage();
msg.arg1 = startId;
mServiceHandler.sendMessage(msg);

// If we get killed, after returning from here, restart
return START_STICKY;
}

@Override
public IBinder onBind(Intent intent) {
// We don't provide binding, so return null
return null;
}

@Override
public void onDestroy() {
Toast.makeText(this, "service done", Toast.LENGTH_SHORT).show();
}
}

此外,您必须确保您的广播接收器列在 list 中并接收到 BOOT_COMPLETED Intent:

<receiver android:name="MyBroadCast">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>

关于android - 为什么我的服务停止了?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9175583/

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