0

有broadcastReceiver 接收启动消息并设置一个alarmmanager。

它每隔 DELAY 分钟启动一项服务:

 Intent intent = new Intent();
 intent.setAction("org.jxdwinter.getMessageServcie");
 PendingIntent pendingintent = PendingIntent.getService(arg0, 0, intent, 0);
 am = (AlarmManager) arg0.getSystemService(Context.ALARM_SERVICE); 
 am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), DELAY, pendingintent);

来自 romate 服务器的服务轮询数据在一个新线程中:

public int onStartCommand(Intent intent, int flags, int startId)
{
    this.getMessage.start();
    return Service.START_NOT_STICKY;
}
private class GetMessage extends Thread
{
    public void run()
    {
       ... ....
    }       
}

当应用程序启动时,我可以在第一时间获取数据并且没有错误。

然后在 DELAY mins 内再次启动服务,出现错误:线程已启动。

为什么会发生?请帮我解决这个问题。谢谢!

4

2 回答 2

1

也许您可以在每次需要运行时创建线程的新实例:

public int onStartCommand(Intent intent, int flags, int startId)
{
    new GetMessage().start();
    return Service.START_NOT_STICKY;
}
于 2012-11-12T08:29:49.767 回答
0

您正在使用的 GetMesdsage 类的实例是否是静态的?如果是,请不要这样做 - 初始化新的

于 2012-11-12T08:30:46.180 回答