android服务在启动时开始强制停止

时间:2011-11-24 06:25:22

标签: android android-service

我是Android新手..我正在尝试编写Android服务,该服务将在设备启动时启动。当我使用模拟器运行应用程序时,我能够在" adb logcat&#34 ;;中看到我的调试语句(system.outs)。这意味着我的服务在启动完成后被调用。但是根据日志,整个代码在' onStart'服务方法没有被执行。日志未显示所有预期的system.outs。

以下是显示“强制停止服务”的日志。陈述(以BOLD标记): -

**D/Exchange(  222): BootReceiver onReceive
D/EAS SyncManager(  222): !!! EAS SyncManager, onCreate
I/ActivityManager(   58): Start proc com.webapps.service for broadcast com.webap
ps.service/.MyStartupIntentReceiver: pid=232 uid=10038 gids={1015}
D/EAS SyncManager(  222): !!! EAS SyncManager, onStartCommand
D/EAS SyncManager(  222): !!! EAS SyncManager, stopping self
D/Eas Debug(  222): Logging:
D/EAS SyncManager(  222): !!! EAS SyncManager, onDestroy
**I/System.out(  232): **************MyStartupIntentReceiver******************
I/System.out(  232): **************Service created******************
I/ActivityManager(   58): Start proc com.android.music for broadcast com.android
.music/.MediaAppWidgetProvider: pid=241 uid=10022 gids={3003, 1015}
**I/System.out(  232): **************Service started****************
I/System.out(  232): *********Current date is :=>  2011/Nov/24 11:02:34**
I/ActivityManager(   58): Start proc com.android.quicksearchbox for broadcast co
m.android.quicksearchbox/.SearchWidgetProvider: pid=247 uid=10012 gids={3003}
I/ActivityThread(  247): Publishing provider com.android.quicksearchbox.google:
com.android.quicksearchbox.google.GoogleSuggestionProvider
I/ActivityManager(   58): Start proc com.android.protips for broadcast com.andro
id.protips/.ProtipWidget: pid=255 uid=10007 gids={}
D/PackageParser(   58): Scanning package: /data/app/vmdl34597.tmp
W/PackageParser(   58): Unknown element under <application>: uses-permission at
/data/app/vmdl34597.tmp Binary XML file line #18
I/PackageParser(   58): com.webapps.service: compat added android.permission.WRI
TE_EXTERNAL_STORAGE android.permission.READ_PHONE_STATE
D/MediaScanner(  195):  prescan time: 2646ms
D/MediaScanner(  195):     scan time: 91ms
D/MediaScanner(  195): postscan time: 1ms
D/MediaScanner(  195):    total time: 2738ms
I/PackageManager(   58): Removing non-system package:com.webapps.service
D/dalvikvm(  128): GC_EXTERNAL_ALLOC freed 4610 objects / 268504 bytes in 125ms
D/PackageManager(   58): Scanning package com.webapps.service
I/PackageManager(   58): Package com.webapps.service codePath changed from /data
/app/com.webapps.service-2.apk to /data/app/com.webapps.service-1.apk; Retaining
 data and using new
