startActivity冻结我的android应用程序后的startService
下面的代码在应用程序从一个活动导航到另一个活动后冻结。我检查了我的日志,没有任何错误。你能帮我解决这个问题吗?谢谢 我的服务班startActivity冻结我的android应用程序后的startService,android,android-intent,android-service,android-pendingintent,Android,Android Intent,Android Service,Android Pendingintent,下面的代码在应用程序从一个活动导航到另一个活动后冻结。我检查了我的日志,没有任何错误。你能帮我解决这个问题吗?谢谢 我的服务班 public class MyAlarmService extends Service { private Timer timer1 = new Timer(); private Timer timer2 = new Timer(); private static final long UPDATE_INTERVAL = 25000; @Override publi
public class MyAlarmService extends Service
{
private Timer timer1 = new Timer();
private Timer timer2 = new Timer();
private static final long UPDATE_INTERVAL = 25000;
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
@Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
_startService();
}
private void _startService() {
timer1.scheduleAtFixedRate(new TimerTask() {
public void run() {
System.out.println("Timer started1");
Leavenotification(MyAlarmService.this);
Timesheetnotification(MyAlarmService.this);
}
}, 0, UPDATE_INTERVAL);
}
@Override
public void onStart(Intent intent, int startId) {
super.onStart(intent, startId);
}
@Override
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
}
}
这就是我开始服务和活动的方式
Intent intent = new Intent(this, Dashboard.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
intent.putExtra("userID", userID);
intent.putExtra("name", name);
startActivity(intent);
startService(new Intent(this,MyAlarmService.class);
我不确定这里出了什么问题 在清单中声明您的服务
<service class=".MyService" name=".MyService">
<intent-filter>
<action
android:value="sample.service.MY_SERVICE"
android:name=".MyService"
android:process="another_thread"/>
</intent-filter>
</service>
一旦您开始另一项活动,您当前的活动将进入后台。您当前的活动将不会在主线程上运行。但是服务需要在主线程上运行。。服务在其宿主进程的主线程中运行。试着这样做
startService(new Intent(this,MyAlarmService.class);
startActivity(intent);
服务不会自动在另一个线程中运行,您的服务也不会尝试启动workerthread
另外,onStart(…)
已被弃用,您的服务是一个“绑定”和“启动”的服务,一般来说还可以,但如果不是真的需要,我会决定只使用一个。最简单的方法可能是使用为您处理线程的,但有一些限制
我建议看一看“绑定”服务和“启动”服务之间的区别
总之,首先检查它是否适合您的需要,如果不适合,您将不得不自己处理服务中的线程问题。调试服务需要的不仅仅是LogCat,我建议您仔细检查