如何在Android 3.x中自动启动服务,测试tabblet是三星Galaxy 10.1。我的代码工作在一个noname tabblet与android 2.2.1代码工作也不在Android模拟器与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 package 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);
}
}
}
清单
<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 :(得分:5)
这是SD卡问题,Eclipse在我的三星Galaxy 10.1上默认在SD卡上安装新应用程序。要解决这个问题,我需要在清单中添加android:installLocation =“internalOnly”。
新的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>
我希望这将有助于在未来中取得成功。
答案 1 :(得分:0)
确保您的应用程序不在SD上。我的应用程序需要在启动时运行,然后不要将它放在SD上。