自动启动的Android服务 [英] Autostart android service
问题描述
如何自动启动的Android 3.X服务,测试tabblet是三星Galaxy 10.1。我的code适用于采用Android 2.2.1 NONAME tabblet的code工作也不在了Android 3.x版本Android模拟器
code:
StartAtBootService.java
包test.autostart;
进口android.app.Service;
进口android.content.Intent;
进口android.os.IBinder;
进口android.util.Log;公共类StartAtBootService延伸服务
{
公众的IBinder onBind(意向意图)
{
返回null;
} @覆盖
公共无效的onCreate()
{
Log.v(StartServiceAtBoot,的onCreate);
} @覆盖
公众诠释onStartCommand(意向意图,诠释旗帜,INT startId)
{
Log.v(StartServiceAtBoot,onStartCommand());
返回START_STICKY;
} @覆盖
公共无效的onDestroy()
{
Log.v(StartServiceAtBoot,的onDestroy);
}
}
StartAtBootServiceReciver.java
包test.autostart;
进口android.content.BroadcastReceiver;
进口android.content.Context;
进口android.content.Intent;公共类StartAtBootServiceReceiver扩展广播接收器
{
@覆盖
公共无效的onReceive(上下文的背景下,意图意图)
{
如果(intent.getAction()。等于(Intent.ACTION_BOOT_COMPLETED)){
意图I =新意图();
i.setAction(test.autostart.StartAtBootService);
context.startService(ⅰ);
}
}
}
清单
<应用机器人:图标=@绘制/图标机器人:标签=@字符串/ APP_NAME>
<服务机器人:名字=StartAtBootService>
&所述;意图滤光器>
<作用机器人:名字=test.autostart.StartAtBootService>
< /作用>
&所述; /意图滤光器>
< /服务>
<接收机器人:名字=StartAtBootServiceReceiver>
&所述;意图滤光器>
<作用机器人:名字=android.intent.action.BOOT_COMPLETED>
< /作用>
<类机器人:名字=android.intent.category.HOME>
< /类别>
&所述; /意图滤光器>
< /接收器>
< /用途>
< /清单>
这是一个SD卡的问题时,Eclipse在我的三星Galaxy 10.1在默认情况下安装在SD卡上的新应用。要解决我需要添加Android的问题:在清单中的installLocation =internalOnly
新清单:
<?XML版本=1.0编码=UTF-8&GT?;
<清单的xmlns:机器人=http://schemas.android.com/apk/res/android
包=test.autostart
安卓版code =1
机器人:=的versionName1.0安卓的installLocation =internalOnly>
<采用-SDK安卓的minSdkVersion =8/>
<使用许可权的android:NAME =android.permission.RECEIVE_BOOT_COMPLETED>< /使用许可权>
<应用机器人:图标=@绘制/图标机器人:标签=@字符串/ APP_NAME>
<服务机器人:名字=StartAtBootService>
&所述;意图滤光器>
<作用机器人:名字=test.autostart.StartAtBootService>
< /作用>
&所述; /意图滤光器>
< /服务>
<接收机器人:名字=StartAtBootServiceReceiver>
&所述;意图滤光器>
<作用机器人:名字=android.intent.action.BOOT_COMPLETED>
< /作用>
<类机器人:名字=android.intent.category.HOME>
< /类别>
&所述; /意图滤光器>
< /接收器>
< /用途>
< /清单>
我希望这将有助于有人为在futre。
How to automatically start a service in Android 3.x, the test tabblet is a Samsung 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
Code:
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);
}
}
}
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. To fix the issue I needed to add android:installLocation="internalOnly" in the manifest.
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.
这篇关于自动启动的Android服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!