1

我的 android 服务出现问题,无法启动:
这是我的代码片段 -

public class MainActivity extends Activity 
{

    static String exp_name = "";
    @SuppressLint("SdCardPath") 
    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_main);      
       Log.i("here", "1"); 
    }

    public void onStart(View view) 
    //triggered by start button viz present on R.layout.activity_main
    {
         startService(new Intent(MainActivity.this, ExperienceLoggerService.class));
         Log.i("here", "2"); 
    }

    ... code....

    //inner class service (needs to be inner class for certain features of my APP)

    public class ExperienceLoggerService extends Service
    {
       @Override
       public void onCreate()
       {
           Log.i("here", "3");
           super.onCreate();
       }

            .....code....
    }

    //back to outer class
    ....other code...... 

}

我的问题是我的android活动正常启动,
(即)“here 1”在活动启动时打印(LogCat),“here 2”在按下开始按钮时打印。
但是服务永远不会启动,“here 3”也不会打印!我只是不明白为什么?

这是我的android清单文件供参考:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.nostalgia"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission>

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.example.nostalgia.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>
    <activity
        android:name="com.example.nostalgia.StartExperienceActivity"
        android:label="@string/title_activity_start_experience" >
    </activity>
    <service android:enabled="true" android:name="com.example.nostalgia.ExperienceLoggerService"
        android:permission="android.permission.ACCESS_FINE_LOCATION"
        android:label="@string/service_name" android:exported="true"/>
</application>

4

1 回答 1

0

我想通了,在这种情况下内部类不起作用!
我必须将它们分隔在不同的类文件中,并通过 Intent 传递资源。
(直到现在我试图通过创建内部类在类之间共享资源)

于 2013-07-16T14:46:52.423 回答