2012-06-30 146 views
1

對於你們來說,這可能是一個簡單的問題,但它是我第一次嘗試創建一個應用程序,當我的應用程序關閉時應該在後臺運行。我的問題是:當我點擊我的「啓動服務」按鈕時,服務無法啓動。我沒有看到任何敬酒,也沒有看到logcat(也沒有錯誤)。提前致謝!服務未啓動

服務類

public class MyService extends Service { 
private static final String TAG = "MyService"; 

@Override 
public IBinder onBind(Intent intent) { 
    return null; 
} 

@Override 
public void onCreate() { 
    Toast.makeText(this, "My Service Created", Toast.LENGTH_LONG).show(); 
    Log.d(TAG, "onCreate"); 

} 

@Override 
public void onDestroy() { 
    Toast.makeText(this, "My Service Stopped", Toast.LENGTH_LONG).show(); 
    Log.d(TAG, "onDestroy"); 

} 

@Override 
public void onStart(Intent intent, int startid) { 
    Toast.makeText(this, "My Service Started", Toast.LENGTH_LONG).show(); 
    Log.d(TAG, "onStart"); 

} 
} 

我的主要活動

public void startServiceClick(View v){ 

    Log.d("foo", "onClick: starting srvice"); 
    startService(new Intent(this, MyService.class)); 
} 

public void stopServiceClick(View v){ 

    Log.d("foo", "onClick: stopping srvice"); 
    stopService(new Intent(this, MyService.class)); 
} 

清單XML

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
package="se.johanberntsson.main" 
android:versionCode="1" 
android:versionName="1.0" > 

<uses-sdk android:minSdkVersion="15" /> 
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/> 
<uses-permission android:name="android.permission.READ_PHONE_STATE"/> 
<uses-permission android:name="android.permission.INTERNET"/> 
<uses-permission android:name="android.permission.BLUETOOTH"/> 

<application 
    android:icon="@drawable/maps1" 
    android:label="@string/app_name" > 
    <uses-library android:name="com.google.android.maps" /> 
    <activity 
     android:name=".LongitudeActivity" 
     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=".DebugActivity" /> 
    <activity android:name=".GoogleMapsActivity" /> 

    <service android:enabled="true" android:name=".MyService" /> 

</application> 

</manifest> 
+0

你有沒有嘗試過context.startService? 正在啓動服務日誌是gettting生成的嗎? – AAnkit

+0

@AnkitAwasthi現在試過了,沒有成功。是的,我在日誌中獲得了「onclick,starting service」。但沒有任何服務..:/ – Johan

+0

我認爲答案張貼可以解決您的問題。如果不是發佈完整的活動代碼,將嘗試我的設置.. – AAnkit

回答

4
this is suggestion as all else looking fine 

應該叫超級每個函數一樣super.onCreate();

的可能服務已經啓動,並嘗試停止和重新啓動,並檢查它似乎敬酒....

嘗試

<service android:enabled="true" android:name="se.johanberntsson.servies.MyService" /> 
+1

http: //mobile.dzone.com/articles/android-services –

+1

沒有運氣...你確定它需要嗎? http://marakana.com/forums/android/examples/60.html – Johan

+0

這是建議,因爲所有其他看起來很好 –

0

我將你的服務添加到我的代碼中,它對我來說工作正常。請參閱下面的代碼並與您的相匹配。

活動>>

public class TestActivity extends Activity implements OnClickListener { 
    /** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
    Button bt = (Button)findViewById(R.id.button11); 
    bt.setOnClickListener(this); 
} 


public void startServiceClick(View v){ 

    Log.d("foo", "onClick: starting srvice"); 
    startService(new Intent(this, MyService.class)); 
} 

public void stopServiceClick(View v){ 

    Log.d("foo", "onClick: stopping srvice"); 
    stopService(new Intent(this, MyService.class)); 
} 

@Override 
public void onClick(View v) { 
    startServiceClick(v); 

} 

清單文件>>

<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
package="com.test" 
android:versionCode="1" 
android:versionName="1.0" > 

<uses-sdk android:minSdkVersion="8" /> 

<application 
    android:icon="@drawable/ic_launcher" 
    android:label="@string/app_name" > 
    <activity 
     android:name=".TestActivity" 
     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:enabled="true" android:name=".MyService" /> 

</application> 

+0

是的,我匹配。除了我不使用onclicklistener。只是'onClick'屬性在xml。但點擊獲取記錄,所以我猜那是不是問題 – Johan

+0

哦好吧..嘗試添加onclicklistener然後,。,Android不是那麼成熟...可能是你的代碼愚蠢的事 – AAnkit

+0

我認爲問題可能是我的服務是在一個不同的包叫'se.johanberntsson.services'? – Johan

1

您可能已經想通了這一點。在你的代碼中,你可以重寫幾個本地函數,以便添加一個toast和一個catlog條目。不要忘記添加超級{無論你正在調用的函數}。它所做的就是告訴android設備在功能沒有被覆蓋的情況下執行的功能,然後執行額外的功能。在你的情況下,如果你打電話給你的oncreate函數,那麼計算機將做出敬酒並添加一個catlog條目,但它不會創建該服務。但是,如果您添加super.onCreate,手機將創建該服務,然後它將執行您要求的操作。

@Override //forget about doing whatever onCreate usually does. 
    public void onCreate() { 
     super.onCreate(); // do whatever onCreate usually does. Then do whatever is after me. 
     Toast.makeText(this, "My Service Created", Toast.LENGTH_LONG).show();//toast 
     Log.d(TAG, "onCreate");//Add message to cat- log 

    } 

我希望這會有所幫助。