我遵循了几条如何在启动时运行服务的说明。
在 Android 2.2 中,一切正常。
我注意到在 Android 2.3 中,进程崩溃,ActivityManager将服务调度为反复重启。
在我的服务中,我想每5秒钟doSomething()
!为此,我使用了TimerTask。
这是 MyService.java 代码:
package example.service;
import java.util.Timer;
import java.util.TimerTask;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;
public class MyService extends Service {
private static final String TAG = "MyService";
private static final int TIMER_SECONDS = 5;
private Timer doSomethingTimer;
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
super.onCreate();
Log.d(TAG, TAG + ": My Service Created");
}
@Override
public void onDestroy() {
super.onDestroy();
Log.d(TAG, TAG + ": My Service Destroyed");
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
super.onStartCommand(intent, flags, startId);
initDoSomethingTimer();
Log.d(TAG, TAG + ": My Service Started");
return START_STICKY;
}
private void initDoSomethingTimer() {
doSomethingTimer = new Timer();
doSomethingTimer.schedule(new TimerTask() {
@Override
public void run() {
doSomething();
}
}, 0, TIMER_SECONDS * 1000);
}
private void doSomething() {
Log.d(TAG, TAG + ": did something!!");
}
}
这是 MyStartupIntentReceiver.java 代码:
package example.service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class MyStartupIntentReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Intent serviceIntent = new Intent();
serviceIntent.setAction("example.service.MyService");
context.startService(serviceIntent);
}
}
并且,最后我的 AndroidManifest.xml 文件:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
android:installLocation="internalOnly"
package="example.service"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="10" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<service android:name=".MyService" >
<intent-filter>
<action android:name="example.service.MyService" />
</intent-filter>
</service>
<receiver android:name=".MyStartupIntentReceiver" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
</application>
</manifest>
结果并不出名:(启动后,服务已创建,但未启动,然后崩溃并且ActivityManager计划重新启动它 - 并启动循环! 以下是我们在 Logcat :
中可以看到的内容I/ActivityManager( 187): Start proc example.service for service example.service/.MyService: pid=615 uid=10052 gids={}
D/MyService( 615): MyService: My Service Created
(...)
I/Process ( 187): Sending signal. PID: 615 SIG: 9
W/ActivityManager( 187): Scheduling restart of crashed service example.service/.MyService in 59628ms
(...)
I/ActivityManager( 187): Start proc example.service for service example.service/.MyService: pid=639 uid=10052 gids={}
D/MyService( 639): MyService: My Service Created
(...)
I/Process ( 187): Sending signal. PID: 639 SIG: 9
W/ActivityManager( 187): Scheduling restart of crashed service example.service/.MyService in 238512ms
有什么建议吗?!我被卡住了。我注意到除了这个新服务之外的其他服务(没有timertask)在 Android 2.3 中开始崩溃,但是出现了相同的错误。
答案 0 :(得分:0)
在我看来,问题在于你没有足够的内存,因此android会终止这项服务并再次启动它。当我在模拟器中尝试ICS时,我遇到了这个问题。尝试在AVD Manager中为您的AVD(参数“设备RAM大小”增加到512Mb)增加RAM。请写一下结果。