use*_*331 0 android android-intent android-service
我有一个简单的服务,但它似乎没有开始,因为我的两个都没有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();
}
}
Run Code Online (Sandbox Code Playgroud)
我把这样的服务称为:
Intent service = new Intent(this, MyService.class);
startService(service);
Run Code Online (Sandbox Code Playgroud)
主要节日是:
<?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>
Run Code Online (Sandbox Code Playgroud)
我做错了什么?
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>
Run Code Online (Sandbox Code Playgroud)
我想它应该有效
在这里详细介绍
服务完全不需要图标
| 归档时间: |
|
| 查看次数: |
7419 次 |
| 最近记录: |