Java 自动启动android服务

Java 自动启动android服务,java,android,android-manifest,Java,Android,Android Manifest,如何在Android 3.x中自动启动服务,测试标签是三星Galaxy 10.1。我的代码在android 2.2.1的noname选项卡上运行,在android 3.x版的android emulator中也不运行 <application android:icon="@drawable/icon" android:label="@string/app_name"> <service android:name="StartAtBootService"&

如何在Android 3.x中自动启动服务,测试标签是三星Galaxy 10.1。我的代码在android 2.2.1的noname选项卡上运行,在android 3.x版的android emulator中也不运行

    <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>
代码:

    <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>
StartAtBootService.java 包测试.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");
        }
}
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>
StartBootServiceReciver.java 包测试.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");
        }
}
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>
显示

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


确保您的应用程序不在SD上。I您的应用程序需要在启动时运行,然后不要将其放在SD上。

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

    <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>
新舱单:

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


我希望这将在未来对某人有所帮助。

你在引导时没有收到onReceive调用吗?如果他在emulator上运行它,我想无论如何都不会涉及SD。我使用eclpise安装和测试emulator、galaxy 10.1和android 2.2.1的noname选项卡。我的代码只适用于android 2.2.1。是的,android SDK模拟器确实有SD@OP:您是否确保emulator上的应用程序不在SD上?由于某些原因,模拟器实际上存在与实际设备不同的问题。我在2.3.3 emulator上运行一些应用程序时遇到问题,因为它在我实际的2.3.3设备上运行得非常好。
    <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>