2014-01-27 8 views
0

По какой-то причине служба не запускается, ни один из моих журналов отладки не вызывается, и не отображаются тосты, которые я создал в отображаемой услуге (тосты, отображаемые через обработчик, код тоста не показан , простое руководство здесь для справки: http://www.jjoe64.com/2011/09/show-toast-notification-from-service.html)Служба Android не запускается

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 
+0

Не используйте '==' в сравнении строк, используйте 'equals()'. –

ответ

0

Try указав полное имя пакета в вашем манифесте android:name="com.rperryng.intellilocation.backgroundServices.LocationService"

+0

Пробовал это, возвращает исключение нулевого указателя при вызове startervice с намерением – rperryng

+0

«MainActivity.this», если странно, почему бы не просто «startService (locationPollingIntent);'? –

+0

У меня возникли проблемы с вызовами методов из onClick и других подобных методов, но вы правы, я думаю, в этом случае, поскольку контекст не имеет значения, это не нужно. После их удаления ничего не изменилось. – rperryng

Смежные вопросы