我需要在启动时启动一个服务。我找了很多。他们在谈论广播接收器。由于我是Android开发的新手,我对android上的服务没有一个清晰的了解。请提供一些源代码。
发布于 2010-12-30 21:03:17
发布于 2010-12-30 20:55:29
你的接收者:
public class MyReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Intent myIntent = new Intent(context, YourService.class);
context.startService(myIntent);
}
}
您的AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.broadcast.receiver.example"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name" android:debuggable="true">
<activity android:name=".BR_Example"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!-- Declaring broadcast receiver for BOOT_COMPLETED event. -->
<receiver android:name=".MyReceiver" android:enabled="true" android:exported="false">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
</application>
<!-- Adding the permission -->
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
</manifest>
发布于 2016-12-22 15:15:22
您应该注册BOOT_COMPLETE并重新启动
<receiver android:name=".Services.BootComplete">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
<action android:name="android.intent.action.REBOOT"/>
</intent-filter>
</receiver>
https://stackoverflow.com/questions/4562734
复制相似问题