화면이 켜지면 비행기 모드가 켜지도록 브로드 캐스트 수신기를 등록하려고합니다. 제가 등록한 활동에 머물러있는 한 그것은 효과가 있습니다. 그러나 활동을 떠나 자마자 활동이 멈 춥니 다. Android Manifest는 정적이므로 사용할 수 없으며 Android Manifest 메서드를 사용하면 Screen을 깨울 때 안드로이드가 여러 가지를 실행하지 않기 때문에 SCREEN_ON을 사용할 수 없습니다. registerReceiver AFAIK를 사용합니다.방송 수신기를 활동에 동적으로 등록 및 등록 해제하면서도 활동 외부에서 실행할 수있게 허용합니다.
내 활동 -
public class WakeActivity extends Activity {
IntentFilter screenon = new IntentFilter("android.intent.action.SCREEN_ON");
//Skipped a bunch of code here to keep it relevant. Remember, the broadcast receiver
//does in fact work while I'm in the activity, so the problem isn't with
//the missing code here. Still, let me know if I'm missing something.
//The following method is called within onCreate()
protected void airplane(int i) {
Screen_On screenon_airplane = new Screen_On();
if (i == 0) {
screenon_airplane.airplanei = 0;
registerReceiver(screenon_airplane, screenon);
} else if (i == 1) {
screenon_airplane.airplanei = 1;
registerReceiver(screenon_airplane, screenon);
} else if (i == -1) {
unregisterReceiver(screenon_airplane);
}
}
}
내 방송 수신기 : -
public class Screen_On extends BroadcastReceiver {
public int airplanei;
@Override
public void onReceive(final Context context, Intent intent) {
boolean isEnabled = Settings.System.getInt(
context.getContentResolver(), Settings.System.AIRPLANE_MODE_ON,
0) == 1;
if (airplanei == 0) {
if (isEnabled != false) {
Settings.System.putInt(context.getContentResolver(),
Settings.System.AIRPLANE_MODE_ON, 0);
Intent turnplaneoff = new Intent(
Intent.ACTION_AIRPLANE_MODE_CHANGED);
turnplaneoff.putExtra("state", 0);
context.sendBroadcast(turnplaneoff);
}
} else if (airplanei == 1) {
if (isEnabled == false) {
Settings.System.putInt(context.getContentResolver(),
Settings.System.AIRPLANE_MODE_ON, 1);
Intent turnplaneon = new Intent(
Intent.ACTION_AIRPLANE_MODE_CHANGED);
turnplaneon.putExtra("state", 1);
context.sendBroadcast(turnplaneon);
}
}
}
}
로그 캣 오류 : -
05-17 23:44:24.886: E/ActivityThread(2435): Activity com.dragonheart.autodroid.ActionActivities.WakeActivity has leaked IntentReceiver [email protected] that was originally registered here. Are you missing a call to unregisterReceiver()?
는 오류가 방송을 이야기하지와 함께 할 수있는 뭔가가 수 onPause() 또는 onDestroy()에서 무엇을 수신할까요? ... 일단 등록되면 생각할지라도 등록되지 않은, 활동에 관계없이 ...
감사 :
는 LocalBroadcastReceiver 사용하는 방법에 대한 예와,이 이전 질문을 참조하십시오. 나는 똑같은 것을 생각하고 있었지만 서비스를 많이 사용하는 것은 많은 RAM을 소비하고 더 중요한 것은 서비스가 종종 안드로이드에 의해 메모리를 비우기 위해 죽임을 당하기 때문에 서비스를 사용하지 말 것을 권고 받았다. –예, 서비스가 항상 백그라운드에서 실행되기 때문에 사실입니다. 그러나 필요하지 않으면 간단히 잠자기 상태로 설정하십시오. 전혀 실행하지 않는 것이 좋습니다.하지만 필요한만큼 방송 수신기를 사용하는 것이 비용입니다. –
SCREEN_ON에 대한 서비스를 시작하는 대신 매니페스트에서 USER_PRESENT를 사용하기로 결정했습니다. 내 애플 리케이션을 "깨끗하고"가볍게 유지하고 싶습니다. 게다가 사용자는 서비스로 인해 (서비스로 인해) RAM 용량을 2 메가 바이트 이상 차지하는 앱을 보았을 때, 특히 달리기 및 죽이기가있는 로우 엔드 전화기의 경우 작업 킬러 (나쁜 아이디어)를 사용하여이를 죽이기로 결정할 때 까다로운 마음을 갖습니다. 화면이 켜지 자마자 몇 가지 서비스를하면 꽤 느려질 수 있습니다. 어쨌든, 당신은 유효한 해결책을 제공하여 답으로 골랐습니다. 나중에 유용 할 수도 있습니다. 감사! =) –