我的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活动正常启动,
(即)“此处1”在活动启动时打印(LogCat),按下开始按钮时“此处2”打印。
但是服务永远不会开始,“这里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>
答案 0 :(得分:0)
我明白了,在这种情况下,内部课程不起作用!!
我必须在不同的类文件中分隔它们,并通过Intent传递资源。
(到现在为止,我试图通过创建内部类来共享类之间的资源)