1
안드로이드 장치 화면 켜기/켜기 또는 터치 입력이있을 때 자동으로 실행할 수있는 백그라운드 서비스를 만들고 싶습니다. 친절하게 도와주세요. 고마워요.안드로이드 백그라운드 서비스 시작 방법 화면 켜기/깨우기시에만 시작
안드로이드 장치 화면 켜기/켜기 또는 터치 입력이있을 때 자동으로 실행할 수있는 백그라운드 서비스를 만들고 싶습니다. 친절하게 도와주세요. 고마워요.안드로이드 백그라운드 서비스 시작 방법 화면 켜기/깨우기시에만 시작
귀하의 활동에서 onCreate
방법으로 서비스를 시작하십시오.
startService(new Intent(this, CDTservice.class));
이 서비스를 매니페스트 파일에 등록하는 것을 잊지 마십시오.
package com.example.sensorsample;
import java.util.List;
import android.app.Activity;
import android.app.ActivityManager;
import android.app.KeyguardManager;
import android.app.Service;
import android.app.KeyguardManager.KeyguardLock;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.IBinder;
import android.os.PowerManager;
import android.os.PowerManager.WakeLock;
import android.util.Log;
import android.view.WindowManager;
import android.widget.Toast;
public class CDTservice extends Service {
ScreenBroadcastReceiver m_receiver;
public String activityname;
PowerManager pm;
WakeLock wl;
@Override
public void onCreate() {
IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
filter.addAction(Intent.ACTION_SCREEN_OFF);
m_receiver = new ScreenBroadcastReceiver();
registerReceiver(m_receiver, filter);
Log.d("Widgettool", "works");
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
start();
Toast.makeText(getBaseContext(), "SERVICE ON", Toast.LENGTH_SHORT)
.show();
return START_STICKY;
}
@Override
public void onDestroy() {
stop();
super.onDestroy();
unregisterReceiver(m_receiver);
wl.release();
}
@SuppressWarnings("deprecation")
public void start() {
try {
pm = (PowerManager) getApplicationContext().getSystemService(
Context.POWER_SERVICE);
wl = pm.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK
| PowerManager.ACQUIRE_CAUSES_WAKEUP, getClass().getName());
wl.acquire();
KeyguardManager mgr = (KeyguardManager)getSystemService(Activity.KEYGUARD_SERVICE);
KeyguardLock lock = mgr.newKeyguardLock(KEYGUARD_SERVICE);
lock.disableKeyguard();
//new SensorActivity().service();
} catch (Exception e) {
}
}
public void stop() {
ActivityManager am = (ActivityManager) this
.getSystemService(ACTIVITY_SERVICE);
List<ActivityManager.RunningTaskInfo> taskInfo = am.getRunningTasks(1);
Log.d("topActivity", "CURRENT Activity ::"
+ taskInfo.get(0).topActivity.getClassName());
activityname = taskInfo.get(0).topActivity.getClassName();
ComponentName componentInfo = taskInfo.get(0).topActivity;
componentInfo.getPackageName();
}
private class ScreenBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
Log.d("ON SCREEN ON", "might hang here");
// start();
// stop();
} else if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
start();
Log.d("SCREEN OFF", "might hang here");
// stop();
}
}
}
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
}
소스 코드를 사용해 보았지만 작동하지 않습니다 ... 원하는 것은 화면이 잠길 때 또는 깨어있을 때 응용 프로그램이 열리는 것입니다. – mighty
하면 해당 질문 http://stackoverflow.com/questions/9477922/android-broadcast-receiver-for-screen-on-and-screen-를 따라 여기
는 서비스 클래스입니다 끄기 –