为什么我的未绑定服务类不起作用?

时间:2015-04-18 21:14:38

标签: android android-service

我正在尝试使用service unbound在数据库上创建一个表来实现此目的。但我没有回应。 service开始,当我检查数据库时,没有它的痕迹! 我的班级unboundService

public class MyUnboundService extends Service {
    HttpClient httpClient = null;

    @Override
    public void onCreate() {
        Log.d("MY_UNBOUNDSERVICE", "onCreate()");
        httpClient = new DefaultHttpClient();
    }
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Context context = this.getApplicationContext();

        Toast.makeText(context, "Start service", Toast.LENGTH_SHORT).show();
        Log.d("MY_UNBOUNDSERVICE", "onStartCommand(...)");

        String create = "CREATE TABLE IF NOT EXISTS test(id INTEGER NOT NULL AUTO_INCREMENT PRIMARY KEY, " +
                "name TEXT, forename TEXT)";

        List<NameValuePair> nameValuePairs = new ArrayList<>(1);
        nameValuePairs.add(new BasicNameValuePair("createTabell", create));
        new AsyncTaskTabell(this).execute(new Pair<>(nameValuePairs, httpClient));

        return Service.START_STICKY;
    }

    @Override
    public void onDestroy() {
        Log.d("MY_UNBOUNDSERVICE", "onDestroy(...)");
        Context context = this.getApplicationContext();
        Toast.makeText(context, "Stopper service", Toast.LENGTH_SHORT).show();
        super.onDestroy();
    }
}

我正在从mainActivity调用MyUnboundService,如下所示:

Intent intent = new Intent(MainActivity.this, MyUnboundService.class);
            startService(intent);

我的代码出了什么问题? - 非常感谢任何帮助og建议,谢谢。 AsyncTask类:

public class AsyncTaskTabell extends AsyncTask<Pair<List<NameValuePair>, HttpClient>, Void, String> {

    private Context context = null;

    public AsyncTaskTabell(Context context) {
        this.context = context;
    }

    @Override
    protected String doInBackground(Pair<List<NameValuePair>, HttpClient>... params) {

        Pair pair = params[0];

        List<NameValuePair> urlParams = (List<NameValuePair>)pair.first;
        HttpClient httpClient = (HttpClient)pair.second;
        try {
            String serverURL = MainActivity.address + MainActivity.stringCommands[MainActivity.SF_POS];
            HttpPost httpPost = new HttpPost(serverURL);
            httpPost.setEntity(new UrlEncodedFormEntity(urlParams));

            HttpResponse response = httpClient.execute(httpPost);
            if (response.getStatusLine().getStatusCode() == 200) {
                return EntityUtils.toString(response.getEntity());
            }
            return "Wrong: " + response.getStatusLine().getStatusCode() + " " + response.getStatusLine().getReasonPhrase();

        } catch (ClientProtocolException e) {
            return e.getMessage();
        } catch (IOException e) {
            return e.getMessage();
        }
    }

    @Override
    protected void onPostExecute(String result) {
        Intent i = new Intent("pos").putExtra("theResult", result);
        LocalBroadcastManager.getInstance(context).sendBroadcast(i);
    }
} 

1 个答案:

答案 0 :(得分:-1)

您是否在AndroidManifest.xml文件中注册了您的服务? 尝试在application元素中添加此行:

<service android:enabled="true"
         android:name=".MyUnboundService"/>