我的廣播接收器在應用程序啓動時自動運行。防止在啓動時啓動服務
如何預防?我想通過點擊按鈕啓動它。
廣播接收器:
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