package com.ondaysteve.administrator.myapplication;
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;
public class MyService extends Service {
BroadcastReceiver receiver;
public MyService() {
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
IntentFilter filter = new IntentFilter();
filter.addAction(Intent.ACTION_USER_PRESENT);
/* 충전기 연결/해제 액션을 intentFilter에 등록 */
receiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String message = ("액션 발생! " + intent.getAction());
/* Toast.makeText(context, message, Toast.LENGTH_LONG).show();*/ //토스트 띄우는거
Log.d("My Service1", "스레드 동작중: ");
Intent i= new Intent(context, popup_page.class);
//위에 만들어진 Intent에 의해 실행되는 Activity는
//액티비티 스택에서 새로운 Task로 Activity를 실행하도록 하라는 설정.
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//Intent에 설정된 Component(여기서는 MainActivity)를 실행
context.startActivity(i);
}
};
registerReceiver(receiver, new IntentFilter(Intent.ACTION_USER_PRESENT));
return START_NOT_STICKY;//강제종료되었을때 다시 시작 하는 거
}
@Override
public void onDestroy() {
super.onDestroy();
Log.d("endend", "onDestroy: ");
}
@Override
public IBinder onBind(Intent intent) {
// TODO: Return the communication channel to the service.
throw new UnsupportedOperationException("Not yet implemented");
}
}
화면잠금 해제하면 특정 액티비티를 여는 서비스를 만들었습니다.(리시버에 서비스를 넣어서)
동작은 잘 되는데 가끔씩 서비스가 꺼짐니다 어떤 경우의수인지는 잘 모르겠는데 핸드폰에 빌드해서
화면잠금해제하면 특정액티비티 뜨게 해놨는데 어느순간보면 서비스가 종료되서 안뜹니다.
이런걸 어떻게 해결할까요?