前台服务在启动时无法启动

3
我将尝试在启动时启动前台服务,但它从未启动,但是如果我尝试启动普通的后台服务,则可以完美地启动。
您能否告诉我我的代码有什么问题?
我的代码如下: 清单文件:
<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name" >
    <receiver android:name="com.test.andsrvfg.AndSrvFgService">
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
        </intent-filter>
    </receiver>
    <service android:name="AndSrvFgService">
        <intent-filter>
            <action android:name="com.test.andsrvfg.AndSrvFgService"></action>
        </intent-filter>
    </service>
</application>

处理ACTION_BOOT_COMPLETED的BroadcastReceiver:

public class AndSrvFgStarter extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) 
    {
        if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)) {
            Intent i = new Intent();
            i.setAction("com.test.andsrvfg.AndSrvFgService");
            context.startService(i);
        }
    }
}

实际服务如下:
public class AndSrvFgService extends Service {

    private boolean bForeground = false;
    public AndSrvFgService() {
    }

    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        if(!bForeground) {
            bForeground = true;
            Notification note = new Notification( 0, null, System.currentTimeMillis() );
            note.flags |= Notification.FLAG_NO_CLEAR;
            startForeground( 1242, note );      
                }


        return START_STICKY;
    }

    @Override
    public void onDestroy() {
        if(bForeground) {
            stopForeground(true);
        }
    }

你确定你的清单正确声明了Intent-Filter吗?由于你没有发布它,我无法在我的端上进行验证,但这似乎是你的问题。 - ahodder
你在测试哪个版本? - abbas.aniefa
是的,我使用了"RECEIVE_BOOT_COMPLETED"。同样的AndroidManifest.xml在后台服务中运行得很好。实际上,如果我使用一个活动来启动前台服务,它完全正常地工作。 - Androidme
请查看此链接:http://commonsware.com/blog/2011/07/13/boot-completed-regression-confirmed.html - abbas.aniefa
http://www.jjoe64.com/2011/06/autostart-service-on-device-boot.html - moskis
显示剩余3条评论
1个回答

5

这段代码对我来说是有效的:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.dfsdf"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="15" />
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/title_activity_main" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <activity android:name="com.google.ads.AdActivity"
              android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"/>

        <service android:name=".MyService"/>

        <receiver android:enabled="true" 
            android:name=".BootUpReceiver"
            android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
            <intent-filter >
                <action android:name="android.intent.action.BOOT_COMPLETED"/>
                <category android:name="android.intent.category.DEFAULT"/>
            </intent-filter>
        </receiver>

    </application>

</manifest>
  • 将以下内容放入接收器中:

    android:permission="android.permission.RECEIVE_BOOT_COMPLETED"

  • 将启用设置为true:

    android:enabled="true"


我添加了一条注释。现在我添加了一些额外的行。但它必须正常工作。 - moskis
仍然不起作用...我能看到你的代码和我的代码唯一的区别是-我没有使用任何活动。因此,它只是广播接收器和服务。请注意,如果我在后台启动服务而不是前台,则相同的代码可以正常工作。 - Androidme
1
请查看接收器。我在intent-filter中放置了android:enabled、android:permission和category。如果您有更多问题,我可以上传完整的工作源代码。 - moskis
谢谢Moskis。我删除了项目,创建了一个具有相同设置的新项目,你知道吗?它对我起作用了。感谢你的帮助。如果你上传你的代码,可能会帮助其他人... - Androidme
谢谢!对我来说,这是:android:enabled="true" - Manza

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接