Question

All I want to do is start the wake_lock with one button and end it with another. It's to use in a larger app I'm creating but this is the part I've been having trouble with. I've looked on this site and others and found the exact same codes for all but its not working for me so - what am I doing wrong?

My Java Code:

package com.example.tester_app;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.os.PowerManager;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class TestMain extends Activity {

PowerManager.WakeLock wl;
Button bstart, bstop;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView (R.layout.main);

    //Views from xml
    bstart = (Button) findViewById (R.id.bStart);
    bstop = (Button) findViewById (R.id.bStop);


    //power manager for wake lock..
    PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
    wl = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK, "My Tag");


    bstart.setOnClickListener(new View.OnClickListener() {

        public void onClick(View arg0) {
            Toast.makeText(TestMain.this, "started", Toast.LENGTH_SHORT).show();
            wl.acquire(); //keeps cpu from sleeping
        }
    });

    bstop.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            Toast.makeText(TestMain.this, "stopped", Toast.LENGTH_SHORT).show();
            wl.release();
        }
    }); 
}
}

My XML Code:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/main_layout_id"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:orientation="vertical" >

<Button
    android:id="@+id/bStart"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Start" />

<Button
    android:id="@+id/bStop"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="12dp"
    android:text="Stop" />

</LinearLayout>

My Manifest Code:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.tester_app"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="15" />
<uses-permission android:name="android.permission.WAKE_LOCK" ></uses-permission>



<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".Main"
        android:label="@string/title_activity_main" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

</manifest>
Was it helpful?

Solution

Looks like the name of your Activity class is different from the name you've given in the manifest (TestMain vs Main). Make sure they're both the same.

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