簡體   English   中英

服務電話沒有任何反應

[英]Nothing happens on service call

我創建了一個全新的簡單項目(在Android Studio 0.8.2上)以測試Service,但不確定為什么它不起作用。

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嗎? 我較舊的項目雖然可以正常工作。

您已經在<application>元素之外聲明了<service android:name=".TestService"> 它應該與其余組件一起使用:

<?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不會抱怨嗎?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM