安卓自启动服务

3

如何在Android 3.x中自动启动服务,测试平板电脑是三星Galaxy 10.1。我的代码在一个带有Android 2.2.1的无名平板电脑上可以工作。但是该代码在Android版本为3.x的模拟器上不起作用。

代码:

StartAtBootService.java package test.autostart;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;

public class StartAtBootService extends Service 
{
        public IBinder onBind(Intent intent)
        {
            return null;
        }

        @Override
        public void onCreate() 
        {
            Log.v("StartServiceAtBoot", "onCreate");
        }

        @Override
        public int onStartCommand(Intent intent, int flags, int startId) 
        {
            Log.v("StartServiceAtBoot", "onStartCommand()");          
            return START_STICKY;
        }

        @Override
        public void onDestroy() 
        {
            Log.v("StartServiceAtBoot", "onDestroy");
        }
}

StartAtBootServiceReciver.java 包测试。autostart;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

public class StartAtBootServiceReceiver extends BroadcastReceiver 
{
    @Override
    public void onReceive(Context context, Intent intent) 
    {
        if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)) {
            Intent i = new Intent();
            i.setAction("test.autostart.StartAtBootService");
            context.startService(i);
        }
    }
}

清单文件

    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <service android:name="StartAtBootService">
            <intent-filter>
                <action android:name="test.autostart.StartAtBootService">
                </action>
            </intent-filter>
        </service>
        <receiver android:name="StartAtBootServiceReceiver">
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED">
                </action>
                <category android:name="android.intent.category.HOME">
                </category>
            </intent-filter>
        </receiver>
    </application>
</manifest>

你的设备在启动时没有收到 onReceive 的调用吗? - Erdal
2个回答

5

这是一个SD卡问题,在我的三星Galaxy 10.1上,Eclipse默认将新应用安装在SD卡上。为了解决这个问题,我需要在清单文件中添加android:installLocation="internalOnly"。

新的清单文件:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="test.autostart"
      android:versionCode="1"
      android:versionName="1.0" android:installLocation="internalOnly">
    <uses-sdk android:minSdkVersion="8" />
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"></uses-permission>
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <service android:name="StartAtBootService">
            <intent-filter>
                <action android:name="test.autostart.StartAtBootService">
                </action>
            </intent-filter>
        </service>
        <receiver android:name="StartAtBootServiceReceiver">
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED">
                </action>
                <category android:name="android.intent.category.HOME">
                </category>
            </intent-filter>
        </receiver>
    </application>
</manifest>

我希望这能帮助未来的某个人。


0
确保您的应用程序不在SD卡上。如果您的应用程序需要在开机时运行,请不要将其放在SD卡上。

如果他在模拟器上运行它,我想就不涉及SD卡了。 - DonGru
我使用Eclipse进行安装和测试,包括模拟器、Galaxy 10.1和一款未知品牌的Android 2.2.1平板电脑。我的代码只能在Android 2.2.1上运行。 - user932865
是的,Android SDK模拟器确实有SD卡。 @OP:你确定模拟器上的应用程序不在SD卡上吗? - kingmaple
仿真器实际上存在与实际设备不同的问题,原因有些复杂。我曾经在2.3.3仿真器上运行某些应用程序时遇到问题,而在我的实际2.3.3设备上却完美运行。 - kingmaple

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