Android服务无法启动

时间:2014-01-27 17:26:32

标签: android service

出于某种原因,服务没有启动,我的调试日志都没有被调用,我在服务中创建的祝酒也没有被显示(通过处理程序显示的Toast,未显示的toast代码,这里的简单指南供参考:http://www.jjoe64.com/2011/09/show-toast-notification-from-service.html

public class MainActivity extends Activity {

Intent locationPollingIntent;
Button updateLocationButton;

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        locationPollingIntent = new Intent(this, LocationService.class);

        updateLocationButton = (Button) findViewById(R.id.updateLocationButton);
        updateLocationButton.setText("Start");

        updateLocationButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.d(TAG, updateLocationButton.getText().toString());

                if (updateLocationButton.getText().toString() == "Start") {
                    MainActivity.this.startService(locationPollingIntent);
                    updateLocationButton.setText("Stop");

                } else if (updateLocationButton.getText().toString() == "Stop") {
                    MainActivity.this.stopService(locationPollingIntent);
                    updateLocationButton.setText("Start");
                }
            }
        });
    }
}

我的Service类的片段如下所示:

public class LocationService extends Service {

    @Override
    public void onCreate() {
        super.onCreate();

        Log.d(TAG, "Location service started");

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.d(TAG, "Starting Service" );
        grabLocation();
        return super.onStartCommand(intent, flags, startId);
    }

    // No implementation
    @Override
    public IBinder onBind(Intent arg0) {
        return null;
    }

最后,我的清单包含以下内容

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.rperryng.intellilocation"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="19" />

    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <service
            android:name=".LocationService"
            android:label="Location Service" />

        <activity
            android:name="com.rperryng.intellilocation.activities.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

值得注意的是,我的mainActivity位于包com.rperryng.intellilocation.activities中,而我的服务位于com.rperryng.intellilocation.backgroundServices

我发现以下日志可能与此问题有关:

01-27 12:53:58.804: W/ActivityManager(262): Unable to start service Intent {     cmp=com.rperryng.intellilocation/.backgroundServices.LocationService }: not found

1 个答案:

答案 0 :(得分:0)

尝试在清单android:name="com.rperryng.intellilocation.backgroundServices.LocationService"

中指定完整的包名称