• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Wake up screen a phone from a service

student71

Lurker
Hi there.
I need a help with wake up phone from the IntentService (background) during the time onHandleIntent is working.
AndroidManifest.xml
[HIGH]
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.survey.powermanagerservice"
android:versionCode="1"
android:versionName="1.0" >

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


<application
android:debuggable="true"
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.survey.powermanagerservice.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />

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

<service android:name=".MyPowerService" android:exported="true"></service>
</application>

</manifest>
[/HIGH]

MainActivity.java
[HIGH]import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;

public class MainActivity extends Activity {

static Button btnStartPowerService;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

btnStartPowerService=(Button) findViewById(R.id.button1);
btnStartPowerService.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
try {
Thread.sleep(2*1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Context AppContext = getApplicationContext();
Intent PowerIntent = new Intent(AppContext,MyPowerService.class);
//AppContext.startService(PowerIntent);
MyPowerService.startWIS(AppContext, PowerIntent);
}
});
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
[/HIGH]

MyPowerService.java
[HIGH]import android.app.IntentService;
import android.content.Context;
import android.content.Intent;
import android.os.PowerManager;
import android.os.PowerManager.WakeLock;
import android.util.Log;

public class MyPowerService extends IntentService {

private static final String NAME = MyPowerService.class.getName() + ".Lock";
private static volatile WakeLock lockStatic = null;

public MyPowerService() {
super("MyPowerService");
setIntentRedelivery(true);
// TODO Auto-generated constructor stub
}
public MyPowerService(String name) {
super(name);
setIntentRedelivery(true);
// TODO Auto-generated constructor stub
}

synchronized private static PowerManager.WakeLock getLock(Context context) {
if (lockStatic == null) {
PowerManager mgr = (PowerManager) context
.getSystemService(Context.POWER_SERVICE);
lockStatic = mgr.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK |
PowerManager.ACQUIRE_CAUSES_WAKEUP, NAME);
lockStatic.setReferenceCounted(true);
}
return (lockStatic);
}

public static void startWIS(Context ctxt, Intent i) {
getLock(ctxt.getApplicationContext()).acquire();
ctxt.startService(i);
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
PowerManager.WakeLock lock = getLock(this.getApplicationContext());

if (!lock.isHeld() || (flags & START_FLAG_REDELIVERY) != 0) {
lock.acquire();
}
super.onStartCommand(intent, flags, startId);
return (START_REDELIVER_INTENT);
}
@Override
protected void onHandleIntent(Intent intent) {
try {
Thread.sleep(10*1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
PowerManager.WakeLock lock = getLock(this.getApplicationContext());
if (lock.isHeld()) lock.release();
}

}

}
[/HIGH]
 
Back
Top Bottom