我有一个活动,它在数据库中存储一些数据(edittext和image)。 服务有时运行,有时不运行。我正在为imageview加上一些文本数据并将它传递给调用我的数据库类的服务。为什么服务不会每次都被调用?有时服务被调用,有时它不会
将数据传递给服务:
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;
我认为这是因为你停止服务 - 根据doccumentations只有单一的电话,以阻止自我采样整个服务 – X3Btel
https://developer.android.com/guide/components/services.html你可以尝试与自己(startId); – X3Btel
@ X3Btel他正在返回'START_STICKY_COMPATIBILITY'。所以,'stopSelf()'应该就够了。 – Shaishav