I/PackageManager(   58): /data/app/com.webapps.service-1.apk changed; unpacking
D/installd(   34): DexInv: --- BEGIN '/data/app/com.webapps.service-1.apk' ---
I/Launcher.Model(  128): not binding apps: no Launcher activity
D/dalvikvm(  128): GC_EXPLICIT freed 1595 objects / 79016 bytes in 251ms
D/dalvikvm(  261): DexOpt: load 200ms, verify 217ms, opt 2ms
D/installd(   34): DexInv: --- END '/data/app/com.webapps.service-1.apk' (succes
s) ---
W/PackageManager(   58): Code path for pkg : com.webapps.service changing from /
data/app/com.webapps.service-2.apk to /data/app/com.webapps.service-1.apk
W/PackageManager(   58): Resource path for pkg : com.webapps.service changing fr
om /data/app/com.webapps.service-2.apk to /data/app/com.webapps.service-1.apk
D/PackageManager(   58):   Services: com.webapps.service.SimpleService
D/PackageManager(   58):   Receivers: com.webapps.service.MyStartupIntentReceive
r
I/installd(   34): move /data/dalvik-cache/data@app@com.webapps.service-1.apk@cl
asses.dex -> /data/dalvik-cache/data@app@com.webapps.service-1.apk@classes.dex
D/PackageManager(   58): New package installed in /data/app/com.webapps.service-
1.apk
D/MediaScannerService(  195): done scanning volume internal
I/ActivityManager(   58): Force stopping package com.webapps.service uid=10038
W/ActivityManager(   58): Scheduling restart of crashed service com.webapps.serv
ice/.SimpleService in 13004ms
I/Process (   58): Sending signal. PID: 232 SIG: 9
**I/ActivityManager(   58):   Force stopping service ServiceRecord{4502c7e0 com.we
bapps.service/.SimpleService}**
W/ActivityManager(   58): setProcessForeground called on unknown pid: 232
E/JavaBinder(   58): !!! FAILED BINDER TRANSACTION !!!
I/ActivityManager(   58): Force stopping package com.webapps.service uid=10038
**I/ActivityManager(   58): Force stopping package com.webapps.service uid=10038**
D/dalvikvm(  128): GC_EXPLICIT freed 6910 objects / 354424 bytes in 210ms 

以下是我的代码

的AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.webapps.service"
      android:versionCode="1"
      android:versionName="1.0">


    <application android:icon="@drawable/icon" android:label="@string/app_name">

        <!--<activity android:name=".ServiceController"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity> -->

        <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

        <receiver android:name=".MyStartupIntentReceiver">
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
                <category android:name="android.intent.category.HOME" />
            </intent-filter>
        </receiver>

       <service android:name=".SimpleService">
            <intent-filter>
                <action android:name="com.wissen.startatboot.SimpleService" />
            </intent-filter>
        </service>

    </application>
</manifest>

package com.webapps.service;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.widget.Toast;

import java.util.Calendar;
import java.text.SimpleDateFormat;

public class SimpleService extends Service {
    @Override
    public IBinder onBind(Intent arg0) {
          return null;
    }
    @Override
    public void onCreate() {
          super.onCreate();
          System.out.println("**************Service created****************");
          Toast.makeText(this,"Service created ...", Toast.LENGTH_LONG).show();
    }

    @Override
    public void onDestroy() {
          super.onDestroy();
          System.out.println("**************Service destroyed****************");
          Toast.makeText(this, "Service destroyed ...", Toast.LENGTH_LONG).show();
    }

    public void onStart(Intent intent, int startId) {

        super.onStart(intent, startId);
        System.out.println("**************Service started****************");
        Toast.makeText(this, "Service Started", Toast.LENGTH_LONG).show();
        int cnt=0;
        while (true)
        {
        Calendar currentDate = Calendar.getInstance();
        SimpleDateFormat formatter=new SimpleDateFormat("yyyy/MMM/dd HH:mm:ss");
        String dateNow = formatter.format(currentDate.getTime());
        System.out.println("*********Current date is :=>  " + dateNow);
        cnt++;
        try{
        Thread.sleep (20000);
        }catch (Exception e){System.out.println(e);}
        if (cnt == 4)
            break;
        }
        System.out.println("********CNT*************  " + cnt);
    }
}

package com.webapps.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("com.wissen.startatboot.SimpleService");
    System.out.println("**************MyStartupIntentReceiver****************");    
    context.startService(serviceIntent);
}
}

SimpleService.java在包com.webapps.service下定义;而MyStartupIntentReceiverintentAction设为com.wissen.startatboot.SimpleService

这是否导致我的服务强行停止?

2 个答案:

答案 0 :(得分:0)

不要在onStart中工作! Android将等到它返回,它永远不会到期。超时将触发,Android将终止您的服务!

你是否在另一个主题中,例如AsyncTask

答案 1 :(得分:0)

如果删除while(true)循环,您的服务是否会崩溃?我猜想onStart预计不会执行太久。 此外,Service不会在单独的线程中执行。