2017-10-19 76 views
2

我有必须在设备启动时启动的应用程序。它运行良好,但问题是,当应用程序完成启动时,屏幕已经很暗。我怎样才能让它不会入睡? 我的接收器:如果应用程序在启动时运行,如何使屏幕不会入睡?

<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."); 
    } 
    } 
} 

在这个设备上是Android 6.0。

回答

3

您可以使用wakelocks,这需要某些权限,或者您可以在onCreate方法中放置一个标志,该标志根据docs不需要权限。

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

wakelock文档给出了上面的例子。

相关问题