pendingintent.getservice无法正常工作

时间:2014-03-11 13:54:41

标签: android android-intent android-pendingintent

我正在尝试使用pendingintent启动服务,这就是我正在尝试的方式

btnSave.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub




                InfoLayout.setVisibility(LinearLayout.VISIBLE);
                mIntentService = new Intent(MainActivity.this, LocationService.class);
                mPendingIntent = PendingIntent.getService(
                        MainActivity.this, 1, mIntentService, 0);

                locationrequest = LocationRequest.create();
                locationrequest.setInterval(10000 * 6);
                locationClient.requestLocationUpdates(locationrequest,
                        mPendingIntent);


                Toast.makeText(MainActivity.this, "Starting service",
                        Toast.LENGTH_SHORT).show();


        }
    });

我在2.3.5上尝试了这个代码并且工作正常,但是当我在4.4.2上尝试这个代码时,我的服务根本没有启动。我不明白为什么会这样。可以请一些人解释我在做什么。

提前致谢:)

修改

public void onStart(Intent intent, int startId) {
        // Toast.makeText(this, "in service", Toast.LENGTH_LONG).show();
        Location location = intent
                .getParcelableExtra(LocationClient.KEY_LOCATION_CHANGED);

        db = new PhoneDatasource(this);
        db.open();
        contactlist = db.getAllContacts();
        db.close();

        if (location != null) {
            add = null;
            Constants.latitude = location.getLatitude();
            Constants.longitude = location.getLongitude();
        }

1 个答案:

答案 0 :(得分:2)

答案可能在此页面上(“Android 4.4上的位置设置更改”):https://support.google.com/nexus/answer/3467281

也就是说,在Android 4.4中,您必须转到新的位置设置并明确打开它(并配置模式),否则将不允许任何应用获取任何位置信息。此外,您需要了解不同的位置模式以及它们将如何影响应用获取位置信息的能力。在4.4中阅读链接页面至关重要。