首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Autostart android服务

Autostart android服务
EN

Stack Overflow用户
提问于 2011-09-08 16:00:13
回答 2查看 5.5K关注 0票数 3

如何在Android 3.x中自动启动服务,测试标签板是三星Galaxy 10.1。我的代码可以在android 2.2.1的noname标签页上运行,也不能在android版本3.x的android模拟器中运行

代码:

StartAtBootService.java包test.autostart;

代码语言:javascript
复制
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包test.autostart;

代码语言:javascript
复制
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);
        }
    }
}

清单

代码语言:javascript
复制
    <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>
EN

回答 2

Stack Overflow用户

发布于 2011-09-14 16:01:10

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

新的清单:

代码语言:javascript
复制
<?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>

我希望这会对将来的人有所帮助。

票数 5
EN

Stack Overflow用户

发布于 2011-09-08 16:27:46

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7344897

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档