【发布时间】:2016-12-14 22:50:23
【问题描述】:
我的广播接收器会在应用启动时自动运行。
如何预防?我想在单击按钮时启动它。
广播接收者:
public class TestAlarmReceiver extends BroadcastReceiver {
@DebugLog
@Override
public void onReceive(Context context, Intent intent) {
Intent i = new Intent(context, TestService.class);
i.putExtra("foo", "bar");
context.startService(i);
}
}
意图服务:
public class TestService extends IntentService {
public TestService() {
super("TestService");
}
@DebugLog
@Override
protected void onHandleIntent(Intent intent) {
Log.d(getClass().getSimpleName(), "Service is running");
}
}
另外,我将这些行添加到应用程序部分的 AndroidManifest 文件中:
<receiver
android:name=".service.TestAlarmReceiver"
android:process=":remote" />
<service
android:name=".service.TestService"
android:exported="false" />
【问题讨论】:
-
这里有一些有用的链接android:enabled 和android-broadcastreceiver-with-no-intent-filte。要从按钮单击开始,您可以使用 Intent intent = new Intent(getApplicationContext(), TestAlarmReceiver.class);发送广播(意图);
标签: android service broadcastreceiver