Question

I want to try to use system service such as wakelock but for some reason when i run it errors saying "System Services not available to Activities before onCreate()" I tried changing a little of the wakelock to see if it would fix it by moving the newWakeLock into the oncreate() but that still does not work, what can fix this?

public class MainActivity extends ListActivity {
    String[] activities = {"package1", "package2", "package3"};

    private PowerManager powerManager = (PowerManager) getSystemService(Context.POWER_SERVICE);
    //@SuppressWarnings("deprecation")
    private PowerManager.WakeLock wakeLock; //= powerManager.newWakeLock(PowerManager.FULL_WAKE_LOCK, "My Tag");


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main); 
        wakeLock = powerManager.newWakeLock(PowerManager.FULL_WAKE_LOCK, "My Tag");
        wakeLock.acquire();
        this.setListAdapter(new ArrayAdapter (this,    android.R.layout.simple_expandable_list_item_1, activities));
    }   

    public void onPause(){
        wakeLock.release();
    }

    public void onResume(){ }

    public void onDestroy(){ }

    /*  public void onListItemClick(ListView, View, int, long){    }*/    
}
Was it helpful?

Solution

This line :

 powerManager = (PowerManager) getSystemService(Context.POWER_SERVICE);

should be placed in your onCreate method.

Before that, activity is not alive and doesn't have a access to Android services.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top