无法正确使用Service / IntentService保存和显示数据

时间:2016-11-07 12:53:53

标签: android android-intent android-service android-intentservice

我在每个应用程序启动时都在更新数据库,之前我使用的是IntentService,但是我的活动在数据更新之前就开始了,所以数据列表是空的,我用Service重写了它(i我创建了自己的Service,表现得像IntentService,所以我可以控制由Service创建的线程。正如您所看到的,我首先保存数据,然后才开始我的活动。但我的活动是在数据保存之前开始的,任何建议为什么,或者我该如何解决这个问题?

同时

如果你有一些使用ORM /数据库异步的经验,那将是很好的,我只是想学习如何正确使用数据库。任何建议都是相关的。

public class DatabaseWorkService extends Service {
    private ServiceHandler mServiceHandler;
    private DatabaseReference mDatabase;

    private ConnectivityManager conMan;
    private NetworkInfo netInfo;
    private String currentTask;
    private Intent tempIntent;
    private Looper mServiceLooper;
    private ResultReceiver resultReceiver;
    private Context context =this;
    public DatabaseWorkService delegate = null;


    // Handler that receives messages from the thread
    private final class ServiceHandler extends Handler {
        public ServiceHandler(Looper looper) {
            super(looper);
        }
        @Override
        public void handleMessage(Message msg) {
            if(msg.getData()!=null) {
                switch (msg.getData().getString(Utils.INTENT_SERVICE_INVOKE)) {
                    case Utils.LOAD_All_DATA: {
                        saveActivities();
                        savePersons();
                        savePictureData();

                        Intent intent = new Intent(context, MainActivity.class);
                        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        startActivity(intent);
                        Log.e("order", "forth");
                        break;
                    }
                    case Utils.READ_ACTIONS_DATA: {
                        readActionData();
                        break;
                    }
                    case Utils.READ_PERSONS_DATA: {
                        readPersonsData();
                        break;
                    }
                }
            }
            stopSelf(msg.arg1);
        }
    }

    @Override
    public void onCreate() {
        // Start up the thread running the service.  Note that we create a
        // separate thread because the service normally runs in the process's
        // main thread, which we don't want to block.  We also make it
        // background priority so CPU-intensive work will not disrupt our UI.
        HandlerThread thread = new HandlerThread("ServiceStartArguments");
        thread.start();

        // Get the HandlerThread's Looper and use it for our Handler
        mServiceLooper = thread.getLooper();
        mServiceHandler = new ServiceHandler(mServiceLooper);
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.e("service","service started");

        Log.e("data",intent.getStringExtra(Utils.INTENT_SERVICE_INVOKE));

        Bundle bundle = new Bundle();
        bundle.putString(Utils.INTENT_SERVICE_INVOKE,intent.getStringExtra(Utils.INTENT_SERVICE_INVOKE));

        // For each start request, send a message to start a job and deliver the
        // start ID so we know which request we're stopping when we finish the job
        Message msg = mServiceHandler.obtainMessage();
        msg.arg1 = startId;
        msg.setData(bundle);
        mServiceHandler.sendMessage(msg);

        // If we get killed, after returning from here, restart
        return START_STICKY;
    }


    @Override
    public void onDestroy() {
        Toast.makeText(this, "service done", Toast.LENGTH_SHORT).show();
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

1 个答案:

答案 0 :(得分:0)

如果这些quires在数据库中插入后返回任何值,那么只需要启动活动

                saveActivities();
                savePersons();
                savePictureData();

尝试以下代码在主线程处理程序

中启动您的活动
 Handler handler = new Handler(Looper.getMainLooper());
                    handler.post(new Runnable() {
                        @Override
                        public void run() {
                              Intent intent = new Intent(context, MainActivity.class);
                            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                            startActivity(intent);
                        }
                    });