【发布时间】:2011-10-05 23:13:42
【问题描述】:
如何在 Android 设备已打开且操作系统正在运行时启动 Service?
【问题讨论】:
标签: android
如何在 Android 设备已打开且操作系统正在运行时启动 Service?
【问题讨论】:
标签: android
添加到 AndroidManifest.xml:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<service android:name=".YourService" />
<receiver android:name="com.your.package.AutoStart">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
创建类 AutoStart.java:
public class AutoStart extends BroadcastReceiver
{
@Override
public void onReceive(Context context, Intent intent)
{
Intent startServiceIntent = new Intent(context, YourService.class);
context.startService(startServiceIntent);
}
}
【讨论】: