2016-08-02 4 views
0

데이터베이스에 일부 데이터 (edittext 및 image)를 저장하는 활동이 있습니다. 서비스가 실행되고 언젠가 실행되지 않습니다. 나는 이미지 뷰를위한 이미지와 텍스트 데이터를 가져 와서 데이터베이스 클래스를 호출하는 서비스로 전달하고있다. 왜 매번 서비스를받지 못하는거야?때로는 서비스가 호출되고 때로는 서비스가 호출되지 않습니다

서비스에 데이터를 전달 :

imgView = (ImageView)findViewById(R.id.imageView2); 
    if(imgView.getDrawable()==null) { 
     imageData = null ; 
    } 
    else { 
     Bitmap bitmap = ((BitmapDrawable) imgView.getDrawable()).getBitmap(); 
      imageData = getBytes(bitmap); 
    } 

    Intent i = new Intent(this,TaskService.class); 
    i.putExtra("heading",s); 
    i.putExtra("subject",s1); 
    i.putExtra("date",s2); 
    i.putExtra("notes",s3); 
    i.putExtra("imageData",imageData); 
    startService(i); 

을이 내 서비스 클래스 : (TaskDatabaseClass이 내 DB 클래스입니다)

public class TaskService extends Service { 

String s,s1,s2,s3; 
byte[] imagedata; 
public TaskService() { 
} 

@Override 
public int onStartCommand(Intent intent, int flags, int startId) { 

    s = intent.getStringExtra("heading"); 
    s1 = intent.getStringExtra("subject"); 
    s2 = intent.getStringExtra("date"); 
    s3 = intent.getStringExtra("notes"); 
    imagedata = intent.getByteArrayExtra("imageData"); 

    Runnable r = new Runnable() { 
     @Override 
     public void run() { 
      try{ 
       TaskDatabaseClass enterData = new TaskDatabaseClass(TaskService.this); 
       enterData.open(); 
       enterData.createEntry(s,s1,s2,s3,imagedata); 
       enterData.close(); 
      }catch(Exception e) { 
       } 
     } 
    }; 
    Thread thread = new Thread(r); 
    thread.start(); 
    this.stopSelf(); 
    return 0; 
} 

@Override 
public void onDestroy() { 
    Log.i(TAG ," destroying "); 
} 

@Override 
public IBinder onBind(Intent intent) { 
    // TODO: Return the communication channel to the service. 
    // throw new UnsupportedOperationException("Not yet implemented"); 
    return null; 
+1

나는 당신이 서비스를 멈추고 있기 때문에 그것이라고 생각한다. - 문서마다 하나의 호출만으로는 전체 서비스를 멈춘다. – X3Btel

+0

https://developer.android.com/guide/components/services.html 당신은 stopself로 시도 할 수있다. (startId); – X3Btel

+0

@ X3 그가 'START_STICKY_COMPATIBILITY'을 (를) 돌려주고 있습니다. 그래서,'stopSelf()'만으로 충분할 것입니다. – Shaishav

답변

1

그것은 당신이 데이터베이스 쓰기 작업의 약간의를하고있는 것 같다 귀하의 Service 안드로이드 서비스가 이러한 작업에 적합하지 않기 때문에 중복되는 것처럼 보입니다 (쓸모 없지는 않을 것입니다). 새로운 스레드를 생성하기 만하면, 심지어는 Activity의 새 스레드에서 같은 작업을 호출 할 수 있으며 현재 설정보다 훨씬 효율적입니다. Service에서이 작업을 수행

심지어 당신이 시스템에 추가적인 오버 헤드 의도를 통한 데이터 전송에 대해 걱정할 필요가하는 Service를 만들고 그것의 라이프 사이클을 유지하는 안드로이드 시스템에 오버 헤드를 생성뿐만 아닙니다.

Services의 사용에 대한 자세한 내용은 here의 "서비스 란 무엇인가"에서 확인할 수 있습니다.

+0

당신이 말한대로 해주고 알려 드리겠습니다! – aman003

+0

일부 문제가 줄어 듭니다. 감사! – aman003

관련 문제