我只是剛剛開始android開發,並已閱讀有關服務here。我創建了一個服務,然後嘗試從一項活動開始,但我無法弄清楚爲什麼它不會顯示敬酒通知。啓動服務
我已經得到了以下活動:
package com.example;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
public class Example extends Activity
{
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
Context context = getApplicationContext();
Intent intent = new Intent(context, MService.class);
startService(intent);
setContentView(R.layout.main);
}
}
,在我的清單我有這樣的:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example"
android:versionCode="1"
android:versionName="1.0">
<application android:label="example" android:icon="@drawable/icon">
<activity android:name="Example"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<service android:name=".MService" />
</activity>
</application>
,然後我的服務是這樣的:
package com.example;
import android.app.IntentService;
import android.content.Intent;
import android.widget.Toast;
public class MService extends IntentService {
public MService() {
super("MService");
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Toast.makeText(this, "service starting", Toast.LENGTH_SHORT).show();
return super.onStartCommand(intent,flags,startId);
}
@Override
protected void onHandleIntent(Intent intent) {
Toast.makeText(this, "Systems starting", Toast.LENGTH_SHORT).show();
}
}
我知道你不是真的要覆蓋onStartCommand但這更多的是掛職鍛鍊,因爲我還沒有
試過了,仍然沒有運氣,謝謝你的建議,雖然 – DNN 2011-04-29 22:53:26
你嘗試把它放在一個重寫'的onCreate()'方法? – Haphazard 2011-04-29 23:29:16