服务电话没有任何反应

时间:2014-07-12 18:22:54

标签: java android xml service android-manifest

我创建了一个全新的简单项目(在Android Studio 0.8.2上)来测试服务,但不确定为什么它不起作用。

MyActivity.java

import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;

public class MyActivity extends ActionBarActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_my);

    startService(new Intent(this, TestService.class));
}

TestService.java

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;

public class TestService extends Service {
@Override
public IBinder onBind(Intent intent) {
    return null;
}

@Override
public void onCreate() {
    super.onCreate();
    // do something when the service is created
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    // TODO Auto-generated method stub
    super.onStartCommand(intent, flags, startId);

    return START_REDELIVER_INTENT;
}
}

我也在AndroidManifest.xml中添加了

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android"     
package="..." >         
<application         ...     
</application>     
<service         
android:name=".TestService">
</service> 
</manifest>

正确地说,基于这些,服务应该在运行/调试时启动,但没有任何反应。我在onStartCommand上放置了一个断点,但它没有到达它。请帮忙。是因为我设置了minSDK = 8吗?我的旧项目虽然提供服务。

1 个答案:

答案 0 :(得分:2)

您已在<service android:name=".TestService">元素之外声明<application>。它应该与你的其他组件一起进去:

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="..." >         
    <application     
        <service         
            android:name=".TestService">
        </service> 
        ...
    </application>     
</manifest>

如果AndroidManifest.xml无效,Eclipse中的构建将失败; Android Studio没有抱怨它吗?

相关问题