2017-10-19 9 views
2

J'ai l'application qui doit démarrer sur le démarrage de l'appareil. Cela fonctionne bien mais le problème est que lorsque l'application termine le démarrage, l'écran est déjà sombre. Comment puis-je le faire ne pas s'endormir? Mon récepteur:Comment faire pour qu'un écran ne s'endorme pas si l'application s'exécute au démarrage?

<receiver 
     android:enabled="true" 
     android:exported="true" 
     android:name=".view.receivers.BootReceiver" 
     android:permission="android.permission.RECEIVE_BOOT_COMPLETED"> 
     <intent-filter> 
     <action android:name="android.intent.action.BOOT_COMPLETED" /> 
     <action android:name="android.intent.action.QUICKBOOT_POWERON" /> 

     <category android:name="android.intent.category.DEFAULT" /> 
     </intent-filter> 
    </receiver> 


public class BootReceiver extends BroadcastReceiver { 

    private final static String LOG_TAG = BootReceiver.class.getSimpleName(); 

    @Override 
    public void onReceive(Context context, Intent intent) { 
    if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)) { 
     Log.i(LOG_TAG, "Loading after booting..."); 
     Intent startCalendarActivityIntent = new Intent(context, CalendarActivity.class); 
     startCalendarActivityIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
     context.startActivity(startCalendarActivityIntent); 
    } else { 
     Log.e(LOG_TAG, "Error while restarting after boot."); 
    } 
    } 
} 

Sur cet appareil est Android 6.0.

Répondre

3

Vous pouvez utiliser wakelocks, qui nécessite certaines autorisations, ou vous pouvez placer un indicateur dans votre méthode onCreate qui, selon le docs, ne nécessite pas d'autorisation.

getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); 

La documentation de wakelock a donné l'exemple ci-dessus.