데이터베이스에 일부 데이터 (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;
나는 당신이 서비스를 멈추고 있기 때문에 그것이라고 생각한다. - 문서마다 하나의 호출만으로는 전체 서비스를 멈춘다. – X3Btel
https://developer.android.com/guide/components/services.html 당신은 stopself로 시도 할 수있다. (startId); – X3Btel
@ X3 그가 'START_STICKY_COMPATIBILITY'을 (를) 돌려주고 있습니다. 그래서,'stopSelf()'만으로 충분할 것입니다. – Shaishav