[英]Autostart android service
How to automatically start a service in Android 3.x, the test tabblet is a Samsung Galaxy 10.1. 如何在Android 3.x中自动启动服务,测试tabblet是三星Galaxy 10.1。 My code works on a noname tabblet with android 2.2.1 The code works nor in android emulator with the android version 3.x 我的代码适用于Android 2.2.1的noname tabblet该代码也适用于android版本3.x的android模拟器
Code: 码:
StartAtBootService.java package test.autostart; StartAtBootService.java包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 package test.autostart; StartAtBootServiceReciver.java包test.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);
}
}
}
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>
It was a SD-card issue, Eclipse install new apps on the SD-card by default on my Samsung Galaxy 10.1. 这是一个SD卡问题,Eclipse在我的三星Galaxy 10.1上默认在SD卡上安装新应用程序。 To fix the issue I needed to add android:installLocation="internalOnly" in the manifest. 要解决这个问题,我需要在清单中添加android:installLocation =“internalOnly”。
The new 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>
I hope this will help somone in the futre. 我希望这将有助于在未来中取得成功。
Make sure your application is not on SD. 确保您的应用程序不在SD上。 I your application needs to run on boot time, then do not place it on SD. 我的应用程序需要在启动时运行,然后不要将它放在SD上。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.