//Shake.java
package com.example.shakeresponse;
import android.app.Notification;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;
import android.os.PowerManager;
public class Shake extends Service implements AccelerometerListener {
PowerManager pm;
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
public void onCreate() {
super.onCreate();
}
public void onShake(float force) {
pm = (PowerManager) getBaseContext()
.getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wl = pm.newWakeLock(
PowerManager.FULL_WAKE_LOCK
| PowerManager.ACQUIRE_CAUSES_WAKEUP, "");
wl.acquire(10000);
Intent i = new Intent(this, Shake.class);
startService(i);
}
public int onStartCommand(Intent intent, int flags, int startId) {
super.onStartCommand(intent, flags, startId);
startForeground(0, new Notification());
// Check device supported Accelerometer sensor or not
if (AccelerometerManager.isSupported(this)) {
// Start Accelerometer Listening
AccelerometerManager.startListening(this);
}
return START_REDELIVER_INTENT;
}
public void onAccelerationChanged(float x, float y, float z) {
// TODO Auto-generated method stub
Intent i = new Intent(this, Shake.class);
startService(i);
}
}
우선 위는 서비스 부분의 코드이고요...
흔들림 감지 소스는 구현해 놓은 상태입니다.
//BootStartReceiver.java
package com.example.shakeresponse;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class BootStartReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)) {
Intent i = new Intent(context, Shake.class);
context.startService(i);
}
}
}
-------------------------------------------------------------------
//MainActivity.java
package com.example.shakeresponse;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
public class MainActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// TODO Auto-generated method stub
Intent i = new Intent(this, Shake.class);
startService(i);
}
}
이런식으로 앱을 시작할 때, 휴대폰을 시작할 때, 서비스를 포그라운드로 실행되게끔 하였습니다.
근데 문제는 꺼져도 언제든지 흔들면 화면이 켜져야 하는데, 일정시간 지나면 서비스가 죽는(?)듯 합니다..ㅜㅜ
꺼지고 바로 흔들면 켜지긴 하는데..어쩔 땐 가끔씩 켜지긴 합니다만...
휴면 상태에서도 서비스를 계속 실행시켜야 할 듯 한데, 잘 모르겠습니다...ㅜㅜ
도와주세요..ㅜㅜ