我有一项简单的服务,但它似乎没有启动,因为Log
logcat
都没有显示
public class MyService extends Service {
@Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
Log.d("ID", "Y");
}
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
@Override
public void onStart(Intent intent, int startId) {
// TODO Auto-generated method stub
super.onStart(intent, startId);
Log.d("S", "Y");
}
@Override
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
}
}
我称这样的服务为:
Intent service = new Intent(this, MyService.class);
startService(service);
主要节日是:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.servicetutorials"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.servicetutorials.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>
<service
android:name=".MyService"
android:icon="@drawable/ic_launcher"
android:label="label" >
</service>
</application>
</manifest>
请问我做错了什么?
答案 0 :(得分:8)
A)我不认为服务有android:icon="@drawable/ic_launcher"
B)如果你正在开始服务onClickListener它应该是
startService(new Intent(Activity.this, MyService.class));
C)确保logcat
显示Log.d
D)AndroidManifest.xml你的服务声明
<service
android:name="com.example.servicetutorials.MyService"
android:enabled="true"/>
</service>
我想它应该有用
详细介绍here
服务完全不需要图标