如何在 Android 2.3 (Gingerbread) 中启动时运行服务而不崩溃

发布于 2024-12-23 14:34:59 字数 4266 浏览 0 评论 0原文

我遵循了有关如何在启动时运行服务的几条说明。

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

有什么建议吗?!我被困住了。我注意到除了这个新服务(没有计时器任务)之外的其他服务在 Android 2.3 中开始崩溃,并出现相同的错误。

I followed several instructions in how to run a service on boot.

In Android 2.2 everything works OK.

I noticed that in Android 2.3 the process crashes and ActivityManager schedules the service to restart over and over.

In my service I want to doSomething() every 5 seconds! For this, I'm using a TimerTask.

Here is MyService.java code:

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!!");
    }

}

Here is MyStartupIntentReceiver.java code:

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);
    }

}

and, finnally my AndroidManifest.xml file:

<?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>

The result is not famous :( After the boot, the service is created, but not started, and then crashes and ActivityManager schedule to restart it - and start the loop!
Here is what we can see at 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

Any suggestions?! I'm stuck. I noticed that other services besides this new one (without the timertask) started to crash in Android 2.3, with the same errors.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

赢得她心 2024-12-30 14:34:59

在我看来,问题在于你没有足够的内存,因此android杀死了这个服务并一直重新启动它。我在模拟器中尝试ICS时遇到了这个问题。尝试在 AVD 管理器中增加 AVD 的 RAM(参数“设备 RAM 大小”至 512Mb)。请写下结果。

It seems to me that the problem is that you do not have enough memory, thus android kills this service and starts it again all the time. I had this problem when I tried ICS in the emulator. Try to increase RAM for your AVD (parameter "Device RAM size" to 512Mb) in AVD Manager. Write, please, about the results.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文