0

Right at the acquire() it fails. Eclipse doesn't say what the error was. It just stops the execution on my emulator and gives me that "Class File Editor" "Source not found" display.

public class MyAppActivity extends Activity {

    private PowerManager pManager;
    private PowerManager.WakeLock wakeLock;

    public void onCreate(Bundle savedInstanceState) {        
          super.onCreate(savedInstanceState);
          // setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); 
          setContentView(R.layout.main);
          allocStructs();
    }

    private void allocStructs() {

        // I've tried this with "getBaseContext()" and with "this"
        // same results.  I get a pManager and a wakeLock
        // Then it crashes when I attempt to acquire
        pManager = (PowerManager)getBaseContext().getSystemService(
                               Context.POWER_SERVICE);
        wakeLock = pManager.newWakeLock(       
                                    PowerManager.FULL_WAKE_LOCK, "full");
    }

    public void onWakeLockButtonClicked(View view) {
        boolean checked = ((RadioButton) view).isChecked();
        if (!checked) {
            return;
        }
        if (!wakeLock.isHeld()) {
            wakeLock.acquire();    // fails here
        }
    }
}
Joe C
  • 2,728
  • 4
  • 30
  • 38
  • What does the logs say? – mach Jul 02 '13 at 19:03
  • Which log? My "LogCat" filtered for my App doesn't say anything. I clear it just before I cause the failaure, then I cause the failure, and the LogCat is empty. – Joe C Jul 02 '13 at 19:14
  • What is this "Class File Editor" "Source not found" ? Screen ? Where is `allocStructs()` called ? Post more of your code. Edit : add @user if you want _user_ to be notified of your comment – Mr_and_Mrs_D Jul 03 '13 at 15:31
  • Well I attached more source code, now I no longer get the "Class File editor Source not Found Screen". Now it simply stops at some line of code within the WakeLock handler. I can't see any indication of what the error was or why it stopped. This is my main entry point class for my App. – Joe C Jul 03 '13 at 15:43
  • Post your logs with no filters. Do you `release()` someplace ? – Mr_and_Mrs_D Jul 03 '13 at 15:50
  • My LogCat filtered by my App showed nothing. I guess I should have looked at the unfiltered LogCat. Maybe it did say something about permission. Anyway once I put the try ... catch then I got the info I neeeded. Well next time I'll be able to figure out the problem faster. Thx anyway – Joe C Jul 03 '13 at 16:24
  • accept your answer then :) – Mr_and_Mrs_D Jul 03 '13 at 16:41

1 Answers1

1

OK I got my answer and am embarassed. The quick answer is I didn't get the permission for Wake Locks in the manifest.

I had read the part that you need to get the wakelock permission but I thought in the debug emulator you may not need it. Or it may get settled just by pressing . Then by the way it was stopping I thought it was a crash, not a permission violation. So I added this:

    try {
         wakeLocks.acquire();
    } catch (Exception e) {
         e.printStackTrace();
         return;
    }

And sure enough it was a permission violation. This link told me how to add the permission to my manifest.

How to get an Android WakeLock to work?

I couldn't figure out how to add the permission thru those menus, but I added this line to the xml source directly.

 <uses-permission android:name="android.permission.WAKE_LOCK" />

Then it works.

Community
  • 1
  • 1
Joe C
  • 2,728
  • 4
  • 30
  • 38