我的广播接收器在应用程序启动时自动运行。防止在启动时启动服务
如何预防?我想通过点击按钮启动它。
广播接收器:
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);
}
}
IntentService:
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" />
下面是一些有用的链接[安卓:启用](http://developer.android.com/guide/topics/manifest/receiver-element.html)和[Android的广播接收器与 - 无意图-filte](http://stackoverflow.com/questions/9060137/android-broadcastreceiver-with-no-intent-filter)。从按钮单击开始,您可以使用Intent intent = new Intent(getApplicationContext(),TestAlarmReceiver.class); sendBroadcast(intent); – avinash